# Unable to rotate GameObject properly?

My GameObject is able to jump towards a moving target (another GameObject), thus adjusting its trajectory - while simulating gravity (as in the .gif). I want to make the "jumper object" rotate. But I am unable to rotate my GameObject properly. When the GameObject jumps, it doesn't complete its rotation and rotates abruptely. How can I fix this? How can I properly rotate the cube GameObject you see in the .gif? Any fixes are appreciated.

Wanted rotation: What I get: • Image is not available for me. But i guess GetMouseButtonUp couses problems. Try GetMouseButton instead. So your object will turn constantly while you press button, not only when you relase it. Nov 13, 2015 at 20:12
• Image now available. Nov 13, 2015 at 20:13
• "don't cooperate very well" is super vague. tell us what is happening Nov 13, 2015 at 20:26
• Question edited. Nov 13, 2015 at 20:29
• @TheGameChanger I guess what you want to do is making cube to look forward vector of it's current position on trajectory right? As far as i can see, you already calculate velocity vector in your jumpAndFollow method. Then you can simply use this: transform.LookAt(transform.position + velocityVector.normalized); This will make your gameObject's +z axis to line on the vector you give in paranthesis. Nov 13, 2015 at 20:36

The code structure you want:

transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, lerpFactor);


Change the line

transform.Rotate (new Vector3 (a, b, c) * Time.deltaTime);


with

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(a, b, c), Time.deltaTime);


Also,

        void Update()
{
if(Input.GetMouseButtonUp(0))
{
//this is only called when button gets relased.
}

if (Input.GetMouseButton (0))
{
//this is called constantly if button is holding down.
}
}


Edit:

This code piece should do what you want:

    Quaternion targetRot = Quaternion.identity; //try not to modify this directly, or do it, it's up to you.
bool doRotate = false;
float lerpLimit = 10; //the degrees you want lerp to stop at. set higher than 0 (i suggest higher than 1)

public void SetRotationTarget(Vector3 eulerAngles) //call this method to update the rotation target.
{
targetRot = Quaternion.Euler (eulerAngles); //building a quaternion from given vector3
doRotate = true;
}

void Update()
{
if (doRotate)
{
if (Quaternion.Angle(transform.rotation, targetRot) > lerpLimit) //if angle between our rotation and target rotation is not below limit
{
transform.rotation = Quaternion.Slerp (transform.rotation, targetRot, Time.deltaTime); //Slerp to target rotation
}
else
{
transform.rotation = targetRot;
doRotate = false;
}
}

if(Input.GetMouseButtonUp(0)) //when mouse button 0 is relased
{
SetRotationTarget(new Vector3(a, b, c)); //set new rotation target with values of (a, b, c)
}
}

• It doesn't lerp! Same problem. Exactly. Nov 13, 2015 at 21:08
• @TheGameChanger Take a look at the edit. Nov 13, 2015 at 21:14
• Let's say I have transform.Rotate (new Vector3 (0, 0, 180) * Time.deltaTime);  How do I convert it with your most recent edit? Where do I put the numbers? Nov 13, 2015 at 21:23
• Just write an example with numbers in your answer. (or a, b, c) Nov 13, 2015 at 21:23
• @TheGameChanger Revised the edit. Nov 13, 2015 at 21:38