目前我試圖創建一個腳本(在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;
}
}
這也不是AI,但控制理論。我已經移除了標籤。 – ziggystar