如何统一从子类更改父类变量

时间:2019-03-08 10:54:14

标签: c# unity3d

我只是从Unity开始,我正在使用面向角色的OOP原理制作基本的2D游戏。 我创建了一个名为 EnemyController 的常规敌人类,它有一个名为 BasicEnemy 的孩子。

这个想法是当产生敌人时(在两个点之间,在这种情况下为床和条),它会向左或向右移动并开始在物体之间巡逻。 我为此使用了射线投射,到目前为止,当敌人击中这些点中的任何一个时,我都已经获得了射线与精灵一起翻转。 我似乎无法让精灵沿新翻转的方向移动。

我尝试在父类中修改 moveSpeed 变量,并尝试编写自己的运动函数 LeftMove RightMove

我的代码如下。

这是Parent类的代码:

public class EnemyController : MonoBehaviour
{
    //transform for each of the enemies
    protected Transform enemyTransform;
    protected Vector2 startPosition;
    protected int damageValue, healthLevel;
    public int moveSpeed;
    protected int randNum;
    protected Rigidbody2D rb;

    private void Start()
    {
        enemyTransform = transform;
        startPosition = enemyTransform.position;
        rb = GetComponent<Rigidbody2D>();
        randNum = Random.Range(1,3);
        // Debug.Log(randNum);

        if(randNum == 1) {
            Debug.Log("Moves Left at the start");
        } if(randNum == 2) {
            Debug.Log("Moves Right at the start");
            Flip(); 
        }
    }
    private void FixedUpdate()
    {
        Move(); 
    }

    protected virtual void Move()
    {
        if(randNum == 1) {
            rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
        }
        if(randNum == 2) {
            rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
        }
    }

    public void Flip() {
        Vector3 enemyScale = transform.localScale;
        enemyScale.x *= -1;
        transform.localScale = enemyScale;
    }

}

这是Child类的代码:

public class BasicEnemy : EnemyController
{
    //Origin, Direction, Range for Raycasting.
    public Transform rayOriginPoint;
    private Vector2 rayDir = new Vector2(-1,0);
    public float range;

    //GameObjects that it should hit
    GameObject bar, bed;
    EnemyController parentClass;
    private void Start()
    {
        bar = GameObject.Find("minibar");
        bed = GameObject.Find("bed");
    }

    private void Update()
    {
        RaycastHit2D hitObject = Physics2D.Raycast(rayOriginPoint.position,rayDir,range);
        Debug.DrawRay(rayOriginPoint.position,rayDir*range);

        if(hitObject == true) {
            if(hitObject.collider.name == bed.name) {
                Debug.Log(hitObject.collider.name);
                Flip();
                rayDir *= -1;
                LeftMove();
            }
            if(hitObject.collider.name == bar.name) {
                Debug.Log(hitObject.collider.name);
                Flip();
                rayDir *= -1;
                RightMove();
            }
        }
    }

    void LeftMove() {
        rb.velocity = new Vector2(-1, rb.velocity.y);
    }

    void RightMove(){
       rb.velocity = new Vector2(1, rb.velocity.y);
    }
}

有人可以帮我吗? 预先感谢!

2 个答案:

答案 0 :(得分:1)

 private void FixedUpdate()
    {
        Move(); 
    }
protected virtual void Move()
{
    if(randNum == 1) {
        rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
    }
    if(randNum == 2) {
        rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
    }
}

基本控制器中的上述代码块根据randNum告诉敌人要向哪个方向移动。

void LeftMove() {
        rb.velocity = new Vector2(-1, rb.velocity.y);
    }

    void RightMove(){
       rb.velocity = new Vector2(1, rb.velocity.y);
    }

您继承的控制器中的该块会翻转速度。

将速度从“左/右”移动翻转后,FixedUpdate调用并调用Move,然后将速度更改回randNum中的速度

建议的解决方案: 存储bool directionRight,在创建时将其值随机化,

在您的fixedUpdate-Move中

if(directionRight)
    rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
else
   rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);

让您的翻转功能调用directionRight =!directionRight即可翻转方向。 不再需要左右移动功能

答案 1 :(得分:0)

我设法通过覆盖子类中的父函数来解决此问题。

private bool facingRight;

    private void Start()
    {
        bar = GameObject.Find("minibar");
        bed = GameObject.Find("bed");

        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        RaycastHit2D hitObject = Physics2D.Raycast(rayOriginPoint.position,rayDir,range);
        Debug.DrawRay(rayOriginPoint.position,rayDir*range);

        if(hitObject == true) {
            if(hitObject.collider.name == bed.name) {
                Debug.Log(hitObject.collider.name);
                rayDir *= -1;
                facingRight = true;
                Flip();
            }
            if(hitObject.collider.name == bar.name) {
                Debug.Log(hitObject.collider.name);
                rayDir *= -1;
                facingRight = false;
                Flip();
            }
        }
    }

    protected override void Move()
    {
        // rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
        base.Move();
        if(facingRight == false) {
            rb.velocity = new Vector2(-1, rb.velocity.y);
        }
        else {
            rb.velocity = new Vector2(1,rb.velocity.y);
        }
    }

就像@Prodigle所说,我有一个布尔值,为了翻转精灵,我对其进行了更改。