One more thing(#)

Ok hello again. It is the same script as before. . .
Now the problem I am having is that the object that is moving keeps tilting, and sometimes gets flung.

Code:

using UnityEngine;
public class MovingScriptTest : MonoBehaviour
{
    public Vector3 startPosition;

    bool up = true;
    void Start()
    {
       
        startPosition = transform.position;
    }
    // Update is called once per frame
    void Update()
    {
        MoveVertical();
    }
    void MoveVertical()
    {
        var temp = transform.position;
        print(up);
        if (up == true)
        {
            temp.y += (40f * Time.deltaTime);
            transform.position = temp;
            if (transform.position.y >= 1f)
            {
                up = false;
            }

        }
       
        if (up == false)
        {
            temp.y -= (1f * Time.deltaTime);
            transform.position = temp;
            if (transform.position.y <= 1f)
            {
                up = true;

            }
        }
       
    }
}
1 Like

I don’t know what “before” is, but there is nothing in your code that would cause the cube to rotate like that. I don’t think the problem is related to the code that you posted. Do you have a rigidbody attached to that object? Is it set to kinematic?

1 Like