4
I’m doing a tutorial on Unity that I downloaded in the Asset Store on the following link https://www.assetstore.unity3d.com/en/#! /content/21028
I tried to make my character rotate to look towards the mouse, but he can’t rotate, and only looks forward in the case and I can’t make him look at the other places, such as the right corner, left, behind the character, only in a straight line!
Use the function Turning()
to take care of the 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);
}
}
Inspector do Player - http://i.imgur.com/p6BgvJw.png
Floor Inspector - http://i.imgur.com/Pgwlkxt.png
@Derpplayss, is the camera working right? Changing the Floor solved everything? Is there any more problem?
– Nils
@Nils Feel free to post an answer now :) Outstanding its initiative to encourage its members to come here and ask questions. Thank you for sharing the site with them.
– stderr
@Qmechanic73 Thanks to stackoverflow and its community, I learned a lot coming here and the structure to solve doubts collectively is sensational. And a few weeks ago I saw that now you can doubt almost everything(has area for everything).
– Nils