dBlue
July 25, 2015, 11:03pm
1
When the two objects collide, instead of one being destroyed, they both touch each other. I’m not sure what’s wrong, is there something in my code?
using UnityEngine;
using System.Collections;
public class Upward : MonoBehaviour {
public float StartingPosition;
public Vector3 AntiGravity;
public bool FloorTouch = false;
void FloorCheck (Collision col){
if (col.gameObject.tag == "Floor") {
FloorTouch = true;
} else {
FloorTouch = false;
}
}
void Start () {
float RandomFl = Random.Range(-0.5f, 0.5f);
float StartP = StartingPosition;
transform.position = new Vector3 (RandomFl, StartP, -1f);
gameObject.GetComponent<Rigidbody2D>().freezeRotation = true;
}
void Update () {
if (transform.position.y >= 4) {
float RandomFl = Random.Range (-0.5f, 1f);
transform.position = new Vector3 (RandomFl, -4f, -1f);
gameObject.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
transform.rotation = Quaternion.identity;
}
if (!FloorTouch) {
Destroy (gameObject);
}
transform.position += AntiGravity;
}
}
It doesn’t seem like you’re actually making use of the collision event.
Consider replacing FloorCheck with OnCollisionEnter, (using the same method contents). OnCollisionEnter is automatically called when two colliders hit each other, and it uses Collision as a parameter too.
You could also call FloorCheck from the other object if it has an OnCollisionEnter method, passing the collision info, but I would advise letting your Upward class handle itself in its own method.
If you plan to use FloorCheck independently elsewhere, then I recommend adding OnCollisionEnter, and inside of that making a call to FloorCheck.
Umm this is your script. It was missing the OnCollisionEnter… If this is right then make sure you check the above peoples answers as right because i just put theirs into your script.
using UnityEngine;
using System.Collections;
public class Upward : MonoBehaviour {
public float StartingPosition;
public Vector3 AntiGravity;
public bool FloorTouch = false;
void OnCollisionEnter(Collision collision) {
FloorCheck(collision);
}
void FloorCheck (Collision col){
if (col.gameObject.tag == "Floor") {
FloorTouch = true;
} else {
FloorTouch = false;
}
}
void Start () {
float RandomFl = Random.Range(-0.5f, 0.5f);
float StartP = StartingPosition;
transform.position = new Vector3 (RandomFl, StartP, -1f);
gameObject.GetComponent<Rigidbody2D>().freezeRotation = true;
}
void Update () {
if (transform.position.y >= 4) {
float RandomFl = Random.Range (-0.5f, 1f);
transform.position = new Vector3 (RandomFl, -4f, -1f);
gameObject.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
transform.rotation = Quaternion.identity;
}
if (!FloorTouch) {
Destroy (gameObject);
}
transform.position += AntiGravity;
}
}
You could do that or…
using UnityEngine;
using System.Collections;
public class Upward : MonoBehaviour {
public float StartingPosition;
public Vector3 AntiGravity;
public bool FloorTouch = false;
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "Floor") {
FloorTouch = true;
} else {
FloorTouch = false;
}
}
void Start () {
float RandomFl = Random.Range(-0.5f, 0.5f);
float StartP = StartingPosition;
transform.position = new Vector3 (RandomFl, StartP, -1f);
gameObject.GetComponent<Rigidbody2D>().freezeRotation = true;
}
void Update () {
if (transform.position.y >= 4) {
float RandomFl = Random.Range (-0.5f, 1f);
transform.position = new Vector3 (RandomFl, -4f, -1f);
gameObject.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
transform.rotation = Quaternion.identity;
}
if (!FloorTouch) {
Destroy (gameObject);
}
transform.position += AntiGravity;
}
}