2012-06-04 24 views
1

介紹如何在一個指定的位置上找到一個物體並減速到停止位置?

目前我試圖創建一個腳本(在Unity C#腳本),控制點和點擊一個遊戲對象的行爲。

實質上,玩家最終將通過簡單地點擊屏幕來控制熱氣球,從而物體開始加速並移向點擊位置。當在點擊位置的特定距離處時,熱氣球應該開始在相反的方向上加速(從而減速)並完全停止在點擊位置。

我已經創建了一個功能齊全的評論腳本(在這篇文章的底部發現),它只是以一個固定的速度向一個點擊位置移動一個GameObject,然後在特定距離內開始放慢速度直到停止。

我還創建了一個單獨的功能完整的腳本,它在加速(朝着小行星推進行爲的那種類型)的同時向點擊位置移動。這也可以在帖子的底部找到。

的問題

現在我面臨的問題是,我已經工作相當長一段時間,現在找到一個解決方案,你可以採取這兩種行爲,並實現它們作爲一個工作的腳本,也就是說,熱氣球有加速行爲,同時放緩,在目標位置停止,酷似這裏找到這樣的視覺例如:

Visual Arrival Behaviour Demonstration

問題然後

我的問題是:

你怎麼能做出到來行爲不僅使用恆定的速度,但加速包括等式中呢?我試圖研究這個問題並嘗試使用我自己的解決方案,但是我沒有做任何事情,似乎按照它應該的方式工作。

腳本附件

點和點擊以恆定速度移動就到達​​行爲

using UnityEngine; 
using System.Collections; 

public class PlayerControl : MonoBehaviour 
{ 
    // Fields 
    Transform cachedTransform; 
    Vector3 currentMovementVector; 
    Vector3 lastMovementVector; 
    Vector3 currentPointToMoveTo; 
    Vector3 velocity; 
    int movementSpeed; 

    Vector3 clickScreenPosition; 
    Vector3 clickWorldPosition; 

    float slowingDistance = 8.0f; 

    Vector3 desiredVelocity; 

    float maxSpeed = 1000.0f; 

    // Use this for initialization 
    void Start() 
    { 
     cachedTransform = transform; 
     currentMovementVector = new Vector3(0, 0); 
     movementSpeed = 50; 
     currentPointToMoveTo = new Vector3(0, 0); 
     velocity = new Vector3(0, 0, 0); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     // Retrive left click input 
     if (Input.GetMouseButtonDown(0)) 
     { 
      // Retrive the click of the mouse in the game world 
      clickScreenPosition = Input.mousePosition; 

      clickWorldPosition = Camera.main.ScreenToWorldPoint(new  Vector3(clickScreenPosition.x, clickScreenPosition.y, 0)); 
      currentPointToMoveTo = clickWorldPosition; 

      currentPointToMoveTo.z = 0; 

      // Calculate the current vector between the player position and the click 
      Vector3 currentPlayerPosition = cachedTransform.position; 

      // Find the angle (in radians) between the two positions (player position and click position) 
      float angle = Mathf.Atan2(clickWorldPosition.y - currentPlayerPosition.y, clickWorldPosition.x - currentPlayerPosition.x); 

      // Find the distance between the two points 
      float distance = Vector3.Distance(currentPlayerPosition, clickWorldPosition); 

      // Calculate the components of the new movemevent vector 
      float xComponent = Mathf.Cos(angle) * distance; 
      float yComponent = Mathf.Sin(angle) * distance; 

      // Create the new movement vector 
      Vector3 newMovementVector = new Vector3(xComponent, yComponent, 0); 
      newMovementVector.Normalize(); 

      currentMovementVector = newMovementVector; 
     } 

     float distanceToEndPoint = Vector3.Distance(cachedTransform.position, currentPointToMoveTo); 

     Vector3 desiredVelocity = currentPointToMoveTo - cachedTransform.position; 

     desiredVelocity.Normalize(); 

     if (distanceToEndPoint < slowingDistance) 
     { 
      desiredVelocity *= movementSpeed * distanceToEndPoint/slowingDistance; 
     } 
     else 
     { 
      desiredVelocity *= movementSpeed; 
     } 

     Vector3 force = (desiredVelocity - currentMovementVector); 
     currentMovementVector += force; 

     cachedTransform.position += currentMovementVector * Time.deltaTime; 
    } 
} 

點和使用的加速度,但沒有到來行爲單擊運動

using UnityEngine; 
using System.Collections; 

public class SimpleAcceleration : MonoBehaviour 
{ 
    Vector3 velocity; 
    Vector3 currentMovementVector; 

    Vector3 clickScreenPosition; 
    Vector3 clickWorldPosition; 

    Vector3 currentPointToMoveTo; 
    Transform cachedTransform; 

    float maxSpeed; 

    // Use this for initialization 
    void Start() 
    { 
     velocity = Vector3.zero; 
     currentMovementVector = Vector3.zero; 
     cachedTransform = transform; 
     maxSpeed = 100.0f; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     // Retrive left click input 
     if (Input.GetMouseButtonDown(0)) 
     { 
      // Retrive the click of the mouse in the game world 
      clickScreenPosition = Input.mousePosition; 

      clickWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(clickScreenPosition.x, clickScreenPosition.y, 0)); 
      currentPointToMoveTo = clickWorldPosition; 

      // Reset the z position of the clicking point to 0 
      currentPointToMoveTo.z = 0; 

      // Calculate the current vector between the player position and the click 
      Vector3 currentPlayerPosition = cachedTransform.position; 

      // Find the angle (in radians) between the two positions (player position and click position) 
      float angle = Mathf.Atan2(clickWorldPosition.y - currentPlayerPosition.y, clickWorldPosition.x - currentPlayerPosition.x); 

      // Find the distance between the two points 
      float distance = Vector3.Distance(currentPlayerPosition, clickWorldPosition); 

      // Calculate the components of the new movemevent vector 
      float xComponent = Mathf.Cos(angle) * distance; 
      float yComponent = Mathf.Sin(angle) * distance; 

      // Create the new movement vector 
      Vector3 newMovementVector = new Vector3(xComponent, yComponent, 0); 
      newMovementVector.Normalize(); 

      currentMovementVector = newMovementVector; 
     } 

     // Calculate velocity 
     velocity += currentMovementVector * 2.0f * Time.deltaTime; 

     // If the velocity is above the allowed limit, normalize it and keep it at a constant max speed when moving (instead of uniformly accelerating) 
     if (velocity.magnitude >= (maxSpeed * Time.deltaTime)) 
     { 
      velocity.Normalize(); 
      velocity *= maxSpeed * Time.deltaTime; 
     } 

     // Apply velocity to gameobject position 
     cachedTransform.position += velocity; 
    } 
} 
+0

這也不是AI,但控制理論。我已經移除了標籤。 – ziggystar

回答

3

適應所述第一腳本:

引入一個可變velocity,如在第二腳本。在Start()中設置爲movementSpeed,之後不要使用movementSpeed不要繼續,直到這完美的作品。

下面介紹加速度:

if (distanceToEndPoint < slowingDistance) 
{ 
    velocity *= distanceToEndPoint/slowingDistance; 
} 
else 
{ 
    velocity += direction * 2.0f * Time.deltaTime; 
} 
3

根據您希望如何顯示動作,您可能需要constant velocity equationsthese equations。等速度會更容易。

例如:您可以將原點和目標點之間的距離除以2.然後使用數學中途加速,然後減速。

+0

啊,是的,我早些時候在看一些這些方程:)但是我認爲讓我困惑的部分是,當你開始添加來自小行星的運動時(當你改變速度時,你不要停止當前的方向,而要在新的方向上順利加速)。我似乎無法將它實現到基本上可以工作的第一個腳本中,它不會考慮恆定的加速度(我會使用它),而只是恆定的速度而不加速。也許我現在只是盲目地盯着自己 – CodingBeagle

相關問題