Hello! I have this script/animation controller setup to idle, walk and suppose to jump.
I plan on adding run next. However, spacebar “jump” will not trigger. I get an error in my console when hitting spacebar. I appreciate any insight/advice/guidance! thank you!
NullReferenceException: Object reference not set to an instance of an object
PlayerControllerv3.Jump () (at Assets/Scripts/Player_v3/PlayerControllerv3.cs:30)
PlayerControllerv3.Update () (at Assets/Scripts/Player_v3/PlayerControllerv3.cs:21)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerv3 : MonoBehaviour
{
public Animator anim;
private Rigidbody rb;
public LayerMask layerMask;
public bool grounded;
void Start()
{
this.rb.GetComponent<Rigidbody>();
}
private void Update()
{
Grounded();
Jump();
Move();
}
private void Jump()
{
if(Input.GetKeyDown(KeyCode.Space) && !this.grounded)
{
this.rb.AddForce(Vector3.up * 80, ForceMode.Impulse);
}
}
private void Grounded()
{
if(Physics.CheckSphere(this.transform.position + Vector3.down, 0.2f, layerMask))
{
this.grounded = true;
}
else
{
this.grounded = false;
}
this.anim.SetBool("jump", this.grounded);
}
private void Move()
{
float verticalAxis = Input.GetAxis("Vertical");
float horizontalAxis = Input.GetAxis("Horizontal");
Vector3 movement = this.transform.forward * verticalAxis + this.transform.right * horizontalAxis;
movement.Normalize();
this.transform.position += movement * .2f;
this.anim.SetFloat ("vertical", verticalAxis);
this.anim.SetFloat ("horizontal", horizontalAxis);
}
}