- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
MuseumTourGames · Aug 23, 2013 at 07:57 PM ·
c#rotationmouse
Character Not Rotating
When I use this code on my player script:
private void joystickMovementUpdate() {
direction = new Vector3(Input.GetAxis ("Right Joystick Horizontal"), Input.GetAxis ("Right Joystick Vertical"), 0);
horizontalAxis = Input.GetAxis ("Left Joystick Horizontal");
verticalAxis = Input.GetAxis ("Left Joystick Vertical");
if (direction.sqrMagnitude > 0.1f) {
qTo = Quaternion.LookRotation(direction);
qTo.eulerAngles = new Vector3(0, 180, qTo.eulerAngles.z);
}
transform.eulerAngles = new Vector3(0, 180, transform.eulerAngles.z);
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, ROTATION_SPEED * Time.smoothDeltaTime);
move ();
}
private void keyboardMovementUpdate() {
horizontalAxis = Input.GetAxis ("Keyboard Horizontal");
verticalAxis = Input.GetAxis ("Keyboard Vertical");
transform.LookAt (Camera.main.ScreenToWorldPoint (Input.mousePosition));
transform.eulerAngles = new Vector3(0, 180, transform.eulerAngles.z);
move ();
}
He does not rotate properly to face the mouse/joystick. Instead he remains completely still.. What is wrong with this snippet?
He's on the XY plane.
Comment
So this is a top-down game, like war craft, and you want your character to face the direction in which your cursor is?
Yes sir. And in the joystick, I want the right one to rotate the player. This should work..possibly something external is making it dysfunctional.
Try something like this in line 21 ins$$anonymous$$d:
Vector3 targetPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.LookAt (targetPoint.x, transform.y, targetPoint.z);
Basically, you want your object to look at the cursor in the x and z direction, but not "up" and "down".