简体   繁体   中英

GameObject does not move after transform.position

I have a GameObject that I try to move. For the GameObject I created a new class and this class has a function UpdatePosition(). In this function I update the transform.position of the GameObject and if I log the transform.position of the object, the console writes the newly updated position. However, in the game and scene view in Unity the object does not move. I call the function every tick via an Update function and this works since the Debug.Log outputs to the console every tick. Does anyone know what is going wrong here?

public class ActiveObjects
{
    [SerializeField]
    public GameObject theMovingObject;
    [SerializeField]
    public Transform startPosition;
    [SerializeField]
    public Transform endPosition;

    [NonSerialized]
    private float speed = 10.0f;
    [NonSerialized]
    public float startTime;
    [NonSerialized]
    public float journeyLength;

    // Sets new position of theMovingObject
    public void UpdatePosition()
    {
        float step = speed * Time.deltaTime;
        theMovingObject.transform.position =    Vector3.MoveTowards(theMovingObject.transform.position, endPosition.position, step);
        Debug.log(theMovingObject.transform.position);
    }

}

The output of in the console is the position which theMovingObject should have, but does not have in the game/scene view.

I did this:

Vector3 newPosition = new Vector3();
newPosition.x = *;
newPosition.y = *;
newPosition.z = *;
transform.position = newPosition;

And if you want Decimal numbers, do this:

newPosition.x = 0.5f;

*Any number

Just to explain:

Vector2 = X, Y

Vector3 = X,Y,Z

Get it? If it doesn't work, just write a comment at this answer. Greetings, Tjovo studios.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM