0
\$\begingroup\$

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;
        }
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ It looks like your derived version updates the direction and sprintB variables only after the base class has done the base version of movement - too late for that to have any effect. Have you considered passing your modified direction and sprint variables as parameters, telling the base function what you want it to do differently through that channel? \$\endgroup\$
    – DMGregory
    Feb 26, 2021 at 16:18
  • \$\begingroup\$ yes i wanted to controll input variables throught derived class and then pass it to main class. I also tried lot of solutions for example put virtual FixedUpdate to main function and then override it in derived class, and so on.But this solutions didn't work for me(maybe bcs. i done it completely wrong) so if u know any other oop solution how to fix not responding control pls tell me \$\endgroup\$
    – Gorlomi
    Feb 26, 2021 at 17:09
  • \$\begingroup\$ i also have characterA which also derive from characterMain and this derived class have method for jumping - characterA provides jumping and characterB provides sprint \$\endgroup\$
    – Gorlomi
    Feb 26, 2021 at 17:12
  • \$\begingroup\$ Are you sure these even need to be separate classes, instead of one class with inspector parameters that control the availability/tuning of the jump and sprint? That way you can make characters who jump, characters who sprint, characters who jump AND sprint, and ones that do neither, without repeating the code for each combination. Generally in games, we try to avoid implementing game mechanics through rigid inheritance hierarchies, since we usually like the flexibility to mix and match effects like this. \$\endgroup\$
    – DMGregory
    Feb 26, 2021 at 17:16
  • 1
    \$\begingroup\$ My advice is: do not do that. OOP isn't something to reach for for OOP's own sake. Use it when it solves a problem. When it causes problems, don't use it. There are (somewhat rare) situations where OOP and inheritance can help in Unity, but this does not look like one of them. \$\endgroup\$
    – DMGregory
    Feb 26, 2021 at 18:08

0

You must log in to answer this question.

Browse other questions tagged .