- Home /
Player Rotation with the mouse
Hello everyone and thank you for helping me, I'm actually blocked with a simple thing like the player rotation. I followed this tutorial "https://unity3d.com/learn/tutorials/projects/survival-shooter/player-character" and I don't understand why my player won't rotate. Here is the script :
#pragma strict
var speed : float = 6f;
private var playerRigidbody : Rigidbody;
private var movement : Vector3;
private var camRayLength : float = 100f;
private var floorMask : int;
function Awake ()
{
floorMask = LayerMask.GetMask("Floor");
playerRigidbody = GetComponent(Rigidbody);
}
function FixedUpdate ()
{
var h : float = Input.GetAxisRaw("Horizontal");
var v : float = Input.GetAxisRaw("Vertical");
// Move the player
Move(h, v);
//Turning the player
Turning();
}
function Move (h : float, v : float)
{
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition(transform.position + movement);
}
function Turning ()
{
var camRay : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var floorHit : RaycastHit;
if(Physics.Raycast(camRay, floorHit, camRayLength, floorMask))
{
var playerToMouse : Vector3 = floorHit.point - transform.position;
playerToMouse.y = 0f;
var newRotation : Quaternion = Quaternion.LookRotation(playerToMouse);
playerRigidbody.MoveRotation(newRotation);
}
}
Thanks again :)
Comment
Answer by Halfbiscuit · May 21, 2015 at 10:56 AM
Are you sure the ray cast is hitting something? If it doesn't nothing will happen.
Indeed, I have not put "Floor" as a layer of my terrain... Thank you !
Answer by Nikki L · Nov 28, 2015 at 11:24 PM
I had the same problem and I had forgotten to set Floor in Layer. Thanks