How to reset a movement wich use the Input.GetAxis function?

Here is the basic getAxis function + an if click de mouse button 0.

Goal: add a button wich make the object go back to the starting point. then we can turn around again from this starting point.

My problem: when I click again to move, it go back to the last point before the click to the home button.

Scripts : the first one is to go back to the starting point, the second one is to turn around

            if (home == true)
            {
                desiredRotation = Quaternion.Euler(37, 142, 0);
                currentRotation = Quaternion.Euler(37, 142, 0);

               
                //transform.rotation = Quaternion.Euler(37, 142, 0);
                
                //rotation = Quaternion.Euler(37, 142, 0);
                
            }
            if (Input.GetMouseButton(0))
            {


                //Set the AmtToRotate
                xDeg += Input.GetAxis("Mouse X") * xSpeed * SpeedCoef;
                yDeg -= Input.GetAxis("Mouse Y") * ySpeed * SpeedCoef;

                // Make sure that the yDeg is still in the Y limits
                yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
                
                //Use the AmtToRotate to rotate the camera
                desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
                currentRotation = transform.rotation;
                //Add a damping
                rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
                transform.rotation = rotation;
                
            }

Thank’s for your help !

BooBi

up!!

The first script excerpt doesn’t really do anything, since the code to apply the rotation reset is commented out. As written (and assuming there is no other code that you haven’t listed), I would expect clicking your ‘home’ button to have no effect at all. To fix it, un-comment the ‘transform.rotation = …’ line in the first script section.

Nope, in fact the line in comment doesn’t work too. (I let them as a comment to show that i’ve tried them, that wasn’t clear at all sorry).

The problem is that doesn’t reset anything it just put the camera on that quaternion, then if I click again to move it starts the new movement from the old quaternion…

I don’t see how, since you set transform.rotation to the result of Slerp’ing from the current transform.rotation… Are you sure you don’t have any code you haven’t shown that, in between the two fragments you did show, is setting transform.rotation?

            //--------------Rotation--------------------

            //Check if the mouse button is on

That’s what inbetween the codes

and that’s at the end, it’s finish the slerp rotation when in release the mouse

                   if (!Input.GetMouseButton(0)) 
            {
                currentRotation = transform.rotation;
                rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
                transform.rotation = rotation;
            }

I tried to put the Home part at the end and it doesn’t change anything…

It’s hard to get a clear picture of what the script looks like split into bits like this; try posting the complete script, as it currently exists.

