- Home /
2D top down shell ejection
Hi, I've been trying to figure this out for days. Some codes I've tried work but not as quite as i want it to work. I have the shell ejection working fine when I use transform.translate but when i use AddForce the shells just floats to the left. I finally have the shell to rotate on its own axis as other codes i have tried would rotate it according to the position of its parent, which shows the shell floating all over the place. I also have a box collider comp on the shell but set as is triggered so it would not collide with the player collider. Not sure if that matters.
What am I doing wrong?
Any help will be appreciated!
My code:public class ShellEjectionCtrl : MonoBehaviour {
public float ejectSpeed;
public float rotspeed;
void FixedUpdate(){
transform.Translate(new Vector2 (-2,ejectSpeed)*Time.deltaTime); //this code works properly the shell gets affected by gravity but rotates weird like it has a point of rotation.
GetComponent<Rigidbody2D> ().AddForce (new Vector2(-2,ejectSpeed));//this code makes the shell float to the left position but shell rotates properly
GetComponent<Rigidbody2D> ().AddTorque (Random.Range (0, rotspeed));//rotates the shell not relative to its position i.e ejection port of the rifle
Destroy (gameObject, 2.0f);
}
}
Answer by FortisVenaliter · Jul 09, 2017 at 03:24 PM
Well, it's physics. When a shell is ejected, it only gets the force applied as it's actually ejected, not when it's flying through the air. You need to add the force and torque at the moment of ejection, and just let the physics system handle it from there.
So how would I go about it? Should I modify the Instantiate function?
Wasn't very clear but it led me to the right direction. Finally working how I wanted it to work. Thank You so much!
Your answer
Follow this Question
Related Questions
RotateAround using physics 1 Answer
Why is the Unity 2D physics engine only deterministic when Z axis rotation is frozen? 0 Answers
I'm attempting to make a character face the joystick in a top down unity2d game. 1 Answer
Look for the player isn't working 1 Answer
2D Directional top down movement,Topdown 2d Directional Movement 0 Answers