Paweł Mach
Paweł Mach

Reputation: 745

Why is position of my object not changing?

I am currently developing a Unity3D based evolutionary algorithm. The simulation is two-dimensional. A single subject is being depicted as car, being a prefab, consisting of 3 sprites(2 wheels and body), and a CarScript. Each sprite has a proper collider(BoxCollider2D for body and CircleCollider2D for wheels). CarBody also has two WheelJoint2D. Parameters of those colliders are changed by code.

I want this car to be destroyed, if it stops moving or better - advancing. In the Game window, the car is obviously moving downhill. The problem is, that after checking for transform.position of gameobject, this value seems to be constant. It always shows the position of SpawnPoint. SpawnPoint is empty GameObject with SpawnScript, which fragment is below:

public GameObject carprefab; //Set from editor
public Transform[] spawnPoints; //transform of SpawnPoint, just one element. Set from editor.
private void GenerateCar(Chromosome chromosome)
{
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
var instace = (GameObject)Instantiate(carprefab, spawnPoints[spawnPointIndex].position,     spawnPoints[spawnPointIndex].rotation);
    instace.SendMessage("SetChromosome", chromosome);

    foreach (Transform child in instace.transform)
    { //code omitted for clarity, changes some of parameters based on chromosome.

Instantiated object has a CarScript:

 // Update is called once per frame
    void Update()
    {
        if (frames%10 == 0)
            CheckForMoving();
        frames++;
    }
 private void CheckForMoving()
    {
        var pos = gameObject.transform.position; //this is never changing - why?


        var diff = pos - startingPosition; //difference between local position and starting position
        if (CountLength(diff) >= recordLengthYet)
        {
            Debug.Log("It is moving!");
            recordLengthYet = CountLength(diff);
            framesTillLastRecord = 0;
        }
        else
        {
            Debug.Log("It is not moving");
            if (framesTillLastRecord > 4)
                Destroy(gameObject, 0f);
            framesTillLastRecord++;
        }
    }

I tried getting the position by any of the following:

var pos = gameObject.transform.position;
var pos = GameObject.FindGameObjectWithTag("player");
var pos = this.transform.position;

The question is - what did I miss, or why this is not changing? I started learning Unity just recently, and had no previous experience with any similiar software. I also wonder, if it is even the right way to do this.

Upvotes: 3

Views: 1945

Answers (2)

Paweł Mach
Paweł Mach

Reputation: 745

Few days has passed, nobody had uploaded a proper answer, and I managed to get a workaround. As long as it is really dirty, it seems to work.

I placed a tag on child GameObject, instead of prefab, and I am geting position of this child by

var pos = GameObject.FindGameObjectWithTag("player");

I am not sure, if this is really good answer, but I hope somebody might find it useful someday.

Upvotes: 3

user585968
user585968

Reputation:

Assuming you are using rigidbodies, you should be overriding FixedUpdate() and not Update() as per Unity doco:

This function is called every fixed framerate frame, if the MonoBehaviour is enabled. FixedUpdate should be used instead of Update when dealing with Rigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update. - Tell me more...

Replace:

 // Update is called once per frame
void Update()
{
    if (frames%10 == 0)
        CheckForMoving();
    frames++;
}

With:

 // Update is called once per frame
void FixedUpdate()
{
    if (frames%10 == 0)
        CheckForMoving();
    frames++;
}

Upvotes: 0

Related Questions