i’m stuck at that point
I saw the bug @anthonyirwin82 has shown on the first answer.
I changed
animator.SetFloat(“Look X”, lookDirection.x);
animator.SetFloat(“Look Y”, lookDirection.y);
but I keep on getting error:
NullReferenceException: Object reference not set to an instance of an object RubyController.Update () (at Assets/Scripts/RubyController.cs:51)
I commented out the three line:
//animator.SetFloat(“Look X”, lookDirection.x);
//animator.SetFloat(“Look Y”, lookDirection.y);
//animator.SetFloat(“Speed”, move.magnitude);
and the game can run
I’ve also debugged the 3 variable:
Debug.Log(lookDirection.x + “/” + lookDirection.y);
Debug.Log(move.magnitude);
and they works
so I think the problem is that
Animator animator;
is not working, but is written similar to EnemyController
someone got this problem to?
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RubyController : MonoBehaviour
{
public float speed = 3.0f;
public int maxHealth = 5;
public float timeInvincible = 2.0f;
public int health { get { return currentHealth; } }
private int currentHealth;
bool isInvincible;
float invincibleTimer;
Rigidbody2D rigidbody2d;
Animator animator;
// Start is called before the first frame update
void Start()
{
rigidbody2d = GetComponent();
currentHealth = maxHealth;
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis(“Horizontal”);
float vertical = Input.GetAxis(“Vertical”);
Vector2 move = new Vector2(horizontal, vertical);
Vector2 lookDirection = new Vector2(1, 0);
if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
{
lookDirection.Set(move.x, move.y);
lookDirection.Normalize();
}
//Debug.Log(lookDirection.x + “/” + lookDirection.y);
//Debug.Log(move.magnitude);
animator.SetFloat(“Look X”, lookDirection.x);
animator.SetFloat(“Look Y”, lookDirection.y);
animator.SetFloat(“Speed”, move.magnitude);
Vector2 position = rigidbody2d.position;
position = position + move * speed * Time.deltaTime;
rigidbody2d.MovePosition(position);
if (isInvincible)
{
invincibleTimer -= Time.deltaTime;
if (invincibleTimer < 0)
isInvincible = false;
}
}
public void ChangeHealth(int amount)
{
if (amount < 0)
{
if (isInvincible)
return;
isInvincible = true;
invincibleTimer = timeInvincible;
}
currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
Debug.Log(currentHealth + “/” + maxHealth);
}
}
thanks