Camera does not follow character UNITY 5

Asked

Viewed 685 times

0

Error in Unity

Look rotation viewing vector is zero
UnityEngine.Quaternion:LookRotation(Vector3

Codigo Inteiro

    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
        public float speed = 6f;
        Vector3 movement;
        Animator anim;
        Rigidbody playerRidibody;
        int floorMask;
        float camRayLenght = 100f;
        private Vector3 playerToMause;


        void Awake()
        {
            floorMask = LayerMask.GetMask("Floor");

            anim = GetComponent<Animator>();
            playerRidibody = GetComponent<Rigidbody>();


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

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

        }

        void Move(float h, float v)
        {
            movement.Set(h, 0f, v);
            movement = movement.normalized * speed * Time.deltaTime;

            playerRidibody.MovePosition(transform.position + movement);
        }
        void Turning()
        {
            Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit floorHit;

            if (Physics.Raycast(camRay, out floorHit, camRayLenght, floorMask))

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

Linha Erro  >>  Quaternion newRotation = Quaternion.LookRotation(playerToMause);

Linha Erro  >>  playerRidibody.MoveRotation(newRotation);

        }


    }


    void Animating(float h, float v)
    {

        bool Walking = h != 0f || v != 0f;
        anim.SetBool("IsWalking", Walking);


    }


}
  • 2

    The error you quoted has no relation to the camera following the character or not, it is an error in the character looking in the mouse direction, try to follow correctly the tutorial you are following that will solve the code error. The script listed is from a Unity tutorial, done on the training day. To make the camera follow the character you need to follow the next step of the tutorial, which is the following video. https://www.youtube.com/watch?v=uGoAqv-uy6E Which will teach you to create a class that takes care of the camera following the character.

1 answer

1

Put a condition before Lookrotation, for example:

if (playerToMouse != Vector3.zero)
    Quaternion newRotation = Quaternion.LookRotation(playerToMause);

Browser other questions tagged

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