C# 统一-向左或向右倾斜宇宙飞船
目前我有一艘环绕行星飞行的宇宙飞船。看起来像这样 我使用这个代码C# 统一-向左或向右倾斜宇宙飞船,c#,unity3d,C#,Unity3d,目前我有一艘环绕行星飞行的宇宙飞船。看起来像这样 我使用这个代码 public class PlayerMovement : MonoBehaviour { private float currentMovementSpeed = 30; private float rotationSpeed = 80; private Rigidbody rigid; private void Start() { rigid = GetCompone
public class PlayerMovement : MonoBehaviour
{
private float currentMovementSpeed = 30;
private float rotationSpeed = 80;
private Rigidbody rigid;
private void Start()
{
rigid = GetComponent<Rigidbody>();
}
private void Update()
{
// move the spaceship
rigid.MovePosition(rigid.position + transform.TransformDirection(new Vector3(0, 0, 1)) * currentMovementSpeed * Time.deltaTime);
// rotate the shaceship by pressing A or D
transform.Rotate(0, Input.GetAxis(StringCollection.INPUT_HORIZONTAL) * rotationSpeed * Time.deltaTime, 0);
}
}
公共类玩家移动:单一行为
{
专用浮点数currentMovementSpeed=30;
专用浮动旋转速度=80;
私有刚体;
私有void Start()
{
刚性=GetComponent();
}
私有void更新()
{
//移动宇宙飞船
刚性.MovePosition(刚性.position+transform.TransformDirection(新向量3(0,0,1))*currentMovementSpeed*Time.deltaTime);
//按A或D旋转轴瓦
transform.Rotate(0,Input.GetAxis(StringCollection.Input_HORIZONTAL)*rotationSpeed*Time.deltaTime,0);
}
}
所以当我按下A键时,我想把船向左倾斜
按D键时,我想把船向右倾斜
有人知道怎么做吗?而不是这个:
transform.Rotate(0, Input.GetAxis(StringCollection.INPUT_HORIZONTAL) * rotationSpeed * Time.deltaTime, 0);
您应该使用如下内容:
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
下面是一个使用它的示例:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
您可能有兴趣观看以下教程,因为它与您尝试创建的内容类似:
您也可以查看此资产-所有代码都有注释,而且非常流畅-对我帮助很大 基本上,它有一个非常通用的脚本来执行基于LERP的船的cam跟踪。这有点像一个正常的平滑跟随,除了你在全3D中得到它,所以船可以完全颠倒,等等。控制很容易改变,这是我自己做的,但默认情况是船在不断移动,你使用箭头键俯仰/偏航 这里是主要的想法-您有一个儿童相机目标对象,相机使用LERPing来跟踪它-为访问/减速提供了一个超级有趣的效果:
// lerp the camera back to its correct position
_cam.transform.localPosition = Vector3.Lerp(_cam.transform.localPosition, _camTarget.transform.localPosition, 1f - CameraLag);
_cam.transform.localRotation = Quaternion.Lerp(_cam.transform.localRotation, _camTarget.transform.localRotation, 1f - CameraLag);
如果A或D关闭,您可以签入更新,然后应用旋转功能。