无法在Unity C中跟踪对象的位置#

时间:2016-09-15 17:57:17

标签: c# unity3d position transform gameobject

我有一个非常奇怪的问题,我几个小时都无法解决。我试图在两个物体彼此靠近时触发和发生事件。我过去从来没有遇到任何麻烦,但由于某种原因,这次我正在看的对象的位置似乎没有更新,而当我打印()它返回原始的开始位置对象而不是它们当前的位置。

在游戏窗口和检查器中,我可以看到对象(代理)正在改变位置。但事件未被触发,因为代码认为对象处于其原始起始位置。

我的代码中有一些奇怪的自然语言功能,比如“a”类和“the”类,它们保存公共变量并存在于场景中。 Self类“my”继承自具有一个变量Self class“my”的Agent类。 Self类直接在游戏对象上。我将Agent与Self分开的唯一原因是玩家可以控制场景中的任何Self类,而无需拥有自己的Self类。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Brain : Agent {

    public List<Self> sight;
    public float time;

    void Start () {
        the = my.the;
        a = my.a;
    }

    void Update () {
        // set thinking interval
        if (time <= Time.deltaTime - 0.5) {
            time = Time.deltaTime;
            Think ();
        }

        // set a random destination based on current destination
        Vector2 randomVector = Vector2.one * Random.Range(-0.1f, 0.1f);
        my.destination += randomVector;
    }

    void Think() {

        print(FindObjectOfType<Agent>().transform.position); // a test that always returns the initial starting position rather than the current position (I don't know why)

        foreach (Self their in transform.parent.GetComponentsInChildren<Self>()) {

            if (their != my) { // don't perform this action on myself

                print (my.name + "'s position is... " + my.transform.position); // returning start position not current position
                print (their.name + "'s position is... " + their.transform.position); // returning start position not current position

                if (the.Distance (my.transform.position, their.transform.position) < my.vision && !sight.Contains (their)) {

                    Limb limb = (Limb)Instantiate (a.limb);
                    limb.transform.parent = my.transform;
                    limb.transform.localPosition = Vector2.zero;
                    limb.focus = their.gameObject;
                    sight.Add (their);

                }
            }
        }
    }

对于那些在彼此视觉距离内开始的物体,肢体在游戏开始时实例化,但这应该在整个游戏中间隔更新。

非常感谢任何帮助。以下是其他类的进一步参考。

using UnityEngine;
using System.Collections;

public class Grammar : MonoBehaviour {

    public A a;
    public The the;
}

using UnityEngine;
using System.Collections;

public class A : MonoBehaviour {

    public Limb limb;
    public Node node;
    public Link link;
}

using UnityEngine;
using System.Collections;

public class The : MonoBehaviour {

    public Grid grid;

    public Structure border;
    public Structure map;

    public Player player;

    public GameObject squareParent;
    public GameObject linkParent;
    public GameObject nodeParent;

    public Color red;
    public Color yellow;
    public Color blue;
    public Color orange;
    public Color green;
    public Color purple;
    public Color gray;
    public Color darkGray;
    public Color black;
    public Color white;

    public void NewColor(GameObject gameObject, Color color) { gameObject.GetComponent<SpriteRenderer> ().color = color; }
    public Vector2 HeadingFrom(float angle) { return new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)); }
    public float AngleFrom(Vector2 heading) { return Mathf.Atan2 (heading.y, heading.x) * Mathf.Rad2Deg; }
    public Vector2 Relative(Vector2 heading, float z) { return (Vector2)(HeadingFrom(AngleFrom(heading) - z)); }
    public float Distance(Vector2 from, Vector2 to) { return (Heading(from,to)).magnitude; }
    public Vector2 Heading (Vector2 from, Vector2 to) { return to - from; }
    public Vector2 MidPoint (GameObject my, GameObject their) { return (my.transform.position + their.transform.position) / 2; }
}

using UnityEngine;
using System.Collections;

public class Agent : Grammar {

    public Self my;

    void Start () {

    }

    void Update () {

    }
}

using UnityEngine;
using System.Collections;

public class Self : Agent {

    public SpriteRenderer skin;
    public CircleCollider2D edge;
    public Rigidbody2D body;
    public Brain brain;

    public Square square;

    public float vision;
    public float speed;
    public float maxSpeed;

    public Vector2 restPosition;
    public Vector2 destination;
    public Vector2 gridPos;

    void Awake () {
        a = FindObjectOfType<A> ();
        the = FindObjectOfType<The> ();
        my = this;
        destination = transform.position;
    }

    void OnMouseDown() {
        the.player.transform.parent = transform;
    }
}

0 个答案:

没有答案