我对Unity和C#比较陌生,并且一直在努力弄清楚这一点!
我希望我的飞船逐渐加速,然后慢慢停下来,就像没有重力一样,因为这将是一场太空游戏!
如果任何人能帮助我,或者甚至指导我到这个主题的教程,我将不胜感激!:)
这是我目前的宇宙飞船代码。
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerMovement : MonoBehaviour {
public float maxSpeed = 6f;
public float rotSpeed = 180f;
void Start ()
{
}
void Update () {
Quaternion rot = transform.rotation;
float z = rot.eulerAngles.z;
z -= CrossPlatformInputManager.GetAxis ("Horizontal") * rotSpeed * Time.deltaTime;
rot = Quaternion.Euler (0, 0, z);
transform.rotation = rot;
Vector3 pos = transform.position;
Vector3 velocity = new Vector3 (0, CrossPlatformInputManager.GetAxis("Vertical") * maxSpeed * Time.deltaTime, 0);
pos += rot * velocity;
transform.position = pos;
}
}如何做到这一点?
发布于 2016-10-24 15:13:31
与Unity中的任何东西一样,有100种方法可以潜在地做到这一点,但我认为使用物理学将是最简单的。
现在,您正在直接操作变换。这很好,但实际上比仅仅让Unitys物理为您做数学计算要复杂得多。
所有你需要做的就是在你的船上附加一个刚体组件(或者2D游戏的Rigidbody2D )。
然后,通过将力添加到刚体,您将获得良好的渐变加速,并通过调整检查器中刚体的线性和角度拖动,您将获得非常平滑和自然的感觉减速。
这是关于使用物理学的官方文档:https://docs.unity3d.com/Manual/class-Rigidbody.html
利用物理是Unity开发中非常有趣和重要的一部分,所以如果你刚刚开始,我强烈建议你现在就学习它,这是用它来解决的完美问题。
发布于 2016-10-24 16:08:49
您可以尝试这样做:
Vector3 velocity = transform.forward * CrossPlatformInputManager.GetAxis("Vertical") * maxSpeed * Time.deltaTime;
pos += velocity;
transform.position = pos;我希望它是有用的。
https://stackoverflow.com/questions/40212226
复制相似问题