当前位置: 首页 > news >正文

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<Transform>();

        animator=this.gameObject.GetComponent<Animator>();

    }

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);

        }

    }

}

http://www.lryc.cn/news/159746.html

相关文章:

  • 计算机视觉传统图像处理库opencv的使用
  • 【数据库】通过实例讲清楚,Mongodb的增删查改,分组查询,聚合查询aggregate
  • vue + video.js 加载多种视频流(HLS、FLV、RTMP、RTSP)
  • 用 Python 微调 ChatGPT (GPT-3.5 Turbo)
  • 单目标应用:基于蜘蛛蜂优化算法(Spider wasp optimizer,SWO)的微电网优化调度MATLAB
  • 2023年7月京东饮料行业数据分析(京东运营数据分析)
  • 执行 JUnit 单元测试前,修改环境变量
  • openGauss学习笔记-63 openGauss 数据库管理-资源池化架构
  • 计算机竞赛 基于深度学习的植物识别算法 - cnn opencv python
  • ChatGPT如何应对紧急情况和灾害应对?
  • ElementUI浅尝辄止37:Select 选择器
  • PCL 基于任意四点计算球心坐标
  • 飞书即时消息无需API开发连接Cohere,打造飞书AI智能问答助手
  • FPGA实现Cordic算法——向量模式
  • 【常用代码14】el-input输入框内判断正则,只能输入数字,过滤汉字+字母。
  • [NLP]LLM--使用LLama2进行离线推理
  • 初始化一个Gin框架的Go-Web项目
  • Mybatis日期检索格式报错
  • 如何把Android Framework学彻底?一条龙学习
  • uview indexList 按字母跳转不了
  • 安全模型中的4个P
  • 网站优化搜索引擎与关键词
  • aws-msk-托管kafka集群的简单使用(VPC内部访问:无验证和SASL认证)
  • Unity UGUI(二)核心组件
  • 代码随想录二刷第二天(Python)
  • Mybatis学习笔记1 Mybatis入门
  • Spark 管理和更新Hadoop token 流程
  • Android文件关联
  • java操作adb查看apk安装包包名【搬代码】
  • 【JAVA】Object类与抽象类