I’m having trouble with my code and I can not solve it.
The error is the following:
Below also follow both classes involved in the error:
Character.cs:
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour
{
void Update ()
{
characterMoving();
characterAttack();
}
public void characterAttack()
{
IAttackBehaviour a;
if (Input.GetKeyDown(KeyCode.Alpha1))
{
a = new GunAttack();
a.attack();
} else
if (Input.GetKeyDown(KeyCode.Alpha2))
{
a = new SwordAttack();
a.attack();
}
}
public void characterMoving()
{
Movement m = new Movement();
m.running();
}
}
Movement.cs:
using UnityEngine;
using System.Collections;
public class Movement : Character {
private float runSpeed = 10.0f;
private float jumpGravity = 10.0f;
private float jumpPower = 20.0f;
private Vector3 moveDirection = new Vector3(1,0,0);
public void running()
{
CharacterController c = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterController>();
moveDirection = c.transform.TransformDirection(moveDirection);
moveDirection *= runSpeed;
if ((Input.GetKeyDown(KeyCode.Space))(c.isGrounded)) moveDirection.y = jumpPower;
moveDirection.y -= jumpGravity * Time.deltaTime;
c.Move(moveDirection * Time.deltaTime);
}
}
I made the following change, the error disappeared, but the character does not jump gently:
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour
{
void Update ()
{
characterMoving();
characterAttack();
}
public void characterAttack()
{
IAttackBehaviour a;
if (Input.GetKeyDown(KeyCode.Alpha1))
{
a = new GunAttack();
a.attack();
} else
if (Input.GetKeyDown(KeyCode.Alpha2))
{
a = new SwordAttack();
a.attack();
}
}
public void characterMoving()
{
IMovementBehaviour m = new Movement();
m.running();
}
}
public interface IMovementBehaviour
{
void running();
}
using UnityEngine;
using System.Collections;
public class Movement : IMovementBehaviour {
private float runSpeed = 1.0f;
private float jumpGravity = 20.0f;
private float jumpPower = 10.0f;
private Vector3 moveDirection = new Vector3(1,0,0);
public void running()
{
CharacterController c = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterController>();
moveDirection = c.transform.TransformDirection(moveDirection);
moveDirection *= runSpeed;
if ((Input.GetKeyDown(KeyCode.Space))(c.isGrounded)) moveDirection.y = jumpPower;
moveDirection.y -= jumpGravity * Time.deltaTime;
c.Move(moveDirection * Time.deltaTime);
}
}
You are creating a new Movement object on each Update() call - this is doing two things:
On each frame, you are creating a new instance of Movement object, which is unnecessary and will impact memory usage/performance
Since each frame a new Movement object is created, each time you run m.running(), it’ll always be working with the default values for each of the member in Movement class (for example, moveDirection will always appear to be 1,0,0 because you are always dealing with a new copy of Movement object).
Keep a single reference to the Movement object in your Character class instead of creating a new one each time.