How to rotate the character as the mouse moves?

Asked

Viewed 3,240 times

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); 
    }
}

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> ();

}

change

floorMask = LayerMask.GetMask ("floor");

for

floorMask = LayerMask.GetMask ("Floor");

Unity is case sensitive.

Raycast looks for "floor", when actually the layer is like Floor"

Lista de Layers

The rest of the code seems to be all right

By the way, I could not comment on the answer above, but it is good to warn of where you are following the tutorial, that even Luis Vieira suggested.

https://www.assetstore.unity3d.com/en/#! /content/21028 -> It’s from this aquil tutorial

  • Well-observed. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.