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.