0
\$\begingroup\$

My GameObject is able to jump towards a moving target (another GameObject), thus adjusting its trajectory - while simulating gravity (as in the .gif).

.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:

Wanted rotation

What I get:

what I get

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – S. Tarık Çetin Nov 13 '15 at 20:12
  • \$\begingroup\$ Image now available. \$\endgroup\$ – That's me. Nov 13 '15 at 20:13
  • \$\begingroup\$ "don't cooperate very well" is super vague. tell us what is happening \$\endgroup\$ – jhocking Nov 13 '15 at 20:26
  • \$\begingroup\$ Question edited. \$\endgroup\$ – That's me. Nov 13 '15 at 20:29
  • \$\begingroup\$ @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. \$\endgroup\$ – S. Tarık Çetin Nov 13 '15 at 20:36
2
\$\begingroup\$

The code structure you want:

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

A more adjusted version for your script:

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)
        }
    }
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ It doesn't lerp! Same problem. Exactly. \$\endgroup\$ – That's me. Nov 13 '15 at 21:08
  • \$\begingroup\$ @TheGameChanger Take a look at the edit. \$\endgroup\$ – S. Tarık Çetin Nov 13 '15 at 21:14
  • \$\begingroup\$ 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? \$\endgroup\$ – That's me. Nov 13 '15 at 21:23
  • \$\begingroup\$ Just write an example with numbers in your answer. (or a, b, c) \$\endgroup\$ – That's me. Nov 13 '15 at 21:23
  • \$\begingroup\$ @TheGameChanger Revised the edit. \$\endgroup\$ – S. Tarık Çetin Nov 13 '15 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.