正式RPG - 第一回 重温中

这是做出人物上下左右,根据你赋值的速度移动的代码

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

public class Player : MonoBehaviour {

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


    private Vector2 direction;

	void Start () {
       
	}
	

	void Update () {

        GetInput();

        Move();
	}
    public void Move()
    {
        transform.Translate(direction * speed * Time.deltaTime);//一直向右移動(會出屏幕)和上面的Move();呼應

    }
    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、付费专栏及课程。

余额充值