Home Game Development unity – How can I replace my kinematic object’s rotation primarily based on how a lot it has moved per body?

unity – How can I replace my kinematic object’s rotation primarily based on how a lot it has moved per body?

0
unity – How can I replace my kinematic object’s rotation primarily based on how a lot it has moved per body?

[ad_1]

I’m transferring a kinematic circle (and its little one sprite) horizontally backwards and forwards after a delay utilizing rework.Translate(). I’m questioning how I can discover a appropriate rotation worth for my circle primarily based on the quantity it has moved per body, virtually like what one would count on if she or he was rolling a coin throughout a desk.

I’ve tried utilizing a pre-selected rotationSpeed worth, however this conflicts with the variable velocity produced by utilizing acceleration; and updating rework.eulerAngles.z with the present velocity causes the circle to rotate manner an excessive amount of each body (multiplying the rate by Time.deltaTime causes it to maneuver far too little).

I think there’s a mathematical strategy to derive what I’m searching for primarily based on the quantity moved and the dimensions of the circle, however have no idea the place to start with this.

utilizing UnityEngine;

public class RotatingCircle2D : MonoBehaviour
{
    [SerializeField]
    non-public float moveSpeed = 9f;
    [SerializeField]
    non-public float acceleration = 25f;
    [SerializeField]
    non-public float reverseTime = 3f;

    non-public float velocityX = 0;
    non-public float directionX = 1;

    non-public float timer = 0;

    non-public void Update()
    {
        MoveAndRotate();
    }

    non-public void MoveAndRotate()
    {
        if (timer >= reverseTime)
        {
            directionX *= -1;
            timer = 0;
        }

        // Move
        velocityX = Mathf.MoveIn direction of(velocityX, moveSpeed * directionX, acceleration * Time.deltaTime);
        rework.Translate(Vector2.proper * velocityX * Time.deltaTime, Space.World);

        // Rotate
        float targetRotationZ = rework.eulerAngles.z + velocityX * Time.deltaTime; // This is not proper...
        Quaternion targetRotation = Quaternion.Euler(0, 0, targetRotationZ);
        rework.rotation = targetRotation;

        timer += Time.deltaTime;
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here