Unity3D 改良行为控制脚本

更新时间:2023-06-17 11:42:14 阅读: 评论:0

using UnityEngine;
using System.Collections;
public class FPSWalker_edit02 : MonoBehaviour
{
函授研究生/// 类似于VRML的控制方式
/// ↑前进 ↓后退 →右转 ←左转
/// Ctrl + →右平移 Ctrl + ←左平移
/// 按住鼠标左键,可以通过鼠标上下左右转动视角
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
honoriapublic float gravity = 20.0f;
public bool MouChange = true;
private Vector3 moveDirection = ;
private bool grounded = fal;
void FixedUpdate()
{
if (grounded)
{
if (!(Input.GetKey(KeyCode.LeftControl)||Input.GetKey(KeyCode.RightControl))) //如果没有按下左Ctrl键
{
//只能前后平移
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
el //
tauranga{
//可前后左右平移
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetButton("Jump")) //跳跃
{
moveDirection.y = jumpSpeed;
}
}
//重力
moveDirection.y -= gravity * Time.deltaTime;
//移动controller
CharacterController controller = GetComponent("CharacterController") as CharacterController;
CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0; //当controller处在空中间,grounded为fal,即跳动和行走都无效
//鼠标控制视角
// if (Input.GetMouButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && MouChange) //如果按下鼠标左键并且鼠标MouChange为真
if (Input.GetMouButton(0) && MouChange) //如果按下鼠标左键并且鼠标MouChange为真
{
///鼠标旋转视角部分
///
if (axes == RotationAxes.MouXAndY)
{
// Read the mou input axis
//这里,rotationX和rotationY用来保存对象现有的角度,同时还将鼠标的移动中计算出增减的角度并合进来
husband什么意思
rotationX += Input.GetAxis("Mou X") * nsitivityX;
rotationY += Input.GetAxis("Mou Y") * nsitivityY;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up); //通过左右值和Vector3.up(作为以Y为旋转轴的向量值)求出左右旋转度的四元数值
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left); //通过上下值和Vector3.left(作为以X为旋转轴的向量值)求出上下旋转度的四元数值
//originalRotation = transform.localRotation;
transform.localRotation = originalRotation * xQuaternion * yQuaternion; //将上面求出来的左右和上下两个四元数值添加入角度中
}
el if (axes == RotationAxes.MouX)
{
rotationX += Input.GetAxis("Mou X") * nsitivityX;
cpicrotationX = ClampAngle(rotationX, minimumX, maximumX);
Quaternion xQuaternion
= Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
el
suneker{
rotationY += Input.GetAxis("Mou Y") * nsitivityY;
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
el
{
///左右旋转
///并且没有按下左或右ctrl键时
if (!(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)))
{
在线英英词典
Vector3 angle_temp = transform.eulerAngles;
angle_temp.y += Input.GetAxis("Horizontal") * nsitivityX * 0.3f;
rotationX = ClampAngle(angle_temp.y, minimumX, maximumX);
//rotationY = ClampAngle(rotationY, minimumY, maximumY);
transform.eulerAngles = angle_temp;
}
渤海大学研究生学院
//键盘上下键控制俯仰角
//el
//{
// Vector3 angle_temp = transform.eulerAngles;
// angle_temp.x += Input.GetAxis("Vertical") * -1 * nsitivityY * 0.3f;
/
/ rotationY = ClampAngle(angle_temp.x , minimumY , maximumY);
// transform.eulerAngles = angle_temp;
//}
}
}安排翻译
public enum RotationAxes { MouXAndY = 0, MouX = 1, MouY = 2 }
public RotationAxes axes = RotationAxes.MouXAndY;
public float nsitivityX = 15F;
public float nsitivityY = 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)
rigidbody.freezeRotation = true;
/
/originalRotation = transform.localRotation;    //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 Mathf.Clamp(angle, min, max);
}
2014考研英语答案
}

本文发布于:2023-06-17 11:42:14,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/975257.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:鼠标   角度   控制   后退   旋转轴   视角   研究生
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图