- Home /
Smooth shooting in 2d orthographic camera
Hi,
I am new to Unity with developing 2D games. I am trying to shoot bullets in X and Y directions simultaneously. I am setting gravity to the bullet so that it comes down.
I am not able to have a smooth clean shoot possible. While reaching its(bullet) peak, it starts wobbling and while coming down it wobbles vigorously !
float amountToMove = bulletSpeed * Time.deltaTime;
//Move the bullet
//transform.Translate(Vector3.up * amountToMove);//Working
//Try to fire in a parabola
float rightDir = (Vector3.right * amountToMove).x;
float upDir = (Vector3.up * amountToMove).y;
transform.Translate(rightDir, upDir, 0.0f);
I am using 'Orthographic' Projection.
I would like to have a clear parabola like motion without pauses/stoppages. Is there a way to do it ? I am referring to TheLorax's 2D game shooter tutorial.
Nope. Is there a tutorial avaliable anywhere ? If you could point me to one that would be really helpful..
I have used Unity engines' Physics RayCast and LinceCast features to detect collision..but here i am not able to figure it out !
I have gone through VTC videos while developing a First Person Shooter using 3D tools.. m facing problems and am unclear when it comes to 2D .. anyway, can you point me to some link which will help me clear my concepts on the above mentioned points ? A tutorial would definitely help. I guess i am unclear because i haven't studied everything you mentioned completely ! :)
Thanks !
Answer by Mortoc · Jun 03, 2012 at 01:34 PM
The problem is in your math. You are trying to create parabolic movement but both x and y axes are getting the same values. You want your x axis to have a constant speed and your y axis to lose height from gravity each frame.
I think you may have a different sort of bug in this code as well:
float rightDir = (Vector3.right * amountToMove).x;
float upDir = (Vector3.up * amountToMove).y;
transform.Translate(rightDir, upDir, 0.0f);
that's the same as doing:
transform.Translate(amountToMove, amountToMove, 0.0f);
Answer by BroVodo · Sep 02, 2012 at 01:48 PM
Personally I'd just add a rigid body to the bullet and use the add force function.
var bulletForce : Vector3 = Vector3( 10, 10, 0 );
function Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
rigidbody.AddForce( bulletForce );
}
}
This script would need to be attached to the bullet and the bullet would need to also have a rigid body component as well. This wouldn't be good if you were going to have lots of bullets in the scene but you should be able to get the idea.
edit: use real world sizes, makes things much easier, especially if you want to work out where the bullet lands...
Your answer
Follow this Question
Related Questions
2D: Camera Size, in Orthographic Projection vs Scale of a Plane 2 Answers
How To Make Game Remember User Input Then Play It Out, But Backwards 4 Answers
Instantiated objects do not work with Hitboxes(2D) 0 Answers
Hello Im looking for help creating a script for spike or fire damage! 1 Answer
How to calculate the amount of overlap between two 2D objects. 0 Answers