- Home /
Change forward control on mouse rotation
Hello there,
I'm currently developing a character controller. I have a camera that smooths follow the player, and a script to control that player, using a character controller.
I've added that when rotating the mouse, player face always look at the pointer. In what I have, "W" is always "Camera.Forward" but now I have some trouble telling the script that "W" is always where the face of the player is looking, and "A" "D" and "S" are relative.
I'm attaching the character code and some screen-shots. Any help is welcome.
void Start ()
{
charController = GetComponent <CharacterController>();
vertSpeed = minFall;
camera = Camera.main;
}
void Update ()
{
Vector3 movement = Vector3.zero;
float horInput = Input.GetAxis("Horizontal");
float vertInput = Input.GetAxis ("Vertical");
if (horInput !=0 || vertInput !=0)
{
if(Input.GetKey(KeyCode.LeftShift))
{
movement.x = horInput * runSpeed;
movement.z = vertInput * runSpeed;
movement = Vector3.ClampMagnitude(movement, runSpeed);
}
else
{
movement.x = horInput * moveSpeed;
movement.z = vertInput * moveSpeed;
movement = Vector3.ClampMagnitude(movement, moveSpeed);
}
Quaternion tmp = target.rotation;
target.eulerAngles = new Vector3 (0, target.eulerAngles.y, 0);
movement = target.TransformDirection (movement);
target.rotation = tmp;
Quaternion direction = Quaternion.LookRotation(movement);
transform.rotation = Quaternion.Lerp(transform.rotation, direction, rotSpeed * Time.deltaTime);
}
movement.y = vertSpeed;
movement *= Time.deltaTime;
charController.Move(movement);
FixedControlMouse();
}
void FixedControlMouse ()
{
Ray ray = camera.ScreenPointToRay (Input.mousePosition);
Plane groundPlane = new Plane (Vector3.up, Vector3.zero);
float rayDistance;
if (groundPlane.Raycast(ray, out rayDistance))
{
Vector3 point = ray.GetPoint(rayDistance);
Debug.DrawLine(ray.origin, point, Color.green);
LookAt(point);
}
}
void LookAt (Vector3 lookPoint)
{
Vector3 hcPoint = new Vector3(lookPoint.x, transform.position.y, lookPoint.z);
transform.LookAt(hcPoint);
}
I'm thinking about some "transform.forward" but it's not working.
So, according to this: link text https://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html I need to create some kinda function that checks if the character is facing the "X axis", to move "W" in the "Y axis", and the opposite if the character is facing "Y axis"?
I've seen that rotating and moving a rigidbody it's much easier, but the project requires a CharacterController.
Your answer
Follow this Question
Related Questions
Moving forward towards the cursor with LookAtMouse? 0 Answers
Character controller Joystick issues. 1 Answer
How to rotate a Character Controller on the X or Z axis ? (Not for movement) 0 Answers
move character controller forward using gui button. 1 Answer
Precise rotation based on joystick axis input for twin-stick controls 3 Answers