Unity中控制物体移动的方法

方法1

void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
        }
    }

方法2

void Update()
    {
        float h= Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        if (h!= 0 || v!= 0)
        {
            transform.Translate(new Vector3(h, 0, v) * Time.deltaTime * moveSpeed, Space.World);
        }
    }

方法3_通过刚体控制物体移动

void Update()
    {
        Vector3 moveForward = Vector3.zero;
        if (Input.GetKey(KeyCode.W))
        {
            moveForward.z += 1;
        }
        if (Input.GetKey(KeyCode.S))
        {
            moveForward.z += -1;
        }
        if (Input.GetKey(KeyCode.A))
        {
            moveForward.x += -1;
        }
        if (Input.GetKey(KeyCode.D))
        {
            moveForward.x += 1;
        }
        GetComponent<Rigidbody>().MovePosition(Quaternion.LookRotation(transform.forward) 
            * moveForward * moveSpeed * Time.fixedDeltaTime + transform.position);
    }

方法四_通过CharacterController组件控制移动

private CharacterController cha;
    public float speed = 3;
    // Start is called before the first frame update
    void Start()
    {
        cha = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //第一种  受重力影响
        //cha.SimpleMove(new Vector3(h, 0, v) * speed);
        //第二种  无重力影响
        cha.Move(new Vector3(h, 0, v) * speed * Time.deltaTime);
        print(cha.isGrounded);  //判断是否在地面上
    }
    /// 
    /// 
    /// 
    /// 
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        //打印碰撞信息
        print(hit.collider);
    }

5、示例

2D物体移动方法  以2Dtank图片为例
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// 
/// 管理玩家坦克的类
/// 2020/2/25
/// [Tooltip("显示注释")]//鼠标移到变量上后可以看到汉字
/// [Header("显示注释")]//直接在面板上显示汉字
/// 
public class OwnTank : MonoBehaviour
{
    private float h;
    private float v;
    [Header("玩家坦克移动速度")]
    public float ownTankMoveSpeed = 3;
    private SpriteRenderer sr;
    public Sprite[] spriteArray;
    [Header("获取子弹预制体")]
    public GameObject BulletPrefab;
    private Vector3 bulletEulerAngles;
    private float timeVal=0;   //计时器
    private void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
    }
    // Update is called once per frame
    void Update()
    {
        Attack();
    }
    private void FixedUpdate()
    {
        Move();
    }
    /// 
    /// 玩家坦克攻击的方法
    /// 
    void Attack()
    {
        if (timeVal >= 0.4f)   //攻击CD>=0.4,才可以攻击
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                //子弹产生的角度=当前坦克的角度+子弹要旋转的角度
                Instantiate(BulletPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bulletEulerAngles));
                timeVal = 0;
            }
        }
        else
        {
            timeVal += Time.deltaTime;
        } 
    }
    /// 
    /// 玩家坦克移动的方法
    /// 
    private void Move()
    {
        h = Input.GetAxisRaw("Horizontal");
        transform.Translate(Vector3.right * h * ownTankMoveSpeed * Time.fixedDeltaTime, Space.World);
        if (h < 0)
        {
            sr.sprite = spriteArray[2];
            bulletEulerAngles = new Vector3(0, 0, 90);
        }
        else if (h > 0)
        {
            sr.sprite = spriteArray[3];
            bulletEulerAngles = new Vector3(0, 0, -90);
        }
        if (h != 0)
        {
            return;
        }
        v = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector3.up * v * ownTankMoveSpeed * Time.fixedDeltaTime, Space.World);
        if (v < 0)
        {
            sr.sprite = spriteArray[1];
            bulletEulerAngles = new Vector3(0, 0, 180);
        }
        else if (v > 0)
        {
            sr.sprite = spriteArray[0];
            bulletEulerAngles = new Vector3(0, 0, 0);
        }
    }
}

你可能感兴趣的:(Unity中控制物体移动的方法)