- Home /
Unintentional Occasional Collision
I'm working on a game where you try not to run into obstacles. I have main issue with it however, and that is that some instantiated clones that the player collides with don't activate OnCollisionEnter() on either script. (I have a different script with OnCollisionEnter() on the player and the obstacle, and neither are activated.) Why is it doing this? Again I don't think it's an issue with a single piece of code, seeing as I have two separate scripts with OnCollisionEnter(), and neither of them are being called.
Thanks a Million!
Player Code: public class Player : MonoBehaviour { public GameObject button;
public Rigidbody rb;
public MeshRenderer render;
public GameObject particles;
public Material material;
float jumpSpeed = 15;
float moreGravity = 13;
float gravityMultiplier = 6;
float jumpLength = 0;
float torque=1000;
public bool jump = false;
public bool gamePlaying = true;
bool madeFaster = false;
bool touchingGround = true;
private void Start()
{
material.color = Random.ColorHSV();
render.enabled = true;
Physics.gravity = new Vector3(0, -9.81f * gravityMultiplier, 0);
}
void Update ()
{
if (gamePlaying == true)
{
if (touchingGround == true)
{
if (jumpLength <= 8)
{
if ((Input.anyKey && button.GetComponent<Button>().touchingButton == false) || jump)
{
if (jumpLength == 0)
rb.AddTorque(-1*torque,0f, 0f);
rb.velocity = new Vector3(0,1,0)* jumpSpeed;
jumpLength++;
}
else if (jumpLength > 0)
touchingGround = false;
}
else
touchingGround = false;
}
if (touchingGround == false)
{
if (rb.velocity.y < -0.1 && madeFaster == false)
{
Physics.gravity = new Vector3(0, moreGravity * -1 * gravityMultiplier, 0);
madeFaster = true;
}
}
}
}
private void OnCollisionEnter(Collision collision)
{
touchingGround = true;
if (collision.gameObject.tag == "Fence" || collision.gameObject.tag == "Rock" || collision.gameObject.tag == "Stump")
{
gamePlaying = false;
Colapse();
}
if (collision.gameObject.name == "Ground")
{
jumpLength = 0;
Physics.gravity = new Vector3(0, -9.81f * gravityMultiplier, 0);
madeFaster = false;
rb.rotation = Quaternion.identity;
}
}
void Colapse()
{
render.enabled = false;
Quaternion rotation = new Quaternion();
rotation.x = 0;
rotation.y = 0;
rotation.z = 0;
Destroy(Instantiate(particles, rb.position, rotation), 2);
}
}
Cloned Obstacle Code: public class ObsticleClone : MonoBehaviour {
public GameObject obsticle;
public GameObject particles;
public bool changedScore = false;
float time;
public float speed;
private void Start()
{
particles = obsticle.GetComponent<Obsticle>().particles;
speed = obsticle.GetComponent<Obsticle>().speed;
}
void Update ()
{
if (name == "Obsticle")
{
time = obsticle.GetComponent<Obsticle>().time;
this.gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + speed + (time / 200f));
if (gameObject.transform.position.z >= 14.17f)
{
obsticle.GetComponent<Obsticle>().clone = true;
Destroy(this.gameObject);
}
if (gameObject.transform.position.z >= 8.78f && !changedScore)
{
changedScore = true;
obsticle.GetComponent<Obsticle>().score++;
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Player")
Colapse();
}
void Colapse()
{
Renderer r = this.GetComponent<Renderer>();
Collider c = this.GetComponent<Collider>();
c.enabled = false;
r.enabled = false;
Destroy(Instantiate(particles, this.gameObject.transform.position, this.gameObject.transform.rotation), 2);
Destroy(this.gameObject);
}
}
Again, thank you so much!
...some instantiated clones that the player collides with don't activate OnCollisionEnter()
Are you saying that sometimes it works and sometimes it doesn't?
Yeah, it will sometimes collide correctly, but sometimes it doesn't.
Have you tried changing the collison detection on the rigid body to continuous?
It's also not an issue with changing the collision-detection type to Dynamic or Continuous Dynamic, I tried it already.
Answer by Jehy · Jul 14, 2018 at 04:54 AM
Do the "ObsticleClone" GameObjects spawn with Colliders? I see you're trying to access a Collider in the "Colapse" method but I can't see them being added to the GameObject anywhere.
Sorry, I forgot to add the ObsticleCloned script. It's right here: public class Obsticle : $$anonymous$$onoBehaviour { public GameObject player; public GameObject particles; public $$anonymous$$aterial particleColor; public GameObject fence; public GameObject rock; public GameObject stump;
public Rigidbody obsticleRigidbody;
public Text scoreText;
public float speed;
public float height;
public float random;
public float score = 0;
public float time;
public bool clone = true;
void Update()
{
time += Time.deltaTime;
scoreText.text = score.ToString();
if (clone == true)
{
if (player.GetComponent<Player>().gamePlaying == true)
{
clone = false;
instantiation();
}
}
}
void instantiation()
{
System.Random rnd = new System.Random();
height = rnd.Next(1, 4);
Quaternion q = new Quaternion();
Color color = new Color(86,66,0);
if(height == 1)
{
color.r = 86;
color.g = 66;
color.b = 57;
color.a = 0;
GameObject clone = Instantiate(stump);
clone.transform.position = new Vector3(0.94f, -1.16f, -11.3f);
clone.name = "Obsticle";
clone.tag = "Stump";
}
else if (height == 2)
{
color.r = 175;
color.g = 175;
color.b = 175;
color.a = 0;
GameObject clone = Instantiate(rock);
clone.transform.position = new Vector3(0.27f, -0.3596388f, -11.3f);
clone.name = "Obsticle";
clone.tag = "Rock";
}
else
{
color.r = 231;
color.g = 143;
color.b = 49;
color.a = 0;
GameObject clone = Instantiate(fence);
clone.transform.position = new Vector3(-0.84f, -0.73f, -11.3f);
clone.name = "Obsticle";
clone.tag = "Fence";
}
particleColor.color = color;
}
}
Since none of your collisions are working (and I can't see the inspector), I think your objects don't have Collider components on them. Do you have any Collider type components on your GameObjects: box colliders, capsule colliders, mesh colliders, etc?
Yes, everything that needs to collide has a collider on it (Either mesh or box). Again, they do work like 50% of the time, but they don't work the other 50%. It's also not because I'm not instantiating the collider sometimes, I checked: It always makes a collider. It's just for some reason, the event OnCollisionEnter() and the physics engine doesn't register them colliding sometimes.
Your answer
![](https://koobas.hobune.stream/wayback/20220612170652im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NavMesh Collision Detection? 0 Answers
Collision Dection on Tigger Enter 1 Answer
Last collision 1 Answer
How to detect collision on only one side of an object? [C#] 4 Answers
Best collision detection method? 2 Answers