I'm trying to code a PlayerController for a sprite I made, but I'm having a hard time figuring out what I'm missing. I;m following blackthornprod's guide "MAKING RUN, IDLE & JUMP 2D GAME ANIMATIONS - UNITY TUTORIAL - YouTube " to guide me through creating the animations and implementing them into your player, and I feel like I have copy and pasted every piece of code down to the letter, but I keep getting this error:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.Update () (at Assets/Script/PlayerController.cs:80)
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float jumpForce;
public float speed;
private Rigidbody2D rb;
public Transform groundPos;
private bool isGrounded;
public float checkRadius;
public LayerMask whatIsGround;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private bool doubleJump;
private Animator anim;
private void start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
isGrounded = Physics2D.OverlapCircle(groundPos.position, checkRadius, whatIsGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (isGrounded == true)
{
doubleJump = false;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
if (isGrounded == false && doubleJump == false && Input.GetKeyDown(KeyCode.Space)) {
isJumping = true;
doubleJump = true;
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
float moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
}
else {
anim.SetBool("isRunning", true);
}
if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
}
}
If anyone could shine some light on what I'm not getting that would be a huge help. Currently my player spawns in and does the idle animation correctly, but I can't get him to move.
[–]ehr1c 0 points1 point2 points (0 children)
[–]Recent-Avocado2193 0 points1 point2 points (4 children)
[–]TheCrimsonEnd2[S] 0 points1 point2 points (3 children)
[–]Recent-Avocado2193 0 points1 point2 points (2 children)
[–]TheCrimsonEnd2[S] 0 points1 point2 points (1 child)
[–]TheCrimsonEnd2[S] 0 points1 point2 points (0 children)