【问题标题】:Finger-four formation Unity手指四阵统一
【发布时间】:2015-11-06 00:36:49
【问题描述】:

我正在尝试在 Unity 上实现四指编队。本质上,将有一个领导者和三个追随者。领导者在飞机上左键移动,其他人跟随领导者。编队将如下所示:

                0
             1     2
                      3

领导者为0,追随者为1、2、3。当我左键单击时,领导者应该移动到点击的位置,其他人应该跟随。追随者需要面对与他们现在面对的方向相同的方向,而不是面对领导者。有人可以帮我解决这个问题吗? 谢谢。


编辑 1:

我已经实现了移动脚本。领导者在飞机上单击左键移动。这是我用于此的代码:

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

【问题讨论】:

  • 如果您发布您的代码以及运行它时获得的结果,这将有所帮助。您可以编辑您的问题以添加更多信息。
  • 我完全迷失了。我尝试了很多东西。我可以移动角色,但我不知道如何让其他角色跟随他。我将使用我的移动脚本更新原始帖子。

标签: c# unity3d


【解决方案1】:

我想出了解决办法。它实际上非常简单。如果有人需要知道,这就是它的完成方式。

创建一个空的游戏对象。让我们称之为“GameUnits” 将您希望在此编队中移动的所有其他对象拖到 GameUnits 中。 将新的 C# 脚本添加到 GameUnits 并复制并粘贴上面提到的代码。 现在左键单击一个平面将使整个组按其确切的编队移动。

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多