U3DFPS第一人称 控制视野的移动脚本

using UnityEngine;
using System.Collections;

public class MySoliderMove : MonoBehaviour
{

Transform _head;

Transform _gun;
// Use this for initialization
void Start()
{
_head = transform.FindChild("Head");
_gun = transform.FindChild("M16");
}

// Update is called once per frame
void Update()
{
// Cursor.lockState = CursorLockMode.Locked;
_gun.forward = _head.forward; //让枪指向相机的中间位置
Move();
Rotate();
Run();
Jump();
}

[SerializeField]
float moveSpeed = 10f;
void Move()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");


Vector3 desPos = Vector3.right * horizontal + Vector3.forward * vertical;
transform.Translate(desPos * Time.deltaTime * moveSpeed);

}

void Run()
{
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = 30f;
}
else
{
moveSpeed = 10f;
}
}

void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
transform.Translate(Vector3.up*1.9f);
}
}

float rotationY = 0f;
[SerializeField]
float rotateSpeed = 20f;
void Rotate()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");

transform.Rotate(mouseX * Vector3.up * rotateSpeed);

rotationY += mouseY;

rotationY = Mathf.Clamp(rotationY, -60, 60); //控制人物视野

_head.localEulerAngles = Vector3.left * rotationY;


//public class FirstView : MonoBehaviour
//{

// 方向灵敏度
// public float sensitivityX = 10F;
// public float sensitivityY = 10F;

// 上下最大视角(Y视角)
// public float minimumY = -60F;
// public float maximumY = 60F;

// float rotationY = 0F;

// void Update()
// {
// 根据鼠标移动的快慢(增量), 获得相机左右旋转的角度(处理X)
// float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

// 根据鼠标移动的快慢(增量), 获得相机上下旋转的角度(处理Y)
// rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
// 角度限制. rotationY小于min,返回min. 大于max,返回max. 否则返回value
// rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

// 总体设置一下相机角度
// transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
// }

// void Start()
// {
// Make the rigid body not change rotation
// if (rigidbody)
// rigidbody.freezeRotation = true;
// }

}

}

转载于:https://www.cnblogs.com/wanggonglei/p/7833033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值