Unity3d使用列表保持玩家在比赛中的位置

时间:2018-06-01 08:20:19

标签: c# unity3d

我正在创建一个简单的赛车游戏,我想保持我的玩家的位置并显示它。我认为创建一个列表并放入我所有的敌人和我的播放器然后对它进行排序是有用的,但我被困在那里。首先,我只使用了一个敌人和我的玩家。这是我到目前为止所做的代码。任何想法如何继续?

public List<GameObject> Balls;
    public Text scoreText;
    int score;

    [SerializeField]
    GameObject myPlayer;

    [SerializeField]
    GameObject enemy1;

public List<GameObject> Balls;
void myList()
    {
        Balls.Add(myPlayer);
        Balls.Add(enemy1);
        Balls.Sort(CompareDistance);
    }

    private int CompareDistance(GameObject a, GameObject b)
    {
        float distance_a = a.GetComponent<GameObject>().transform.position.z;
        float distance_b = b.GetComponent<GameObject>().transform.position.z;
        if (distance_a >= distance_b)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }

2 个答案:

答案 0 :(得分:1)

欢迎使用Unity3d并尽力尝试,但请考虑使用object-oriented programming原则。这是一种很好的做法。

例如,你可以先创建一个你的玩家和敌人最终都会来自的Participant类。

public abstract class Participant : MonoBehaviour {
    //By default, the race is along the z direction
    public virtual float GetDistanceTravelled(Vector3 startPoint) {
        return startPoint.z - transform.position.z;
    }

    //At every frame, my participants shall move. How they move depends on what the player and enemy subclasses implement it.
    protected virtual void Update() {
        Move();
    }

    //Your participants will need to move, but players move with keyboard control, while enemies move programmatically. So we let the subclasses implement them.
    protected abstract void Move();
}

现在,我们创建PlayerEnemy脚本并将其附加到各自的GameObjects

public class Player : Participant {
    public override void Move() {
        //implement how you want to control your player to move
    }
}

public class Enemy : Participant { 
    public override void Move() {
        //implement how the enemies move automatically
    }
}

最后,我们需要一个班级RaceManager来处理比赛详情!您可以为此创建一个空GameObject并在此处附加此脚本。

public class RaceManager : MonoBehaviour {
    public List<Participant> participants; //you can add participants by dragging the gameobjects here from Unity's inspector, or add them in 
    public Vector3 startPoint; //again, set this in the inspector, or in Start()

    void Start() {
        //You can initialize startPoint, participants here. Or do it in the inspector
    }

    public void GetRaceDetails() {
        //Sort the list first. Check if the list is null first.. I will not do that here for clarity sake
        participants.Sort((p1, p2) => p1.GetDistanceTravelled(startPoint).Value.CompareTo(p2.GetDistanceTravelled(startPoint).Value)); //you can sort easily with lambda expressions

        //print the results here, you can iterate through the list and do a Debug.Log() or something
    }
}

答案 1 :(得分:0)

你的代码非常接近,但有一些简单的错误。您的结果应与排序函数相反,GameObject不是组件,是组件容器,您已经有了引用。在这两个更改后,您的方法似乎工作正常。我还添加了一个等于条件,以避免交换不需要交换的元素。

 private int CompareDistance(GameObject a, GameObject b)
    {
        float distance_a = a.transform.position.z;
        float distance_b = b.transform.position.z;
        if (distance_a==distance_b) return 0;
        if (distance_a >= distance_b)
            return -1;  else  return 1;
    }

还要尽量避免过于频繁地创建列表,创建一次并且稍后再排序,创建和填充列表可能相对昂贵(可以排序)