I’m doing the game Nightmares from Unity and in the playerMovement script my character just doesn’t rotate looking at the mouse

Asked

Viewed 74 times

1

This is my code, I thank you in advance for your help.

using UnityEngine;

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 playerRigidbody;

    //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>();
        playerRigidbody = GetComponent<Rigidbody>();

    }

    void FixedUpdate() //movimentacao do jogador
    {

        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        Move(h, v);
        Turning();
        Animating(h, v);

    }

    void Move(float h, float v)
    {
        //determina o movimento
        movement.Set(h, 0f, v);

        //normaliza o movimento
        movement = movement.normalized * speed * Time.deltaTime;

        //efetua o movimento no personagem
        playerRigidbody.MovePosition(transform.position + movement);
    }

    void Turning() //raio da camera que cria um ponto no floorquad a partir do ponteiro do mouse
    {
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit floorHit;

        if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))//sua camray, retorno, raio da camray, mascara do chao 
        {

            Vector3 playerToMouse = floorHit.point - transform.position;
            playerToMouse.y = 0f;

            Quaternion newRotation = Quaternion.LookRotation(playerToMouse); //rotacao do player pro mouse
            playerRigidbody.MoveRotation(newRotation);

        }

    }

    void Animating(float h, float v)
    {
        bool walking = h != 0f || v != 0f; //checando se o player se movimenta ou nao
        anim.SetBool("IsWalking", walking);


    }
}
  • I believe that you could test what is working, if the capture of the mouse position is picking up, also a good solution would be to use Transform. Rotate(0,-1.2f,0) It rotates the object, I use it, so it might work for you too.

  • Everything is working except the mouse capture on the screen, the character should look at the cursor

No answers

Browser other questions tagged

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