Stop object rotation

Hi, Im having trouble trying to figure out how to accomplish what I want.

Simple Idea, I have a cannon that follow the mouse and a button that fires the cannon, the problem is when I click the fire btn the direction of the cannon is pointing towards the button, and i want to FIRST aim the cannon freeze the position and FIRE it with the other btn

This is the script Im currently using for the mouse follow behavior.

private var lineRenderer : LineRenderer;

function Start() {
     lineRenderer = GetComponent(LineRenderer);
     lineRenderer.SetWidth(4,3);
     lineRenderer.SetVertexCount(2);
}

// speed is the rate at which the object will rotate
var speed = 4.0;

function Update () {

    // Generate a plane that intersects the transform's position with an upwards normal.
    var playerPlane = new Plane(Vector3.up, transform.position);
    // Generate a ray from the cursor position
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    // Determine the point where the cursor ray intersects the plane.
    // This will be the point that the object must look towards to be looking at the mouse.
    // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
    //   then find the point along that ray that meets that distance.  This will be the point
    //   to look at.
    var hitdist = 0.0;
    // If the ray is parallel to the plane, Raycast will return false.
    if (playerPlane.Raycast (ray, hitdist)) {
        // Get the point along the ray that hits the calculated distance.
        var targetPoint = ray.GetPoint(hitdist);
        // Determine the target rotation.  This is the rotation if the transform looks at the target point.
        var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
        // Smoothly rotate towards the target point.
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
        // shorten the ray if it's hitting some obstacle:
        var hit: RaycastHit;
        if (Physics.Linecast(transform.position, targetPoint, hit)){
            targetPoint = hit.point;
        }
    } else {
        targetPoint = transform.position;
    }
    lineRenderer.SetPosition(0, transform.position);
    lineRenderer.SetPosition(1, targetPoint);
}

Any help is apreciated, best regards :wink:

To freeze the aim, you could use a bool variable and toggle its value when the button is pressed, and place the aim code inside an if - but you must declare the variable targetPoint outside the function, so it can “remember” the last point aimed when you freeze the aim.

 
private var lineRenderer : LineRenderer;
 
function Start() {
     lineRenderer = GetComponent(LineRenderer);
     lineRenderer.SetWidth(4,3);
     lineRenderer.SetVertexCount(2);
}
 
// speed is the rate at which the object will rotate
var speed = 4.0;
var targetPoint: Vector3; // save the target point even when freezed
var freeze = false; // tells if aim is freezed or not
 

function Update () {
 
    if (Input.GetMouseButtonDown(1){ // freeze/unfreeze the aim with Right Mouse Button
        freeze = !freeze;
    }
    if (freeze){ // if aim freezed...
        if (Input.GetMouseButtonDown(0)){ // allow firing with Left Mouse Button
            // place here the cannon shoot code
            // and finish unfreezing the aim
            freeze = false; // unfreeze aim after shooting
        }
    }
    else { // if not freezed...
        // Generate a plane that intersects the transform's position with an upwards normal.
        var playerPlane = new Plane(Vector3.up, transform.position);
        // Generate a ray from the cursor position
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        // Determine the point where the cursor ray intersects the plane.
        // This will be the point that the object must look towards to be looking at the mouse.
        // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
        //   then find the point along that ray that meets that distance.  This will be the point
        //   to look at.
        var hitdist = 0.0;
        // If the ray is parallel to the plane, Raycast will return false.
        if (playerPlane.Raycast (ray, hitdist)) {
            // Get the point along the ray that hits the calculated distance.
            targetPoint = ray.GetPoint(hitdist);
            // Determine the target rotation.  This is the rotation if the transform looks at the target point.
            var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
            // Smoothly rotate towards the target point.
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
            // shorten the ray if it's hitting some obstacle:
            var hit: RaycastHit;
            if (Physics.Linecast(transform.position, targetPoint, hit)){
                targetPoint = hit.point;
            }
        } else {
            targetPoint = transform.position;
        }
    }
    lineRenderer.SetPosition(0, transform.position);
    lineRenderer.SetPosition(1, targetPoint);
}