【问题标题】:Unity - Tilt a spaceship when turning to the left or the rightUnity - 向左或向右转动时倾斜宇宙飞船
【发布时间】:2018-03-01 05:35:19
【问题描述】:

目前我有一艘环绕行星飞行的宇宙飞船。看起来是这样的

我使用这个代码

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);
    }
}

所以当我按 A 时,我想将船向左倾斜

当按 D 时,我想向右倾斜船

有人知道怎么做吗?

【问题讨论】:

  • 如果 A 或 D 关闭,您可以检查更新,然后应用您的旋转功能。

标签: c# unity3d


【解决方案1】:

而不是这个:

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);
    }
}

您可能有兴趣观看以下教程,因为它与您尝试创建的内容相似:

https://unity3d.com/es/earn/tutorials/projects/space-shooter/moving-the-player?playlist=17147

【讨论】:

    【解决方案2】:

    您还可以查看此资产 - 所有代码都已注释并且非常流畅 - 对我帮助很大。

    基本上,它有一个非常通用的脚本来执行基于 LERP 的船舶凸轮跟踪。这有点像普通的平滑跟随,除了你得到它是全 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);
    

    https://assetstore.unity.com/packages/3d/vehicles/space/low-poly-space-ship-ultra-smooth-360-controller-111640

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 2012-12-14
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      相关资源
      最近更新 更多