2015-12-19 94 views
1

我想添加一個現實的前瞻性駕駛行爲,以簡單的車輛。目前我使用的是剛體MovePosition,當我從Input中檢測到正向/反向軸時,我只應用MoveRotation。車輛的現實轉彎

相反,我想要應用方向/旋轉,即使汽車正在滑行,但不希望它在靜止時轉向,就好像它是一輛履帶式車輛。我還希望轉彎似乎來自車輛前方,車輪(不是遊戲中的實際物體)將位於該前方。

我迄今主要是從坦克教程借:

m_MovementInputValue = Input.GetAxis(m_MovementAxisName); 
    m_TurnInputValue = Input.GetAxis(m_TurnAxisName); 

    // Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames. 
    Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime; 

    // Apply this movement to the rigidbody's position. 
    m_Rigidbody.MovePosition(m_Rigidbody.position + movement); 

    if (m_MovementInputValue > 0) 
    { 
     float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime; 

     // Make this into a rotation in the y axis. 
     Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f); 

     // Apply this rotation to the rigidbody's rotation. 
     m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);  
    } 

回答

0

我認爲你可以通過查看統一API的輪對撞機部件保存自己一些時間:http://docs.unity3d.com/Manual/class-WheelCollider.html,我看到了一個演示一些它曾用它來製作一款完整的賽車遊戲,因此它應該能夠模擬真實的車輛(Demo in question)。

除此之外,這裏是你如何可以做到這一點:

對於初學者來說,如果你想你的車輛轉向,只有當它前進或後退,你可以通過速度乘以你力您的車輛向前矢量

float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime * (m_Rigidbody.velocity.magnitude/MaxVehicleSpeed); 

(m_Rigidbody.velocity.magnitude/MaxVehicleSpeed)的想法是有一個價值betwee取決於車輛速度,n 0和1。

然後從車輛前方出現需要改變旋轉點的位置,並且我找不到使用MoveRotation的解決方案。您可以通過使用剛體AddForceAtPosition函數來實現,但計算它會變得更加複雜。

這裏的理念是:

Vector3 force = transform.right * turn; 
m_Rigidbody.AddForceAtPosition(force, MiddleOfWheelAxis); 

隨着MiddleOfWheelAxis你的兩個前輪之間的點。

+0

我玩輪撞機,但它比我所尋找的更復雜。我可以再試一次 至於其餘的,謝謝。我唯一沒有得到的是velocity.forward。是否在Unity 5中被棄用? – Roger

+0

對不起,我在說速度矢量的前向分量,但它沒有太大意義。幅度/ maxSpeed似乎更合適,我會更新我的帖子。 – Sharundaar