- Home /
Shoot projectile according to rotation
I have been trying for hours and I can't get a projectile to shoot the way it is facing. The projectile is only shooting directly forward. I need it to shoot up into the air. I have its rotation set to the rotation of the camera, but how do I add force in that direction.
Thank you in advance.
function Fire(){
var power:int;
power=0;
while(Input.GetButton("Fire1")){
power++;
Debug.Log("Power"+power, gameObject);
yield WaitForSeconds(0.1);
}
power = power *5;
var angle = Vector3(cam.transform.eulerAngles.x,cam.transform.eulerAngles.y,cam.transform.eulerAngles.z);
// Instantiate the projectile at the position and rotation of this transform
var clone : Rigidbody;
//X,Y,Z
var positionn = Vector3(transform.position.x, transform.position.y+1, transform.position.z);
clone = Instantiate(projectile, positionn, transform.rotation);
//rotate
clone.transform.eulerAngles= angle;
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * power);
}
Answer by SergeantBiscuits · Sep 30, 2012 at 05:30 PM
Try this. When you're setting the clone's velocity (the very last line), use:
clone.velocity = (transform.rotation * Vector3.forward) * power
transform.rotation will give you a quaternion, and multiplying it by Vector3.forward will give you it's normalized 'forward' component in Vector3 form (or at least that's my vague understanding of quaternion...)
I gave it a shot and this does not work. it does exactly the same as clone.velocity = transform.TransformDirection (Vector3.forward * power);
Answer by sarthakshah · Aug 19, 2014 at 07:18 AM
I think below post will help you.
http://answers.unity3d.com/questions/656781/unity3dprojectilearrow-of-bow-is-not-rotating-as-p.html