- Home /
Does anyone know why this keeps happening?
I've been working on a game for a while now where you're a UFO picking up cows. I've redone and reworked a lot of the code many times over but I keep running into the same problem. When I pick up the cows they all clump up and explode everywhere (see attached video). I'm pretty sure the explosion is because the colliders are forced to overlap. But my question is if anyone knows why the cows all shoot to the same position when I try to pick up one of them. I have 3 c# files attached to the cows:
[newFollow] - for making the cows go to an empty parented to the tractor beam: (it's activated, in theory, when the collCheck file detects a collision.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class newFollow : MonoBehaviour { public GameObject player;
// Update is called once per frame void Update() { //follow the ufo { transform.position = new Vector2(player.transform.position.x, player.transform.position.y); } } }
[collCheck] - for detecting collisions and implementing a variable to tell the NewAbduct file
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class collCheck : MonoBehaviour { public Collider beamCollider;
public static bool collIndicator; //Check if touching beam collider. public void OnCollisionEnter(Collision other) { if (other.collider == beamCollider) { Debug.Log("contact!"); collIndicator = true; } } public void OnCollisionExit(Collision other) { if (other.collider == beamCollider) { Debug.Log("disengage beam"); collIndicator = false; } } }
[NewAbduct] - for controlling the other two scripts and handling smaller movements
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewAbduct : MonoBehaviour { //References //where the cow starts public Vector2 spawnPoint;
//where the cow goes when picked up public Transform beamOrigin; //how quickly the cow turns when picked up public float turnSpeed; //whether or not the beam has touched the cow private bool contact; //whether or not the cow is currently in the beam private bool cowLifted; //the place the cow goes when it is following the ufo public GameObject player; //the collider of the beam for collision detection public Collider beamCollider; // Start is called before the first frame update void Start() { //sends the cow to the spawnpoint transform.position = spawnPoint; //resets booleans cowLifted = false; contact = false; } // Update is called once per frame void Update() { //if cow is in the beam, make it rotate. Else, reset the rotation. if (cowLifted == true) { Rotate(); } else { //reset rotation transform.eulerAngles = (new Vector3(0.0f, 0.0f, 0.0f)); } //Make the cow stay with the ufo based on contact. if (contact == true) { Follow(); } else if (contact == false) { //reset rotation and turn off newFollow script transform.eulerAngles = (new Vector3(0.0f, 0.0f, 0.0f)); gameObject.GetComponent<newFollow>().enabled = false; } //if not pressing space, set contact to false if (Input.GetKeyUp(KeyCode.Space)) { contact = false; } //reference the collCheck script to find contact data contact = collCheck.collIndicator; } //rotate the cow if picked up public void Rotate() { transform.Rotate(0.0f, 0.0f, 15 * turnSpeed * Time.deltaTime); } //follow the ufo public void Follow() { //transform.position = new Vector2(player.transform.position.x, player.transform.position.y); gameObject.GetComponent<newFollow>().enabled = true; } }
So does anyone know what's making these cows behave like this? Below are the video and a picture of the inspector for one of the cows (they're all set up the same)
The video didn't work so the picture is the aftermath. Basically what happens is on the collision, the cows all teleport (like transform.position) to the beam and then, I think because of the colliders, they all go flying.
Kindof hard to tell much with only half the context, You could try uploading the video to Imgur then adding the link. Aside from that, I'm sure someone will mention that you should be using the 2D Physics system for a 2D game, since RIgidBody2D and 2D Colliders (Circle Collider in this case) are much better suited and optimised for a 2D game and will cause less issues (You'd also need to change the OnCollisionExit/Enter functions to OnCollisionExit2D(Collision2D other)).
⠀
Without being able to see the full context of the project, I see that you have a load of Debug.Logs in your code, so from that, can you tell if every cow believes they're colliding with the beam and are acting accordingly, or is it that the cows are all following the behaviour of one cow for some reason.
⠀
A helpful tool may be the Debug.Break() function, which pauses the Unity Editor. Might be handy to put that when a cow believes they're colliding with the beam, and then exa$$anonymous$$e the scene, see if there is any collider all the cows could be mistaking for the beam.