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 and stops. See images below:
Before start of game:
After 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!
You should show the rest of your code. Where PickUpBarrel is called, and what is setting isBarrelTriggerOn
Thanks for the reply, the trigger is the big green box around the barrel.
Here’s the rest of the code that relates to the trigger:
BarrelTrigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BarrelTrigger : MonoBehaviour
{
public bool isBarrelTriggerOn = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerExit(Collider other)
{
isBarrelTriggerOn = false;
}
void OnTriggerEnter(Collider other)
{
isBarrelTriggerOn = true;
}
}
PickupBarrel is called in update:
void Update ()
{
if (!isTransitioning) // disable player control if not alive
{
RespondToRotateInput();
RespondToThrustInput();
}
if (Debug.isDebugBuild)
{
DebugMode(); // respond to cheat keys
}
PickUpBarrel();
In OnTriggerEnter you’re not checking if it is the player that entered the trigger, so any collider will cause isBarrelTriggerOn to be set to true. I’m assuming the barrel itself has a collider on it. Your terrain likely has a collider on it. That rock looking block probably has a collider on it.
Same thing for OnTriggerExit. Whenever any collider exits the trigger isBarrelTriggerOn will be set to false, regardless of what object it is that leaves the trigger.
Thanks for the reply.
Seems kind of obvious now that you have pointed that out! I’m very new to Unity and C# so i appreciate the help.
Thanks again.
Check for something on the player, for example if you tagged your player object with Player your code might look something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BarrelTrigger : MonoBehaviour
{
public bool isBarrelTriggerOn = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
isBarrelTriggerOn = false;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
isBarrelTriggerOn = true;
}
}
}