物体移动

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

/// <summary>
/// 物体移动
/// </summary>
public class move : MonoBehaviour
{
    private CharacterController cc;   //角色控制器
    private Vector3 vec;  //向量3
    private float speed = 10;  //速度

    // Use this for initialization
    void Start()
    {
        cc = GetComponent<CharacterController>();  //获取组件
    }


    // Update is called once per frame
    void Update()
    {
        //判断水平和垂直方向
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");


        //不为0时,
        if (h != 0 || v != 0)
        {
            //沿着x轴移动x轴,y轴沿着y轴移动,z沿着x轴移动z轴
            this.transform.Translate(h * speed * Time.deltaTime, 0, v * speed * Time.deltaTime);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_42090285/article/details/80332107