I’ve been working on my project and trying to make the animation and coding work. I added the animations but now all of a sudden, the movement in the player script is all broken and not working.
This is my only script, so there’s no other ones I need to publish, if you’d like to see my Animator or anything else like that, let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
//General Variables
public float speed;
private Rigidbody2D rb;
private GameObject sword;
//Class Setting Variables
public PlayerClassTypes eCurClass = PlayerClassTypes.Tank;
//Animator Variables
Animator sword_Anim;
void Start ()
{
//Sets variables automatically on launch
sword_Anim = sword.GetComponent<Animator> ();
sword = GameObject.Find ("Sword");
rb = gameObject.GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
//Making the velocity variables to make 'fake friction'
Vector3 easeVelocity = rb.velocity;
easeVelocity.y *= 0.75f;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.75f;
//Sets the variables to move them up and down
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
//Fake Friction / Easing the x speed of our player
rb.velocity = easeVelocity;
//Moving the player up and down
rb.AddForce (Vector2.up * speed * v);
rb.AddForce (Vector2.right * speed * h);
}
//Sets the Player classes
public enum PlayerClassTypes
{
Tank,
DPS,
Healer
}
//Handles the Tank class
void HandleTankState ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
//Starts Sword Swing animation
sword_Anim.SetBool("swing_Animation", true);
}
}
}
The error message I’m getting is “Object reference not set to an instance of an object Player.FixedUpdate () (at Assets/Scripts/Player.cs:30)”
Thanks for looking over my script and any input at all would be very helpful!