"Uniy 2D Beat 'Em Up, how to do collision?" (526728)

Hi there. I am currently working in a 2D beat 'em up using Unity 4.3 Native 2D tools. I am basing my game on Castle Crashers and I am completely stumped on how to do collision like they do. Any help will be much appreciated. Thank You.

hm that would interrest me too since i only work with sprites in my current 2d project :confused:
well i’m currently useing a distance check via c# to detect collisions but thats not good

Are you using a Rigidbody2D for collision detection? That’s what I’m using and so far I can have the player move around in X and Y axis. My problem is that in Castle Crashers, you are able to fall from ledges or go up and down stairs, like if the game was 3D.

nope… i wrote into the void update() of my c# ship script and use something like this:

float maxRange = 1f; //insert the min range between 2 objects for collision
if(Vector2.Distance (new Vector2(GameObject.Find (“PlayerShip-Shot”).transform.position.x, GameObject.Find (“PlayerShip-Shot”).transform.position.y), new Vector2(transform.position.x, transform.position.y)) < maxRange){ //collide }

well as i said: not good…

but hm :confused: you can detect collisions with rigidbody2d ?

edit:
look here

they say you can detect collisions - but how ://

If a GameObject has a Rigidbody2D with the “Is Kinematic” property checked off and a collider, it’ll collide with other colliders that do not have the “Is Trigger” property checked on.

yea well i meant only rigidbody2d without any other collider…

You need a collider on it… so it can collide with things.
The Rigidbody just allows for the object to have forces applied to it, like gravity and movement.

For a game like Castle Crashers, you want to use the 3D toolset for most things, not the 2D toolset. The only thing really you’ll be using from the new 2D toolset is the Sprite Renderer and management.

You’ll create an empty Game Object, with a regular Physics Cube Collider and Rigidbody (not 2D). Then your sprite will be a child of this.
You’ll make your ground out of cube or flat mesh colliders. Your Camera will be set to Orthographic and be angled downward slightly.

Making a game like that will take a fair amount of coding knowledge though, you should definitely practice some more. You’re doing some bad things in that last code you posted.

  1. Don’t use “GameObject.Find()” in Update() or FixedUpdate(), or anything that’s going to be called nearly every frame. It’s bad for performance. If the object you’re trying to find is going to exist in the scene for a long time, just call it once in void Start(). Make a variable for it before as well, for example: private GameObject playerShip;. Then in Start(): playerShip = GameObject.Find("PlayerShip-Shot");

  2. Define your variables in the beginning area of your script, before Start() is usually shown. You can always modify the value at anytime within the script if you feel like it (or through the Inspector if it’s declared public).

  3. If you were already working with 2D sprites in those vectors, then you didn’t need to manually declare a new Vector2 with it’s 2 axis, you could of simple just put if(Vector2.Distance(playerShip.transform.position, transform.position) < maxRange)
    (also, if a Vector3 position is given as input, it still works, and merely ignores the .z axis that was also given.)

yea well its because it creates a copy of a deactivated bullet, activate it and then moves it, if it goes out of screen it get removed, but im not sure how to detect if the gameobject variable is set or not.

oh thx dont knew it could made so easy.

hm well i got confused because the settings:

Use Instantiate instead, and you will automatically have a variable with the instantiated object that’s in use, to destroy.

An example of it’s use:

//This will contain the currently instantiated object
	private GameObject firedBullet;
//assign your bullet or w/e Prefab to this in the Inspector
	public GameObject bullet;
void Update()
{
		if (Input.GetKeyDown ("u"))
		{
			firedBullet = Instantiate(bullet, transform.position, Quaternion.identity) as GameObject;
			firedBullet.transform.position = firedBullet.transform.forward * 2f;
		}
		if (Input.GetKeyDown ("i"))
		{
			DestroyImmediate(firedBullet, true);
		}
}

Well, it mentions in the description about a collider on the object that contains this rigidbody. Rigidbodies apply physics and do calculations based on applied collider. Colliders determine collision boundaries.

well and how can i detect if the variable is set ?

What do you mean? It’s set by the Instantiation of the bullet itself.

But if you absolutely need to know for something else, you can just use: if (firedBullt != null )

To detect colisions, I use rigibody 2d with box colider 2d my gravity scale as set to 0, kinematica is off and fixed angle is true…