Posting here and hoping to find an answer to this
I'm new to OOP and right now running across some problems. I'm trying to call a few methods when you press a certain key, but when I press that key, absolutely nothing happens.
Here is the class I use to check what keys are being pressed and what method will be called. Every method I'm calling doesn't get executed. Tried a different way around with the 'Run' method. By switching a bool when you press the run key, 'movement' will be multiplied by a number, but that neither does anything.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Player : MonoBehaviour
{
public PlayerShape currentShape;
public GameObject terrain;
public KeyCode forward;
public KeyCode backward;
public KeyCode left;
public KeyCode right;
public KeyCode crouch;
public KeyCode jump;
public KeyCode run;
public KeyCode toggleAlwaysRun;
public KeyCode fightReady;
public KeyCode hit;
public float horizontalSpeed = 10f;
public float verticalSpeed = 10f;
public KeyCode shapeShift;
void Awake()
{
currentShape = new SpartanKing();
}
void Update()
{
currentShape.isRunning = Input.GetKey(run);
if (Input.GetKey(run))
{
currentShape.isRunning = true;
currentShape.Run();
Debug.Log("is running");
}
if (Input.GetKey(jump))
{
currentShape.Jump();
}
if (Input.GetKeyDown(hit))
{
currentShape.Attack();
}
if (Input.GetKey(shapeShift))
{
ShapeShift();
}
currentShape.Smooth();
}
public virtual void ShapeShift()
{
Vector3 previousPos = currentShape.transform.position;
Quaternion previousRot = currentShape.transform.rotation;
Destroy(currentShape);
//currentShape = Instantiate(_player, previousPos, previousRot);
}
}
The abstract class:
using UnityEngine;
using System.Collections;
public abstract class PlayerShape : MonoBehaviour
{
public float speed;
public int jumpHeight;
public Animator animatorComp;
public Vector3 movement;
public bool isRunning;
// When pressing WASD keys
public abstract void Walk();
// When pressing WASD keys and shift at the same time
public abstract void Run();
// When pressing space
public abstract void Jump();
// When pressing L-mouse
public abstract void Attack();
// When pressing R-mouse
public abstract void Block();
public virtual void Smooth()
{
}
}
And the class that inherits the abstract class:
public class SpartanKing : PlayerShape
{
void Start()
{
animatorComp = this.GetComponent<Animator>();
speed = 2.2f;
jumpHeight = 500;
}
void Update()
{
AnimatorStateInfo stateInfo = animatorComp.GetCurrentAnimatorStateInfo(0);
float mouseX = Input.GetAxis("Mouse X");
transform.Rotate(0, mouseX * 5f, 0);
movement = Input.GetAxis("Vertical") * Vector3.forward * speed + Input.GetAxis("Horizontal") * Vector3.right * speed;
if (isRunning)
{
Debug.Log("Currently running, modifier applied");
movement.z *= 3.0f;
movement.x *= 1.5f;
}
transform.Translate(movement * Time.deltaTime);
animatorComp.SetFloat("Speed", movement.magnitude / 5);
}
// When pressing WASD keys
public override void Walk()
{
}
// When pressing WASD keys and shift at the same time
public override void Run()
{
movement.z *= 3.0f;
movement.x *= 1.5f;
}
// When pressing space
public override void Jump()
{
if (!animatorComp.GetBool("IsJumping"))
{
this.GetComponent<Rigidbody>().AddForce(Vector3.up * jumpHeight);
animatorComp.SetBool("IsJumping", true);
}
}
// When pressing L-mouse
public override void Attack()
{
animatorComp.SetTrigger("IsAttacking");
}
// When pressing R-mouse
public override void Block()
{
}
public override void Smooth()
{
transform.Translate(movement * Time.deltaTime);
animatorComp.SetFloat("Speed", movement.magnitude / 5);
}
}