- Home /
Angle and Velocity in a Projectile
Hello,
I am trying to input angle and velocity with which the cannon should fire in a projectile fashion. The prototype is
Side scroller ( meant that the view is 2D).
The canon has to be placed on the left most corner and fire in a Projectile Fashion so that the bullet lands within the screen (no camera movement).
Game Objects :
I have placed the "First Person Controller" from the standard assets and placed the following script on the camera. My assumption was to shoot from the camera. (I hope I am not colliding within the "First Person Controller".
Code :
var canonPrefab : Transform;
var speed = 555.0f ;
var angle = 90.0f;
function Update()
{
var elevationAngle = Vector3(0,0,angle);
if(Input.GetButtonDown("Fire1"))
{
var canon = Instantiate (canonPrefab, transform.position, Quaternion.identity);
canon.rigidbody.AddForce( Quaternion.Euler(elevationAngle)*transform.forward * speed);
}
}
Code Explanation :
As far as the angle is concerned, I have received the I/P as a float then parsed it to a Vector 3 variable and used Quaternion.Euler function. I believe transform.forward is to move the Prefab along the z axis. I also think that I have rightly used ForceMode.Impulse.
Problem :
No matter how I alter the angle, the canon fires at the very same angle. I assume the angle at which it is fired (projected) is just 'zero'.
Request :
My humble request is could someone help me out. I have been working around and I think I am stuck. My main aim is to I/P the velocity and angle through GUI and then fire the canon with respect to the I/P given. My another issue is (not concern right now), I would like to I/P with real time velocity values but with the I/P I give in, it seems to be not in relation with the Velocity in real life.
Thank you for your patience.
Answer by aldonaletto · Dec 02, 2011 at 04:14 AM
The whole thing is somewhat confusing: why are you using a First Person Controller in a 2D side scroller game? The FPC shows the world from its own point of view, while in a side scroller game the world is showed from an external point of view.
I would place the cannon in the left side, and shoot to the right (Vector3.right); instead of use AddForce, I would set the rigidbody.velocity directly - AddForce depends on the projectile mass and the time during which the force is applied. With these settings, the script would be attached to the cannon:
var canonPrefab : Transform; var speed = 10.0f ; var angle = 45.0f;
function Update(){ if(Input.GetButtonDown("Fire1")) { var canon = Instantiate (canonPrefab, transform.position, Quaternion.identity); var shootDir = Quaternion.Euler(0, 0, angle) Vector3.right; canon.rigidbody.velocity = shootDir speed; } } NOTE: In this case, the camera would be aiming in the Z direction, thus the space showed in the screen would be the YX plane.
Perfect. Thank you very much. I highly appreciate your help. I had a turret model and I happen to add it at the end. It works. I just got one doubt. The velocity which I I/P - is it at m/s?
Yes, the velocity is in m/s - Unity uses the $$anonymous$$$$anonymous$$S system (metro/kg/seconds).
If i want to move the projectile along x-axis at a particular anlge say 30 degrees, what i thought was required var shootDir = Quaternion.Euler(30, 0, 0) * Vector3.right; //because i am assu$$anonymous$$g the projectile is making 30 degrees angle //with x-axis. Please correct me if i am wrong.
but here we are specifying the angle along the z-direction i guess. How does the angle get deter$$anonymous$$ed ? I mean what is the reference ? How do i understand 'with' which axis the angle is established y the projectile and how ?
In the case above, Y is the vertical axis and X is the horizontal axis. The axis Z runs forward from the camera, thus in order to shoot along the X axis we must define an angle around Z, like below:
Thanks. That helps a lot. Now I am trying to shoot the projectile along the z-axis and i have written my code as:
var shootDir = Quaternion.Euler(45 ,0, 0) Vector3.forward; // transform.rigidbody.velocity = shootDir 10; transform.rigidbody.AddForce(shootDir * 50);
but that isn't working ! Rotation along x-axis and forward direction should accomplish this i thought.
Your answer
Follow this Question
Related Questions
Increase velocity without changing trajectory 1 Answer
Projectile not firing right - simple angle and velocity problem? 2 Answers
Projectile trajectory velocity and angle instead of force with a rigidbody 2 Answers
Projectile launch insufficient velocity 2 Answers
Velocity and AddForce Problems 0 Answers