the bug should be around the Update() method, if you need info about what I’m trying to do is I want this object to launch up to the skies, if it reaches y50 it gets launched back down and if it is touching the ground it remains there until a force is applied forcing it to leave the ground, or else it just gets launched back up.
Also, the debug.logs won’t display anything, meaning that something’s wrong with the if statement and I don’t know what…
Thanks in advance! ![]()
Here’s the code:
using UnityEngine;
public class Blue : MonoBehaviour
{
private Quaternion halfExtents;
private Vector3 center = new Vector3(0, -5, 0);
private float power = -3000.0f;
private float radius = 5f;
private Vector3 minusForce = new Vector3(0, -1000, 0);
private Vector3 Force = new Vector3(0, 10, 0);
public GameObject explosionEffect;
private Rigidbody RigidBody;
private ConstantForce constantforce;
[SerializeField] private LayerMask GroundMask;
[SerializeField] private Transform GroundCheckerTransform;
private void Start()
{
RigidBody = GetComponent<Rigidbody>();
constantforce = GetComponent<ConstantForce>();
}
private void Update()
{
//if "blue" is touching on the ground...
if (Physics.CheckSphere(GroundCheckerTransform.position, 1f, GroundMask))
{
//we set the constant force to 0..
constantforce.force = Vector3.zero;
//and set gravity to true.
RigidBody.useGravity = true;
Debug.Log("it's touching the ground");
}
//or...
else
{
//we set gravity to false..
RigidBody.useGravity = false;
//we add the default constant force back..
constantforce.force = Force;
//and if "blue" is above y 50...
if (transform.position.y >= 50f)
{
//we try throwing it back down..
constantforce.force = minusForce;
//"blue" explodes..
GameObject explosionInstance = Instantiate(explosionEffect, transform.position, transform.rotation);
//and affects other rigidbodies in range..
Collider[] colliders = Physics.OverlapBox(transform.position, center, halfExtents);
foreach (Collider nearbyObject in colliders)
{
var nearbyBody = nearbyObject.GetComponent<Rigidbody>();
if (RigidBody != null)
{
RigidBody.AddExplosionForce(power, transform.position, radius); //this adds an explosion force.
}
}
Destroy(explosionInstance, 1); //destroys the particles game object.
}
else
{
constantforce.force = Force;
Debug.Log("HELOO??");
}
}
}
}