unity3d第一人称视角移动 c#
发布网友
发布时间:2022-04-28 13:06
我来回答
共1个回答
热心网友
时间:2022-06-08 19:03
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
public int move_speed = 50;
//电脑测试用
private float mousex = 0;
private float mousey = 0;
public float mouseMoveSpeed = 5;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//空格键抬升高度
if (Input.GetKey(KeyCode.Space))
{
transform.position = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
}
//w键前进
if (Input.GetKey(KeyCode.W))
{
this.gameObject.transform.Translate(new Vector3(0, 0, move_speed * Time.deltaTime));
}
//s键后退
if (Input.GetKey(KeyCode.S))
{
this.gameObject.transform.Translate(new Vector3(0, 0, -1*move_speed * Time.deltaTime));
}
//a键后退
if (Input.GetKey(KeyCode.A))
{
this.gameObject.transform.Translate(new Vector3(-1* move_speed * Time.deltaTime, 0, 0));
}
//d键后退
if (Input.GetKey(KeyCode.D))
{
this.gameObject.transform.Translate(new Vector3(move_speed * Time.deltaTime, 0, 0));
}
//旋转视角
Cursor.visible = false;
mousex += Input.GetAxis("Mouse X") * mouseMoveSpeed;
mousey += Input.GetAxis("Mouse Y") * mouseMoveSpeed;
this.transform.rotation = Quaternion.Euler(-mousey, mousex, 0);
}
}
同学,这个是基础,还是自己要好好加油。
把这个COPY到你的脚本里,再拖到摄像机上就行了。