7
I’m learning how to play a game, but even if I follow it step by step, I think I did something wrong. It moves correctly but just keeps looking forward and does not turn, I do not know if it is problem in Turning of the character. This is the script:
public class PlayerMovement : MonoBehaviour
{
//Velocidade do Jogador
public float speed = 6f;
//Vetor responsavel pelo movimento
Vector3 movement;
//Responsavel pela transicao da animacao
Animator anim;
//Responsavel pela fisica do objeto
Rigidbody playerRigdbody;
//Mascara de chao
int floorMask;
//Inf para raycast
float camRayLength = 100f;
void Awake()
{
//Atribuir a mascara da camada
floorMask = LayerMask.GetMask ("floor");
//Atribuir as referencias
anim = GetComponent <Animator> ();
playerRigdbody = GetComponent <Rigidbody> ();
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Move (h, v);
Turning ();
Animating(h,v);
}
//movimento
void Move ( float h, float v)
{
//Determina o movimneto
movement.Set (h, 0f, v);
//Normaliza o movimento
movement = movement.normalized * speed * Time.deltaTime;
//efetua o movimento no personagem
playerRigdbody.MovePosition (transform.position + movement);
}
//girar
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast (camRay,out floorHit,camRayLength,floorMask))
{
Vector3 playerToMouse = floorHit.point-transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
playerRigdbody.MoveRotation (newRotation);
}
}
void Animating (float h,float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool ("IsWalking", walking);
}
}
Well-observed. :)
– Luiz Vieira