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

  1. Side scroller ( meant that the view is 2D).
  2. 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.

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.