- Home /
[C#] Movement Direction Change
Hello! I'm making an endless runner game, and it is made for the character to change directions of movement multiple times. And i got it to work to change the direction of the movement once but it doesn't want to change multiple times. Please help me and be specific (write the code) Here is the movement code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller1;
private float speed = 5.0f;
private Vector3 direction = Vector3.right;
// Use this for initialization
void Start()
{
controller1 = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
controller1.Move(direction * Time.deltaTime * speed);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Turn") ;
{
transform.rotation = Quaternion.Euler(0, 0, 0);
direction = Vector3.zero;
}
if (other.gameObject.tag == "Turn1") ;
{
transform.rotation = Quaternion.Euler(0,90, 0);
direction = Vector3.zero;
Debug.Log("z");
}
if (other.gameObject.tag == "Turn2") ;
{
transform.rotation = Quaternion.Euler(0, 180, 0);
direction = Vector3.left;
}
if (other.gameObject.tag == "Turn3") ;
{
transform.rotation = Quaternion.Euler(0, 270, 0);
direction = Vector3.forward;
}
}
}
It's actually part of it. Please help me i can't figure this out. Thanks in advance.
@HypeerX - I didn't even read the code further, but there's one error that prevents the whole script from running in Editor - In your update method, second line contains "}" character... which shouldn't be there.
Is Vector3.zero even a valid direction? You are essentially telling your character to stop.
Also there is a superflous '}' in Update()
Also make sure your Triggers are actually triggers and have tags you want them to have
Answer by Moonlads · Aug 24, 2018 at 11:41 AM
@Casiell The error in the code is a copy paste mistake everything works in unity i set the tag as it should be and it turns the other way one time when it gets to the second trigger it doesn’t move i put a debug.log to check if the error isn’t in the code and it isn’t.
Answer by Ermiq · Aug 24, 2018 at 05:00 PM
You tell your player to stop when he reaches "Turn"
and "Turn1"
. It's no surprise he stops there.direction
which is equal to Vector3.zero
multiplied by Time.deltaTime
and multiplied by speed
will be the same Vector3.zero
which means that you tell your player to move 0 units left, 0 units right, 0 units up...
It doesn't matter what rotation you set, controller will move in the direction you set, not the rotation. Rotation could only rotate your character visuals, but actual moving is made by controller1.Move (direction);
. Vector3.zero
is a zero, when you multiply zero by any numbers it still will be zero. When you set direction to move controller to Vector3.zero
controller will be moving in the direction zero with zero speed, so it won't move actually.
$$anonymous$$mhm the zero part is actually a testing line of code. I change it from right to zero just to see if it works just like is said for testing purpsoes i got the direction part working but i have problems with trigerring for exanple i set it to change direction when it hits “turn” tagged object and for some reason it calls the function below it to change to left when it hits “turn2”