- Home /
[C#]CharacterController Turning
Hello! I have a player with character controller and i set it to move forward all the time,now i want when player triggers something to change the direction to right(i know how to do the trigger part i just dont know how to do the direction part). It's an endless runner game. Please help i can't find any good videos for character controller turning. Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : MonoBehaviour {
private CharacterController controller;
private float speed = 5.0f;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
controller.Move(Vector3.forward * Time.deltaTime * speed);
}
}
Answer by Casiell · Aug 23, 2018 at 04:16 PM
Vector3.forward
in
controller.Move(Vector3.forward Time.deltaTime speed);
line is the direction. Just change it to Vector3.right when you want and all will be good.
Example code:
private Vector3 direction = Vector3.forward;
void Update()
{
if (Input.GetKeyDown("space"))
direction = Vector3.right;
controller.Move(direction * Time.deltaTime * speed);
}
This changes the direction to right when user presses space bar
Answer by Moonlads · Aug 23, 2018 at 04:32 PM
Thank you so much, if i earn loads of money of this game (my fourth game) i'm giving you some. It worked thank you so much again.
Hello! I need more help. It turns the first time but when i type for example
direction = Vector3,left
it doesn't turn I want it to turn multiple times Please Help,
Your answer
Follow this Question
Related Questions
[C#] Movement Direction Change 2 Answers
Changing the landing and switch directions of standard assets TPC controller 0 Answers
Change Direction[C#] 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
[C#] Jump on slopes 1 Answer