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.