I am getting a NullReferenceException: object reference not set to an instant of an object.
I am a beginner and had learned this through a video. All the code is good but the null appears on the line:
"playerRigidBody.MovePosition(transform.position + movement);"
I have tried for several hours to write the code but have not nailed it. I do need help.
using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour
{
public float speed = 6f;
public float turnSpeed = 60f;
public float turnSmoothing = 15f;
private Vector3 movement;
private Vector3 turning;
private Animator anim;
private Rigidbody playerRigidBody;
void awake()
{
playerRigidBody = GetComponent<rigidbody>();
anim = GetComponent<animator>();
}
void FixedUpdate()
{
float lh = Input.GetAxisRaw ("Horizontal");
float lv = Input.GetAxisRaw ("Vertical");
Move (lh, lv);
Animating (lh, lv);
}
void Move (float lh, float lv)
{
movement.Set (lh, 0f, lv);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidBody.MovePosition(transform.position + movement);
if (lh != 0f || lv != 0f)
{
Rotating (lh, lv);
}
}
void Rotating(float lh, float lv)
{
Vector3 targetDirection = new Vector3 (lh, 0f, lv);
Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp (GetComponent <rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);
GetComponent<rigidbody>(). MoveRotation(newRotation);
}
void Animating (float lh, float lv)
{
bool running = lh != 0f || lv != 0f;
anim.SetBool ("IsRunning", running);
}
}</rigidbody></rigidbody></animator></rigidbody>