Answer the question
In order to leave comments, you need to log in
Where did NullReferenceExeption come from?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float Speed = 6f;
Vector3 _movement;
Animator _anim;
Rigidbody _playerRigidBody;
int _floorMask;
float CamRayLength = 100f;
void Start()
{
_floorMask = LayerMask.GetMask("Floor");
_anim = GetComponent<Animator>();
_playerRigidBody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
Turning();
}
void Move(float h, float v)
{
_movement.Set(h, 0f, v);
_movement = _movement.normalized * Speed * Time.deltaTime;
_playerRigidBody.MovePosition(transform.position + _movement);
}
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 = 0;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
_playerRigidBody.MoveRotation(newRotation);
}
}
// Update is called once per frame
void Update () {
}
}
Answer the question
In order to leave comments, you need to log in
The next day, the error disappeared as unexpectedly as it appeared)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question