I'm doing a Unity tutorial that I downloaded to the Asset Store at the following link link
I tried to make my character turn to look at the mouse, but it can not rotate, and just look forward to the case and I can not make it look at other places, like for example the right, left, back of the character, only in a straight line!
I use the Turning()
function to take care of rotation.
public class PlayerMovement : MonoBehaviour {
public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRayLenght = 100f;
void Awake(){
floorMask = LayerMask.GetMask ("Floor");
anim = GetComponent<Animator> ();
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate(){
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Move (h, v);
Turning ();
Animating (h, v);
}
void Move(float h,float v){
//tertemina o movimento
movement.Set(h,0f,v);
//normaliza o movimento
movement = movement.normalized * speed * Time.deltaTime;
// efetua o movimento
playerRigidbody.MovePosition (transform.position + movement);
}
//girar o jogador
void Turning(){
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit floorHit;
if (Physics.Raycast (camRay, out floorHit, camRayLenght, floorMask)) {
Vector3 playerToMouse=floorHit.point-transform.position;
playerToMouse.y=0f;
// rotaçao do personagem
Quaternion newRotation= Quaternion.LookRotation(playerToMouse);
playerRigidbody.MoveRotation(newRotation);
}
}
void Animating(float h,float v){
bool walking = h != 0f || v!=0f;
anim.SetBool ("IsWalking", walking);
}
}
Player Inspector - link
FloorInspector- link