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!
P.S. I’ll post any code on request, I just didn’t know which code snippets you were looking for.
The Player:
public class Player : MonoBehaviour
{
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 || 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);
}
}
Clone Instantiation:
public class Obsticle : MonoBehaviour
{
public GameObject player;
public GameObject particles;
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();
if(height == 1 || height == 4)
{
GameObject clone = Instantiate(stump);
clone.transform.position = new Vector3(0.94f, -1.16f, -11.3f);
clone.name = "Obsticle";
clone.tag = "Stump";
}
if (height == 2)
{
GameObject clone = Instantiate(rock);
clone.transform.position = new Vector3(0.27f, -0.3596388f, -11.3f);
clone.name = "Obsticle";
clone.tag = "Rock";
}
if (height == 3)
{
GameObject clone = Instantiate(fence);
clone.transform.position = new Vector3(-0.84f, -0.73f, -11.3f);
clone.name = "Obsticle";
clone.tag = "Fence";
}
}
}
The Clone Itself:
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);
}
}