Character does not follow mouse arrow. UNITY C#

Asked

Viewed 39 times

0

I am creating a game in Unity and I made a code in the point() function for my character to point the gun in the direction of my cursor always. But it’s not happening.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovimentacaoJogador : MonoBehaviour
{
    public float velocidade = 6f;
    Vector3 movimento;
    Animator anim;
    Rigidbody rbJogador;
    int pisoMask;
    float camLongitude = 100f;

    void Awake() //iniciar antes que o jogo comece
    {
        anim = GetComponent<Animator>();
        rbJogador = GetComponent<Rigidbody>();
        pisoMask = LayerMask.GetMask("Piso");
    }

     void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        Mov(h,v);
        Apontar();
        Animar(h,v);
    }

    void Mov(float h, float v)
    {
        movimento.Set(h, 0f, v);
        movimento = movimento.normalized * velocidade * Time.deltaTime;
        rbJogador.MovePosition(transform.position + movimento);
    }

    void Apontar()
    {
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit PisoHit;
        if(Physics.Raycast(camRay, out PisoHit, camLongitude, pisoMask))
        {
            Vector3 jogadorMouse = PisoHit.point - transform.position;
            jogadorMouse.y = 0f;
            Quaternion novaRotation = Quaternion.LookRotation(jogadorMouse);
            rbJogador.MoveRotation(novaRotation);
        }
    }

    void Animar(float h, float v)
    {
        bool parado = ((v == 0) && (h == 0));
        anim.SetBool("EstaAndando", !parado);

    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
  • Please put more details in the question, a print of what your scene is like, and what’s going on, if it’s absolutely nothing or some weird behavior. It’s important to be very specific to get help. I don’t understand why you Zera the y, for example.

No answers

Browser other questions tagged

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