Question by
ofreehan · Jan 26, 2018 at 02:30 PM ·
error message
Object reference not set to an instance of an object at Survival Game Unity
I'm taking error at mouseposition part I check everything but I couldnt find anything.I change names and other things and this project already completed I use completed script but Im stil getting same error.I hope I can fix that proplem with your helps... Thank you
strong text
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRaylength = 100f;
void Awake ()
{
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 ();
Animating (h,v);
}
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 cray = Camera.main.ScreenPointToRay (Input.mousePosition);**. Object reference not set to an instance of an object here
RaycastHit floorHit;
if (Physics.Raycast (cray ,out floorHit ,camRaylength,floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
playerRigidbody.MoveRotation (newRotation);
}
}
void Animating(float h , float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool ("IsWalking", walking);
}
}
Comment