Object moves when game starts, but shouldn't until player collides with trigger
I'm having a strange issue that I can't get to the bottom of. I know what is causing it but I don't know why.
I have a fuel barrel that moves up towards the player ship over time when the player collides with a trigger around the fuel barrel. If the player exits the collider before the fuel barrel collides with it then the barrel falls back to the ground. I'm using gravity enable / disable so the gravity is enabled until the player collides with the trigger (allows barrel to move up to the player) and then disabled when not colliding (allows barrel to fall back down).
Now this all works fine apart from one thing. When the game is started the barrel moves from it's original position slightly towards the player. See images below:
Before start of game:
When game starts:
The issue is with the transform part of the barrel pick up method that I'm using but I don't know why, here's the code:
private void PickUpBarrel()
{
if (barrelTrigger.isBarrelTriggerOn == true)
if (fuelBarrel != null)
{
{
fuelBarrel.GetComponent<Rigidbody>().useGravity = false;
fuelBarrel.transform.position = Vector3.MoveTowards(fuelBarrel.transform.position, this.gameObject.transform.position, 5 * Time.deltaTime);
}
}
if (barrelTrigger.isBarrelTriggerOn == false)
{
fuelBarrel.GetComponent<Rigidbody>().useGravity = true;
}
}
Any help greatly appreciated!
Cheers
Your answer
Follow this Question
Related Questions
How do I trigger a game object to move a desired distance at a desired speed? 1 Answer
Spawning my prefab on a transform point on TriggerEnter 0 Answers
Animation transition problem 1 Answer
,Use a trigger to throw my character in the air 0 Answers
Creating solid meshes and turning them into permeable with trigger 0 Answers