Question by
behnder · Nov 13, 2021 at 11:36 PM ·
charactercontrollercharacter controllercharacter movementfirst-person-controller
I can't rotate and move in the opposite direction with character Controller
Hello all! I have a problem rotating a object with character controller. I have a object with the follow script. If I use raycast and i hit a wall, then the obect face in the opposite direction, due de Rotate, but when i do this my position still relative to the world, so, i turn 180º, but with the same direction. I mean... im "walking" backwards.
using UnityEngine;
public class RayCastForward : MonoBehaviour
{
bool hit;
int speed = 5;
CharacterController characterController;
private void Awake()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
hit = Physics.Raycast(transform.position, transform.forward,2);
if (hit)
{
Debug.Log("HIT!!!!!!!!!");
characterController.transform.Rotate(0, 180, 0);
}
characterController.Move(new Vector3(0, 0, speed * Time.deltaTime));
}
}
Thanks in advance!
Comment
Your answer
Follow this Question
Related Questions
Character moves opposite ways.,My character moves perfectly to every direction except -z. 0 Answers
Character Controller move overrides transform.position 0 Answers
Character Controller - Limit controller speed 0 Answers
Character controller for fixed perspective camera (moving relative to arbitrary point on screen) 0 Answers
Error CS0103 0 Answers