How to rotate the character as the mouse moves?

7

I'm learning how to play a game but by following step by step I think I did something wrong. It moves correctly but just keeps looking forward and does not spin, I do not know if it's a problem in the character's turning. The script is this:

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); 
    }
}
    
asked by anonymous 08.02.2015 / 16:04

1 answer

6

I think I know the problem, in:

void Awake()
{
    //Atribuir a mascara da camada
    floorMask = LayerMask.GetMask ("floor");

    //Atribuir as referencias
    anim = GetComponent <Animator> ();
    playerRigdbody = GetComponent <Rigidbody> ();

}

switch

floorMask = LayerMask.GetMask ("floor");

by

floorMask = LayerMask.GetMask ("Floor");

Unity is case-sensitive.

Raycast looks for "floor", when in fact the layer is as Floor "

Therestofthecodeseemstobeallright

Bytheway,Icouldnotcommentontheanswerabove,butitisgoodtowarnyouwhereyouarefollowingthetutorial,whichevenLuisVieiradidnotsuggest.

link - > It is from this aquil tutorial

    
08.02.2015 / 21:57