Hi guys,
I have some pickups in my game, which are supposed to move towards the player after being picked up. Here is GemPickup.js:
`
#pragma strict
var scoreValue : int = 1;
private var target : GameObject;
private var pickedUp = false;
function Start () {
target = GameObject.FindGameObjectWithTag("Player");
}
function OnTriggerEnter (other : Collider) {
// Make it go towards the penguin and disappear
pickedUp = true;
Debug.Log("Gem collected!");
}
function Update () {
if (pickedUp)
{
// Move the gem towards the player
transform.position = transform.position - target.rigidbody.position * Time.deltaTime;
Debug.Log("Moving gem to player...");
//Remove from game environment after being collected
//TODO: Add scoring system
if (transform.position == target.rigidbody.position)
{
Destroy(this);
Debug.Log("Add score");
//TODO: take scoreValue and add it to the player's score
}
}
}
`
The OnTriggerEnter() code works just fine, since the the debug log message gets printed. However, although the fact of getting picked up is being detected in Update(), the gems just never seem to meet the player, preferring to move in bizzare directions instead. What gives?
MachCUBED