Help With transform.Position

Hello I am trying to produce a temple run like game and I have run into a problem with my side movement, been doing some research on how to use transform.Position to move my player object left & right to my target coordinates over time, I thought everything was going fine then I hit a brick wall… the code just does not work & unity does not give me any errors, I have searched online for answers but I cannot find any solutions, I know I’ve missed something but i’m either too stupid or slow to realize, here is my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour {
    public float RUNSPEED = 0.05f;
    public float SIDESPEED = 0.1f;
    bool MOVEDLEFT;
    bool MOVEDCENTER;
    bool MOVEDRIGHT;

 

    // Use this for initialization
    void Start () {

    }
 
    // Update is called once per frame
    void Update () {

        transform.Translate (Vector3.forward * RUNSPEED);

if (Input.GetKey ("left")) {
         
            Vector3 TargetPosition = new Vector3 (-1, 1, transform.position.z);
         
            transform.position = Vector3.MoveTowards (transform.position, TargetPosition, SIDESPEED * Time.deltaTime);
             
        }

        if (Input.GetKey("right")) {

            transform.position = new Vector3 (1, 1, transform.position.z);

        }
    }
}

The problem is:

if (Input.GetKey ("left")) {
         
            Vector3 TargetPosition = new Vector3 (-1, 1, transform.position.z);
         
            transform.position = Vector3.MoveTowards (transform.position, TargetPosition, SIDESPEED * Time.deltaTime);
             
        }

I get no errors with unity what so ever but it just fails to work, if anyone could help me with this or point me in the right direction I would be greatly appreciated.

Please use code tags when posting code.

Thankyou buddy! learn something new everyday. =)

Is it possible it’s just moving too slowly for you to see? I notice you’re multiplying SIDESPEED by Time.deltaTime, whereas you don’t earlier multiply RUNSPEED by deltaTime. If using deltaTime (and you should!), then those numbers will be in “units per second”, and 0.1 unit per second may be extremely slow.

1 Like

Buddy you are a blessing in disguise! =L extremely slow it is, two days I have been scratching my noggin over this, can’t believe I couldn’t see it myself, you my friend are a legend and I thank you for your help! =)

Haha. The most annoying bugs are the simplest. :smile:

1 Like