我正在尝试在Unity上实现finger-four形成。基本上,将有一个领导者和3个粉丝。领导者左键单击飞机,其他人跟随领导者。阵型将如下所示:
0
1 2
3
领导者为0,粉丝为1,2和3.当我左键单击时,领导者应该移动到点击位置,其他人应该跟随。追随者需要面对他们现在面对的方向,而不是面对领导者。有人可以帮我解决这个问题吗? 谢谢。
我已经实现了移动脚本。领导者正在左侧点击飞机。这是我用于此的代码:
using UnityEngine;
using System.Collections;
public class movementTry : MonoBehaviour {
private Transform myTransform; // this transform
private Vector3 destinationPosition; // The destination Point
private float destinationDistance; // The distance between myTransform and destinationPosition
public float moveSpeed; // The Speed the character will move
void Start () {
myTransform = transform; // sets myTransform to this GameObject.transform
destinationPosition = myTransform.position; // prevents myTransform reset
}
void Update () {
// keep track of the distance between this gameObject and destinationPosition
destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
if(destinationDistance < .5f){ // To prevent shakin behavior when near destination
moveSpeed = 0;
}
else if(destinationDistance > .5f){ // To Reset Speed to default
moveSpeed = 5;
}
// Moves the Player if the Left Mouse Button was clicked
if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
}
// Moves the player if the mouse button is hold down
else if (Input.GetMouseButton(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
// myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
// To prevent code from running if not needed
if(destinationDistance > .5f){
myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
}
}
答案 0 :(得分:2)
我找到了解决方案。它实际上非常简单。如果有人需要知道,这就是它的完成方式。
创建一个空的游戏对象。我们称之为“GameUnits” 将您希望在此阵型中移动的所有其他对象拖到GameUnits中。 向GameUnits添加一个新的C#脚本,并复制并粘贴上面提到的代码。 现在左键单击一个平面将使整个组以其精确的形式移动。