正式RPG - 第二回 重温中

除了player以外,建立一个abstract class character 隶属于player

character代码:

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

public  abstract class Character : MonoBehaviour {

    [SerializeField]
    private float speed;

    protected Vector2 direction;

    void Start () {
		
	}
	
	
	protected virtual void Update () {
        Move();
	}
    public void Move()
    {
        transform.Translate(direction * speed * Time.deltaTime);

    }
}

player修改后代码:

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

public class Player : Character {

    [SerializeField]
    private float speed;
    //public后,可在外面屬性欄賦值,private不可,會隱藏該屬性欄,而加了[SerializeField]后,即可以private的狀態賦值

	void Start () {
       
	}
	

	protected override void Update () {

        GetInput();

        base.Update();
	}

    private void GetInput()
    {

        direction = Vector2.zero;

        if(Input.GetKey(KeyCode.W))
        {
            direction += Vector2.up;
        }
        if (Input.GetKey(KeyCode.A))
        {
            direction += Vector2.left;
        }
        if (Input.GetKey(KeyCode.S))
        {
            direction += Vector2.down;
        }
        if (Input.GetKey(KeyCode.D))
        {
            direction += Vector2.right;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值