在游戏中人物角色移动、跳跃和视角转动是属于最基本的功能,那这些最基本的功能在开发过程中是如何实现的呢?下面就给大家简单介绍下人物移动、跳跃、视角转动这些基本动作的实现方法。
代码如下:
using UnityEngine;
using System.Collections;
//开始时+一个限制 必须有刚
[RequireComponent (typeof(Rigidbody ))]
//碰撞器
[RequireComponent(typeof (CapsuleCollider ))]
public class playerControll : MonoBehaviour {
[System.Serializable] //可视化
//将与速度有关的量提出到一个方法 里
public class MoveSetting
{
public float ForwardSpeed = 5f;
public float BackSpeed = 3f;
public float HorizonSpeed = 4f;
public float RunValue = 2f;
public float JumpForce = 5f;
}
[System.Serializable ]
public class MouseLook
{
//摄像机旋转灵敏度
public float XSensitive = 2f;
public float YSensitive = 2f;
}
public MoveSetting moveSet;
public MouseLook CameraSet;
public float currentSpeed;
//设定一个bool值来判断是否可以跳跃
private bool m_jump = true;
private CapsuleCollider m_capsule;
private Rigidbody m_rigidbody;
private Camera m_camera;
//相机的Transform减少Update中transform的调用
private Transform m_camTrans;
//主角的Transform
private Transform m_chaTrans;
//摄像机的欧拉角
private Vector3 m_camRotate;
//主角的欧拉角
private Vector3 m_chaRotate;
//摄像机旋转四元数
private Quaternion m_camQutation;
//主角的旋转四元数
private Quaternion m_chaQutation;
public AnimationCurve SlopCurve;
private bool m_isOnGround = true;
private Vector3 curGroundNormal;
private Transform m_camTransTemp;
//跳跃的次数
private int num = 0;
// Use this for initialization
void Start () {
m_capsule = GetComponent();
m_rigidbody = GetComponent();
m_camera = Camera.main;
m_camTrans = m_camera.transform;
m_chaTrans = transform;
m_camQutation = m_camTrans.rotation;
m_chaQutation = m_chaTrans.rotation;
m_camTransTemp = GameObject.Find("StartPoint").transform;
}
// Update is called once per frame
//FixedUpdate lateUpdate 刚体给力都放在FixedUpdate中
void Update () {
RotateView();
}
void FixedUpdate()
{
DoMove();
dun();
JumpUP();
}
//视图的旋转
void RotateView()
{
float xRote = Input.GetAxis("Mouse Y")* CameraSet.YSensitive;
float yRote = Input.GetAxis("Mouse X")* CameraSet.XSensitive;
//四元数的使用
m_camQutation *= Quaternion.Euler(-xRote, 0f, 0f);
m_camTrans.localRotation = m_camQutation;
m_chaQutation *= Quaternion.Euler(0f, yRote, 0f);
m_chaTrans.localRotation = m_chaQutation;
// m_camRotate *= Quaternion.Euler(-xRote, 0f, 0f);
// m_camTrans.localRotation = m_camRotate;
//限制摄像机旋转范围
if (m_camQutation.x > 0.5 || m_camQutation.x < -0.5)
{
m_camQutation = a;
}
if(m_chaQutation.y > 0.5 || m_chaQutation.y < -0.5)
{
m_chaQutation = b;
}
void DoMove()
{
CheckGround();
Vector2 input = GetInput();
CaculateSpeed(input);
if ((Mathf.Abs(input.x) > float.Epsilon || Mathf.Abs(input.y) > float.Epsilon) && m_isOnGround)
{
Vector3 desireMove = m_camTrans.forward * input.y + m_camTrans.right * input.x;
desireMove = Vector3.ProjectOnPlane(desireMove, curGroundNormal).normalized;
desireMove *= currentSpeed;
if (m_rigidbody.velocity.sqrMagnitude < currentSpeed * currentSpeed)
{
m_rigidbody.AddForce(desireMove * SlopeValue(), ForceMode.Impulse);
}
}
}
//控制任务二段跳
void JumpUP()
{
CheckGround();
if (m_isOnGround)
{
m_rigidbody.drag = 5f;
num = 0;
m_jump = true;
}
else
{
m_rigidbody.drag = 0f;
}
if (Input.GetKeyDown(KeyCode.Space)&& m_jump==true)
{
num++;
m_rigidbody.drag = 0f;
m_rigidbody.velocity = new Vector3(m_rigidbody.velocity.x, 0f, m_rigidbody.velocity.z);
m_rigidbody.AddForce(new Vector3(0, moveSet.JumpForce, 0), ForceMode.Impulse);
if (num >= 2)
{
m_jump = false;
}
}
}
//检测方向键输入
Vector2 GetInput()
{
Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
return input;
}
//计算速度
void CaculateSpeed(Vector2 _input)
{
currentSpeed = moveSet.ForwardSpeed;
//前进后退
if(Mathf.Abs (_input.x)>float .Epsilon)
{
currentSpeed = moveSet.HorizonSpeed;
}
else if(_input.y<0)
{
currentSpeed = moveSet.BackSpeed;
}
if (Input.GetKey(KeyCode.LeftShift))
{
currentSpeed *= moveSet.RunValue;
}
}
//爬坡参数
float SlopeValue()
{
float angle = Vector3.Angle(curGroundNormal, Vector3.up);
float value = SlopCurve.Evaluate(angle);
return value;
}
//检测地面
void CheckGround()
{
RaycastHit hit;
//一般+0.01
if (Physics.SphereCast(m_capsule.transform.position, m_capsule.radius, Vector3.down, out hit, ((m_capsule.height / 2 - m_capsule.radius) + 0.01f)))
{
curGroundNormal = hit.normal;
m_isOnGround = true;
}
else
{
curGroundNormal = Vector3.up;
m_isOnGround = false;
}
}
//实现下蹲效果 摄像机位置降低 移动速度减小
void dun()
{
if (Input.GetKeyDown(KeyCode.Z))
{
m_camTrans.position = m_chaTrans.position;
moveSet.ForwardSpeed = 2f;
}
if(Input.GetKeyUp(KeyCode.Z))
{
m_camTrans.position = m_camTransTemp.position;
moveSet.ForwardSpeed = 5f;
}
}
//落地缓冲
void Checkbuff()
{
RaycastHit hit;
float speed = m_rigidbody.velocity.y;
if (speed < 0)
{
if (Physics.SphereCast(m_capsule.transform.position, m_capsule.radius, Vector3.down, out hit, ((m_capsule.height / 2 - m_capsule.radius) + 1f)))
{
speed *= 0.5f;
m_rigidbody.velocity = new Vector3(m_rigidbody.velocity.x, speed, m_rigidbody.velocity.z);
}
}
}
}