比如:
// 返回键
if ( Applicationplatform == RuntimePlatformAndroid &&(InputGetKeyDown(KeyCodeEscape)))
{
//
}
// Home键
if ( Applicationplatform == RuntimePlatformAndroid &&(InputGetKeyDown(KeyCodeHome) ))
{
//
}附件上传不了,只有复制了,将就看
using UnityEngine;
using SystemCollections;
public class FPSWalker_edit02 : MonoBehaviour
{
/// 类似于VRML的控制方式
/// ↑前进 ↓后退 →右转 ←左转
/// Ctrl + →右平移 Ctrl + ←左平移
/// 按住鼠标左键,可以通过鼠标上下左右转动视角
public float speed = 60f;
public float jumpSpeed = 80f;
public float gravity = 200f;
public bool MouseChange = true;
private Vector3 moveDirection = Vector3zero;
private bool grounded = false;
void FixedUpdate()
{
if (grounded)
{
if (!(InputGetKey(KeyCodeLeftControl)||InputGetKey(KeyCodeRightControl))) //如果没有按下左Ctrl键
{
//只能前后平移
moveDirection = new Vector3(0, 0, InputGetAxis("Vertical"));
moveDirection = transformTransformDirection(moveDirection);
moveDirection = speed;
}
else //
{
//可前后左右平移
moveDirection = new Vector3(InputGetAxis("Horizontal"), 0, InputGetAxis("Vertical"));
moveDirection = transformTransformDirection(moveDirection);
moveDirection = speed;
}
if (InputGetButton("Jump")) //跳跃
{
moveDirectiony = jumpSpeed;
}
}
//重力
moveDirectiony -= gravity TimedeltaTime;
//移动controller
CharacterController controller = GetComponent("CharacterController") as CharacterController;
CollisionFlags flags = controllerMove(moveDirection TimedeltaTime);
grounded = (flags & CollisionFlagsCollidedBelow) != 0; //当controller处在空中间,grounded为false,即跳动和行走都无效
//鼠标控制视角
// if (InputGetMouseButton(0) && (InputGetKey(KeyCodeLeftControl) || InputGetKey(KeyCodeRightControl)) && MouseChange) //如果按下鼠标左键并且鼠标MouseChange为真
if (InputGetMouseButton(0) && MouseChange) //如果按下鼠标左键并且鼠标MouseChange为真
{
///鼠标旋转视角部分
///
if (axes == RotationAxesMouseXAndY)
{
// Read the mouse input axis
//这里,rotationX和rotationY用来保存对象现有的角度,同时还将鼠标的移动中计算出增减的角度并合进来
rotationX += InputGetAxis("Mouse X") sensitivityX;
rotationY += InputGetAxis("Mouse Y") sensitivityY;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion xQuaternion = QuaternionAngleAxis(rotationX, Vector3up); //通过左右值和Vector3up(作为以Y为旋转轴的向量值)求出左右旋转度的四元数值
Quaternion yQuaternion = QuaternionAngleAxis(rotationY, Vector3left); //通过上下值和Vector3left(作为以X为旋转轴的向量值)求出上下旋转度的四元数值
//originalRotation = transformlocalRotation;
transformlocalRotation = originalRotation xQuaternion yQuaternion; //将上面求出来的左右和上下两个四元数值添加入角度中
}
else if (axes == RotationAxesMouseX)
{
rotationX += InputGetAxis("Mouse X") sensitivityX;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
Quaternion xQuaternion = QuaternionAngleAxis(rotationX, Vector3up);
transformlocalRotation = originalRotation xQuaternion;
}
else
{
rotationY += InputGetAxis("Mouse Y") sensitivityY;
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion yQuaternion = QuaternionAngleAxis(rotationY, Vector3left);
transformlocalRotation = originalRotation yQuaternion;
}
}
else
{
///左右旋转
///并且没有按下左或右ctrl键时
if (!(InputGetKey(KeyCodeLeftControl) || InputGetKey(KeyCodeRightControl)))
{
Vector3 angle_temp = transformeulerAngles;
angle_tempy += InputGetAxis("Horizontal") sensitivityX 03f;
rotationX = ClampAngle(angle_tempy, minimumX, maximumX);
//rotationY = ClampAngle(rotationY, minimumY, maximumY);
transformeulerAngles = angle_temp;
}
//键盘上下键控制俯仰角
//else
//{
// Vector3 angle_temp = transformeulerAngles;
// angle_tempx += InputGetAxis("Vertical") -1 sensitivityY 03f;
// rotationY = ClampAngle(angle_tempx , minimumY , maximumY);
// transformeulerAngles = angle_temp;
//}
}
}
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxesMouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
public float rotationX = 0F;
public float rotationY = 0F;
Quaternion originalRotation;
void Start()
{
// Make the rigid body not change rotation
//使刚体不会改变角度
if (rigidbody)
rigidbodyfreezeRotation = true;
//originalRotation = transformlocalRotation; //BUG处
originalRotation = new Quaternion(0f, 0f, 0f, 1f); //修正代码
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return MathfClamp(angle, min, max);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)