- Home /
Moving a sprite
There is a sprite object that I would like to move from its initial position to a new position. Here is something that I tried but it is not moving from its initial position.
public Vector3 target; public float speed; private Vector3 position; // Use this for initialization void Start () { target = new Vector3 (5, 5, 0); position = gameObject.transform.position; float speed = 1.0f; } // Update is called once per frame void Update () { float step = speed * Time.deltaTime; transform.position = Vector3.MoveTowards(transform.position, target, step); }
Some directions would help. Thanks!
Are you sure the script is running ? Have you tried to add a Debug.Log inside the update function ?
@Hellium Just tried adding the Debug.Log to print strings. And yes its printing them continuously!
Answer by Hellium · Jun 19, 2016 at 05:18 PM
When declaring the speed float as a attribute of your class, the default value is 0
In the Start function you declare a new speed
variable, you do not set the value of the class' attribute.
In the Update function, speed
is still equal to 0, so does step
, thus, your object is not moving.
Change float speed = 1.0f
for speed = 1.0f
in the Start function