• unity 前后左右 移动


    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    public class NewBehaviourScript : MonoBehaviour

    {

        public float moveSpeed = 5f;    // 移动速度

        public float rotateSpeed = 180f;    // 旋转速度

        // Start is called before the first frame update

        void Start()

        {

            tx= this.gameObject.GetComponent();

            animator=this.gameObject.GetComponent();

        }

    public Transform tx;

    private Animator animator;

        // Update is called once per frame

        bool bool_run=false;

        void Update()

        {

            // 按下W键向前移动

            if (Input.GetKey(KeyCode.W))

            {

                transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

                //if(Input.GetAxis("bool_run")==false)

                //if(!bool_run)

                {

                    bool_run=true;

                    animator.SetBool("bool_run",true);

                    //animator.Play("run");

                }

               

               

            }

            // animator.SetBool("bool_run",false);

            // 按下W键向前移动

            if (Input.GetKey(KeyCode.Space))

            {

               //animator.Play("jum");            

               

            }

            // 按下S键向后移动

            if (Input.GetKey(KeyCode.S))

            {

                transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);

            }

            // 按下A键向左移动

            if (Input.GetKey(KeyCode.A))

            {

                transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);

                 Vector3 angle = tx.localEulerAngles;

            angle.y -=0.5f;

            tx.localEulerAngles = angle;

            }

            // 按下D键向右移动

            if (Input.GetKey(KeyCode.D))

            {

                transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);

                Vector3 angle = tx.localEulerAngles;

            angle.y +=0.5f;

            tx.localEulerAngles = angle;

            }

            // 按下Q键向下移动

            if (Input.GetKey(KeyCode.Q))

            {

                transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);

            }

            // 按下E键向上移动

            if (Input.GetKey(KeyCode.E))

            {

                transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);

            }

            // 按下左箭头键向左旋转

            if (Input.GetKey(KeyCode.LeftArrow))

            {

                transform.Rotate(Vector3.up, -rotateSpeed * Time.deltaTime);

            }

            // 按下右箭头键向右旋转

            if (Input.GetKey(KeyCode.RightArrow))

            {

                transform.Rotate(Vector3.up, rotateSpeed * Time.deltaTime);

            }

        }

    }

  • 相关阅读:
    什么是文件格式的幻数
    响应式的 switchboard:让又大又慢的Vue/AIpine 页面爆快
    【小5聊】毕业8年,一直在追梦的路上
    [附源码]JAVA毕业设计框架的电脑测评系统(系统+LW)
    stm32_标准库_中断_按键点灯|蜂鸣器
    对Docker容器进行取证 CSAW CTF 2022 DockREleakage
    神器抓包工具 HTTP Analyzer v7.5 的下载,安装,使用,破解说明以及可能遇到的问题
    Shiro会话管理&&Ehcache缓存管理
    LeetCode每日一题(2438. Range Product Queries of Powers)
    c++ memccpy和 = 都可以用于赋值操作
  • 原文地址:https://blog.csdn.net/qq_30121869/article/details/132765332