2017-07-22 35 views
0

目前我有一艘飛船圍繞着一顆行星飛行。它看起來像這樣團結 - 向左或向右傾斜飛船

enter image description here

,我使用此代碼

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

所以,當我按。我想船上小費向左

enter image description here

,並按時DI要將小船運送到右側

enter image description here

有人知道該怎麼做嗎?

+0

您可以檢查如果A或D關閉,請更新,然後應用您的旋轉功能。 – Universus

+0

您是否可以接受我的答案,以防萬一您正在尋找。如果您需要更多關於如何解決問題的信息,請告訴我,我會用更長的解釋完成 –

回答

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

0

你也可以看看這個資產 - 所有的代碼註釋,並且它的超光滑 - 對我幫助很大。

基本上,它有一個漂亮的多功能腳本來執行LERP基於凸輪的船跟隨。這有點像一個普通的Smooth Follow,除非你用完整的3D獲得它,所以船可以完全顛倒等等。控制很容易改變,這是我自己做的,但默認是船不斷移動並使用箭頭鍵進行俯仰/偏航。

這裏的主要思想 - 你有childed相機的目標對象和相機採用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