4
\$\begingroup\$

So my current concept is top-down arcade style game. Meaning you have a character and you walk around a closed space killing waves of enemies coming at you. My question is how to structure my abstractions. Since I'm using Unity3d at the end of the day there must a MonoBehaviour component that exist in the scene. For example I can have the following structure for the character.

public class CharacterController : MonoBehaviour
{
    ICharacter _character;
    IInputController _inputController;
    //and so on
} 

Is this a good idea at all ? Unity3d doesn't really cater to normal OOP practices imho. I know this question can be flagged as opinion based but please find it in your heart not to. I want to figure out a pattern to write good and easily extensible code in unity.

\$\endgroup\$
3
\$\begingroup\$

Unity does cater for that sort of abstraction, however you have to do some additional work to leverage it.

In my current project I have series of characters that can be controlled by the player or by AI, so I wanted to abstract their control. So I created a base class derived from MonoBehaviour that looks like this:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
public class CharController : MonoBehaviour
{
    [HideInInspector]
    public Vector3 movementVector = Vector3.zero;

    [HideInInspector]
    public Quaternion lookAtRotation = Quaternion.identity;

    public float moveSpeed = 250.0f;
    public float turnSpeed = 1.0f;

    protected Rigidbody _rigidbody = null;

    protected virtual void Awake()
    {
        _rigidbody = GetComponent<Rigidbody>();
    }

    protected virtual void Start()
    {
    }

    protected virtual void Update()
    {
    }

    protected virtual void FixedUpdate()
    {
        _rigidbody.AddForce(movementVector * moveSpeed);
        _rigidbody.rotation = Quaternion.Slerp(_rigidbody.rotation, lookAtRotation, turnSpeed);
    }
}

Which is standard Unity except for the use of protected and virtual on the standard methods.

Here is an example of a specialised version of this class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpecialCharController : CharController
{
    protected override void Awake()
    {
        base.Awake();

        // Special stuff here    
    }

    protected override void FixedUpdate()
    {
        base.FixedUpdate();

        // Special stuff here    
    }
}

Note the use of protected, override and calls to base.Method().

The PlayerController and AIController scripts only need to find and communicate with the CharController script in order to control all of the characters and the specialised version of the characters will work just fine.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.