- Home /
Top down 2D shooter shell collision with ground
Hi, how do I go about bullet casing colliding with the ground? Since it is a top down game gravity does not apply to the player but bullet casings, grenades and other projectiles do. So basically the casing will collide on the floor where it was ejected.
Any help is appreciated!
Answer by springwater · Jul 09, 2017 at 08:40 AM
You could just provide a coordinate, like if the casing is further than -5 on the x axis, bounce.. and destroy. or check for collision, make it a rigid body and the ground as well, add on collision with ground function..add force to the ridid body as necessary if gravity won't be appropriate.
So how do I code the game object to bounce at a certain x,y point? Also where would i input it? on the instantiate function?
Ok, so I added an if statement on the shell ejection script. So if transform.position.y is less than -1 it will destroy. But it is not relative to players position, its relative to world space. So, if my player is below -1 the shell will not instantiate. Basically I want the transform.position.y = 0 whereever it was instantiated.
Also i played around with addforce to simulate bounce effect. It does not simulate the same effect as using collision. Is there another way to achieve the effect?
Just to clarify it's a top down side(right to left) shooter. If this info helps.
if it is a 3d game, you can just code it to say if the position is less than the players position by x amount, destroy.. or bounce and destroy.. (I'd have the bounce/destroy on impact in a 3d game.) bounce can be achieved by adding a negative force, with forcemode -force being used as an optional parameter. The shell needs a rigid body, I'm not an expert on the 2d Unity set up, but if you are using that, I imagine you can just scale your casings down to simulate falling..
the code would look like.. var x=0; var y=0; var z=1.0; var player:GameObject; var speed=5.0;
if (transform.position.z< player.position.z-1 ) Destroy(gameObject); //or for bounce ... GetComponent.().AddForce(transform.forward* -speed, Force$$anonymous$$ode.Force);
... trying to apply force to transform.forward may not work in the 2d setup. in which case, scaling the object down as I said before may be a solution. This code assumes your negative z vector is down. It doesn't factor in other forces you should apply to give the casing initial velocity away from the weapon.. But it should be easy by studying this code and applying it for other directions.. best to read the Unity docs on all this and become very familiar with it anyways. And get comfortable referencing them.
how do i reference the player position? to my Shell object? do I put the code on the player script or shell script?