unity控制物体移动和跳跃的C#脚本

unity控制物体移动和跳跃的C#脚本

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

public class move : MonoBehaviour
{
//该脚本控制游戏对象的移动

void Start()
{
//游戏对象初始化时执行的代码
}

public float speed = 3.0f;

void Update () {
//每帧执行的代码

//获取水平和垂直输入轴的值
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);

//创建一个三维向量,储存水平和垂直输入轴的值
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

//判断是否按下空格键
if (Input.GetKeyDown(KeyCode.Space))
{
//如果是,则给游戏对象一个向上的力
GetComponent<Rigidbody>().AddForce(Vector3.up * 500);
}

//根据按下的方向键,调整移动方向
if (Input.GetKey(KeyCode.W)) {
movement += Vector3.forward;
}

if (Input.GetKey(KeyCode.S)) {
movement += Vector3.back;
}

if (Input.GetKey(KeyCode.A)) {
movement += Vector3.left;
}

if (Input.GetKey(KeyCode.D)) {
movement += Vector3.right;
}

//对游戏对象进行移动
transform.Translate(movement * speed * Time.deltaTime);
}
}

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x