- Home /
Global movement
Hello! i have a script that moves my character relative to the camera, it works well, as it works while the camera rotates, but my character is always facing a single direction, so a part of the code changes his rotation, and the problem starts with this, and i think it's caused because the script moves my character in local coordinates, so when the character rotates it changes the way it moves in a weird way. So my cuestion is, How do i move my character independetnly of it's rotation using global coordinates?
This is the script
var dir: Vector3;
var direction: Vector3;
var speed : float = 2.0;
var gravity : float = 30.0;
var vertical :Vector3;
var horizontal:Vector3;
var total: Vector3;
private var moveDirection : Vector3 = Vector3.zero;
function Start () {
}
function Update () {
var cam : Transform = Camera.main.transform;
var controller : CharacterController = GetComponent(CharacterController);
var cameraRight : Vector3 = cam.TransformDirection (1, 0, 0);
var cameraFord : Vector3=cam.TransformDirection(0,0,1);
vertical = cameraFord* Input.GetAxis("vertical");
horizontal = cameraRight* Input.GetAxis("horizontal");
total = vertical + horizontal;
moveDirection = total*0.5;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
//transform.rotation = Quaternion.LookRotation(total);
moveDirection.y -= gravity * Time.deltaTime*5;
controller.Move(moveDirection * Time.deltaTime);
}
The line which changes the rotation is the one in a comment, and the script works well while this line is in a comment but my character is always facing to the same direction.
Any help would be appreciated. Thanks in advance!!
To answer your specific question, line 33 takes the local coordinates calculate and translates them to global. So if you comment out line 33, it will treat the coordinates as global coordinates. I'm not sure that is what you want to do to solve your problem. Or at least it is likely only part of the solution.
Your answer
Follow this Question
Related Questions
Camera Relative Movement 2 Answers
Relative Movement Problem 1 Answer
Calculate vector exactly between 2 vectors 4 Answers
Problem with camera position 3 Answers
In-Air movement relative to camera 1 Answer