作者:最好的zixue | 来源:互联网 | 2023-09-13 14:15
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;[RequireComponen
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
[AddComponentMenu("Control Script/FPS Input")]
public class FPSinput : MonoBehaviour {//此脚本放在人上,不是摄像机上
public float speed = 120.0f;
public float g = -9.8f;
private CharacterController _charController;
// Use this for initialization
void Start () {
_charCOntroller= GetComponent();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.X)) { speed += 10.0f; }
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 move = new Vector3(deltaX, 0, deltaZ);
move = Vector3.ClampMagnitude(move, speed);//限制行动
move.y = g;
if (Input.GetKeyDown(KeyCode.Space)) {
if(_charController.isGrounded)
{
move.y = 0;
move.y -= g * 52.0f;
}
//rigidbody.AddForce(Vector3.up * 5.0f);
// transform.Translate(new Vector3(transform.position.x, transform.position.y+5.0f, transform.position.z));
}
//有碰撞检测
move = transform.TransformDirection(move);//本地转全局变量
_charController.Move(move* Time.deltaTime);//增量移动
// transform.Translate(deltaX,0,deltaZ);// 无碰撞检测
}
}
配合Mouselook.cs