I moved from C to C# so I am a novice in Object Oriented Programming. I wanted to implement OOP in my character movement, but it didn't work the way I expected.
My character is instantiated when the scene starts, but it doesn't move (maybe because of bad parameter passing).
I just want to control input from derived class which is giving my character the ability to sprint.
- characterB script is attached to character in Inspector
- that character is instantiated prefab and is instantiated when scene start
- inherited script which is overridden is giving ability to sprint to character
- photon is package for multiplayer games so you don't have to notice it
whole source code is so big, so i put there one method for movement and sorry for my weak english skillz
//works like standard MonoBehaviour
public abstract class characterMain : MonoBehaviourPunCallbacks
{
// declaration of variables
public virtual void Start()
{
RB = GetComponent<Rigidbody>();
}
protected virtual void movement(float horXF, float vertYF, bool sprintB)
{
//Debug.Log("movement function base class" + horF); // NOT responding
groundDetect = Physics.Raycast(...);
if (standOnGround)
{
direction = new Vector3(horXF, 0, vertYF) * Time.deltaTime * speed;
direction.Normalize();
adjustSpeed = speed;
targetSpeed = transform.TransformDirection(direction) * adjustSpeed * Time.deltaTime;
RB.velocity = targetSpeed;
}
}
}
public class characterB : characterMain
{
[Header("Movement")]
protected float horXF;
protected float vertYF;
[Header("sprint")]
private bool sprintB;
public void FixedUpdate()
{
if (!photonView.IsMine) return; //Photon
//MOVEMENT INPUT
horXF = Input.GetAxisRaw("Horizontal");
vertYF = Input.GetAxisRaw("Vertical");
//JUMP INPUT
skokB = Input.GetKey(KeyCode.Space);
movement(horXF, vertYF, sprintB);
//Debug.Log("movement function" + horF); //responding
}
protected override void movement(float horXF,float vertYF,bool sprintB)
{
//Debug.Log("movement function" + horF); // NOT responding
if (sprintB && ... ) //sprint
{
base.movement(horXF, vertYF, sprintB);
sprintB = bZrychlenieC && groundDetect && ... ;
if (sprintB) direction = new Vector3(0, 0, sprintSpeed) * Time.deltaTime;
}
}
}