void Start()
    {

        //-------------Set initial Rotation--------------------

        rotation = transform.rotation;

        currentRotation = transform.rotation;
        desiredRotation = transform.rotation;
        //desiredRotation = Quaternion.Euler(37, 142, 0);
        xDeg = Vector3.Angle(Vector3.right, transform.right);
        yDeg = Vector3.Angle(Vector3.up, transform.up);


        //-----------Set initial Position----------------------

        distance = Vector3.Distance(transform.position, target.position);
        currentDistance = distance;
        desiredDistance = distance;

        position = transform.position;


    }
    
    
    //-------------------------------   UPDATE   -------------------------------

    // Update is called once per frame
    void Update()
    {

        if (interrupteur == true)
        {
            //-------Home-------

            if (home == true)
            {
                desiredRotation = Quaternion.Euler(37, 142, 0);

                xDeg = Vector3.Angle(Vector3.right, transform.right);
                yDeg = Vector3.Angle(Vector3.up, transform.up);
                transform.rotation = Quaternion.Euler(37, 142, 0);
                
                //rotation = Quaternion.Euler(37, 142, 0);
                print("up");
            }

            //--------------Rotation--------------------

            //Check if the mouse button is on 

            if (Input.GetMouseButton(0))
            {


                //Set the AmtToRotate
                xDeg += Input.GetAxis("Mouse X") * xSpeed * SpeedCoef;
                yDeg -= Input.GetAxis("Mouse Y") * ySpeed * SpeedCoef;

                // Make sure that the yDeg is still in the Y limits
                yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
                
                //Use the AmtToRotate to rotate the camera
                desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
                currentRotation = transform.rotation;
                //Add a damping
                rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
                transform.rotation = rotation;
                
            }
            if (!Input.GetMouseButton(0)) 
            {
                currentRotation = transform.rotation;
                rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
                transform.rotation = rotation;
            }

up

Up Big time !! :wink:

up

up

Not precisely sure what you’re trying to do here, though after looking at the code… Why is there no DesiredRotation set in the
“if (!Input.GetMouseButton(0))” section of code? Also, for performance and accuracy you might want to move your actual rotation transforms into LateUpdate instead.

Right now your code says to me: check for interupteur and if true, then check for home and if its true as well, check for the Mouse input, and process the code based on whether its been pressed. The lack of a declared desiredRotation in the 2nd part could be causing an issue there but again as laurie mentioned, w/o seeing the whole code its hard to tell.

hi again,

You’ve got here the whole code except the variables declarations.

the part with “if (!Input.GetMouseButton(0))” is used to finish the rotation with a nice deccelaration when you released the mouse button

in my gui script, when I click on the home button it turns on the home bool then off if the button isn’t clicked.

So I don’t know if someone has another idea to do that ?

I need to keep the"if (!Input.GetMouseButton(0))" part.

I tried without that part and it doesn’t change anything.

So basically what I need to do:

My rotateCam script makes my camera rotates around a target, with a nice lerp to have acceleration and decceleration.

When you start playing with in and stop to any position, if you click on the home button I need to go back to the starting position exactly like if I restart the “game”.

here is the whole complete code again:

using UnityEngine;
using System.Collections;

public class RotateCam : MonoBehaviour
{
    //--------------------------------------------------
    // Script By Baghdikian Boris b.baghdikian@gmail.com
    //--------------------------------------------------


    //-------------------------------Déclaration des Variables-----------------------------


    //-------------Rotation Variables---------------------

    //Rotation Speed
    public float xSpeed = 200.0f;
    public float ySpeed = 200.0f;
    public float SpeedCoef = 0.02f;

    // Y Axis Limits
    public float yMinLimit = -80.0f;
    public float yMaxLimit = 80.0f;

    //Damping
    public float RotDamp = 5.0f;

    //AmtToRotate in degrees
    public float xDeg = 0.0f;
    private float yDeg = 0.0f;

    //Rotation from current to desired
    private Quaternion currentRotation;
    public Quaternion desiredRotation;
    public Quaternion rotation;


    //---------Position Variables--------------------------

    // Target
    public Transform target;
    public Vector3 targetOffset;

    // Distances and positions
    public float distance = 5.0f;
    public float maxDistance = 20.0f;
    public float minDistance = 0.6f;
    private float currentDistance;
    public static float desiredDistance;
    private Vector3 position;

    //Zoom parameters
    public int zoomRate = 150;
    public int zoomRateRMB = 550;
    public float zoomDamp = 5.0f;

    //interrupteur GUI
    public static bool interrupteur = true;

    //Sensibility Zooms
    public int RClicSensibility;
    public int MidSensibility;


    public Transform thisTransform;
    //public Vector3 startPos;
    //public Vector3 endPos;
    public float time;
    public float i;
    public float rate;

    public static bool home;



    //-------------------------------   START   -------------------------------


    // Use this for initialization
    void Start()
    {

        //-------------Set initial Rotation--------------------

        rotation = transform.rotation;

        currentRotation = transform.rotation;
        desiredRotation = transform.rotation;
        //desiredRotation = Quaternion.Euler(37, 142, 0);
        xDeg = Vector3.Angle(Vector3.right, transform.right);
        yDeg = Vector3.Angle(Vector3.up, transform.up);


        //-----------Set initial Position----------------------

        distance = Vector3.Distance(transform.position, target.position);
        currentDistance = distance;
        desiredDistance = distance;

        position = transform.position;


    }
    
    
    //-------------------------------   UPDATE   -------------------------------

    // Update is called once per frame
    void Update()
    {

        if (interrupteur == true)
        {
            //-------Home-------

            if (home == true)
            {
                desiredRotation = Quaternion.Euler(37, 142, 0);

                xDeg = Vector3.Angle(Vector3.right, transform.right);
                yDeg = Vector3.Angle(Vector3.up, transform.up);
                transform.rotation = Quaternion.Euler(37, 142, 0);
                
                //rotation = Quaternion.Euler(37, 142, 0);
                print("up");
            }

            //--------------Rotation--------------------

            //Check if the mouse button is on 

            if (Input.GetMouseButton(0))
            {


                //Set the AmtToRotate
                xDeg += Input.GetAxis("Mouse X") * xSpeed * SpeedCoef;
                yDeg -= Input.GetAxis("Mouse Y") * ySpeed * SpeedCoef;

                // Make sure that the yDeg is still in the Y limits
                yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
                
                //Use the AmtToRotate to rotate the camera
                desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
                currentRotation = transform.rotation;
                //Add a damping
                rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
                transform.rotation = rotation;
                
            }
            if (!Input.GetMouseButton(0)) 
            {
                currentRotation = transform.rotation;
                rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
                transform.rotation = rotation;
            }

            




            //---------------Position------------------

            //Set the AmtToMove
            desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance) * MidSensibility;

            if (Input.GetMouseButton(1))
            {
                desiredDistance -= Input.GetAxis("Mouse Y") * RClicSensibility;
            }
            //Clamp the zoom min/max
            desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);

            //Add the damping
            currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDamp);

            // New position
            position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
            transform.position = position;


        }

    }

    private static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }





}

up