- Home /
Rotation Projectile Script Difficulties
Hi
I created a cylinder game object to sample as an arrow. When I run the script the projectile instantiates vertically (which seems to be a common problem for some), instead of firing straight.
This script is attached straight on my character (which currently is just a capsule) and is called by pressing a touchscreen firebutton. The arrow prefab currently has no other scripts attached to it. I've also attempted to adjust the prefab in the proper direction to no avail.
My efforts are in the following code:
var projectile : Rigidbody;
var speed = 20;
var fireitem : GameObject;
var target : Transform;
var ammoCount = 20;
var reloadTime = 0.5;
private var lastShot = -10.0;
function fire () {
if (Time.time > reloadTime + lastShot && ammoCount > 0) {
var misslelocation = fireitem.transform.position;
var misslerotation = transform.rotation;
// var misslerotation = Quaternion.FromToRotation(Vector3.up, -transform.forward);
// var misslerotation = Quaternion.Euler(90, 0, 0);
// var launchbullet : Rigidbody = Instantiate (projectile, misslelocation, gameObject.transform.rotation );
// var launchbullet : Rigidbody = Instantiate (projectile, misslelocation, transform.rotation );
var launchbullet : Rigidbody = Instantiate (projectile, misslelocation, misslerotation);
launchbullet.transform.LookAt(target);
launchbullet.rigidbody.AddForce ( launchbullet.transform.forward * speed);
Physics.IgnoreCollision( launchbullet. collider, transform.root.collider );
lastShot = Time.time;
ammoCount--;
}
}
In the code, I've left in some different approaches that I've tried from other users issues with projectile rotation within this site.
Could anyone assist me or point me in the right direction of why my projectile will not fire properly?
Thanks
Are you sure it's actually wrong? Does the shaft of the arrow point along the arrow's z axis?
No it doesnt, but it does as a prefab. I've tried to adjust it through Quaternion.Euler and a FromToRotation approach. Each try, it still shoots in the vertical position. Is there another area that you know of that I should check?
Thanks.
Also, I am using the Standard Assets $$anonymous$$obile - Player Relative Setup (3rd Person) with Unity Android, if any of this makes a difference. In the Unity editor and android application that I've pushed it to, the response is the same.
Where is 'target' relative to the spawn position? I can't see anything obviously wrong with the script, so I'm really just throwing troubleshooting questions at you to see if there's something odd about your setup...
The target is an empty game object that moves around based on a raycast from the my camera that shoots towards the center of the screen. In the code above, the empty target game object would be the variable "target". The spawn position is on the character controller named "fireitem", also in the above code.
Answer by DBar · Aug 19, 2014 at 07:50 AM
When theory fails, you have TRIAL & ERROR, try this before launching and pause, i dont understand where is the LAUNCH origin so this is rough but it works on my ship:
var ShootDirection = launchbullet.transform.forward; // To try the SHOOT DIRECTION
Debug.DrawRay (misslelocation.transform.position, misslelocation.transform.worldToLocalMatrix.MultiplyVector(ShootDirection) * 30, Color.green);
Debug.Break();
Your answer
![](https://koobas.hobune.stream/wayback/20220613063100im_/https://answers.unity.com/themes/thub/images/avi.jpg)