- Home /
Bullet Rotation faces wrong direction
My shooting script does what it needs to do in terms of shooting a rigidbody bullet forward , but the bullet always faces the Z axis. I don't want the bullet to always face the Z axis, I want it to face forward as normal bullets do. Here's an example...
My code here...
#pragma strict
@script RequireComponent(AudioSource)
var soundeffect : AudioClip;
var soundeffect2 : AudioClip;
var Effect : Transform;
public var TheDamage = 50;
var shootdistance : int = 500;
var bullets : int;
var fullmag : int = 7;
var reloadtime : float = 2.8;
var onetime : boolean = false;
var timer : float = 0;
var fireRate : float = 0.15;
var Spawn : Transform;
var bulletPrefab : Transform;
var shootForce : float = 300;
function Start()
{
bullets = fullmag;
light.intensity = 0;
}
function Update () {
timer += Time.deltaTime;
if (timer > fireRate){
if (Input.GetButtonDown("Fire1")&& bullets > 0){
bullets --;
timer = 0;
audio.clip = soundeffect;
audio.Play();
light.intensity = 4;
Invoke ("lightoff",0.25);
fire();
var particleClone = Instantiate(Effect, Spawn.position, Quaternion.identity);
Destroy(particleClone.gameObject, 1);
}
}
if (bullets <= 0 && !onetime){
audio.Stop();
audio.clip = soundeffect2;
audio.Play();
Invoke ("reload",reloadtime);
onetime = true;
}
}
function fire(){
audio.clip = soundeffect;
audio.Play();
var instanceBullet = Instantiate(bulletPrefab,Spawn.position, Quaternion.identity);
bulletPrefab.rigidbody.useGravity = false;
instanceBullet.rigidbody.AddRelativeForce(transform.forward*shootForce);
}
function lightoff(){
light.intensity = 0;
}
function reload(){
bullets = fullmag;
Invoke("canreload",reloadtime);
}
function canreload(){
onetime = false;
}
function OnGUI()
{
if ( bullets > 0){
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 25 ), "Ammo: " + bullets + "/" + fullmag);
}
else{
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 25 ), "Reloading" );
}
}
Answer by JusticeAShearing · Sep 07, 2014 at 04:40 PM
I recommend getting the player rotation at the time of firing, and scripting, at the creation of the bullet object, the rotation to be that of the player.
Please try replacing your 'function fire()' with mine, as shown below. Please tell me if you have problems with assigning the bullet prefab to the script, and make a copy of your original function.
It is important to attach the script to the object that you want it to get its rotation axes from.
You will notice that I have commented out the 'Quaternion.identity' part of the code. That's because I didn't need it, but if you do need it, please un-comment it.
function fire(){
audio.clip = soundeffect;
audio.Play();
var instanceBullet = Instantiate(bulletPrefab, transform.position, transform.rotation/*, Quaternion.identity*/);
bulletPrefab.rigidbody.useGravity = false;
instanceBullet.rigidbody.AddRelativeForce(transform.forward*shootForce);
}
If there are any problems, please tell me.
The bullet rotation is working wonders, except now the bullet moves to the left ins$$anonymous$$d of forward from the spawn where it should shoot, and the only exception is very inaccurate shooting down the Z axis. Any ideas?
Please try using
instanceBullet.rigidbody.AddForce(transform.forward*shootForce);
as oppose to the last line (with words) of my edited function.
I hope that this works, because the Scripting API and Official Tutorials say that it ought to.
Answer by TRG96 · Sep 07, 2014 at 04:30 PM
var instanceBullet = Instantiate(bulletPrefab,Spawn.position, Quaternion.identity); Quaternion.identity is the default rotation of the bullet, you need to rotate the bullet.
var rotation = Quternion.FromToRotation(bulletPrefab.forward, Spawn.position.forward); var instanceBullet = Instantiate(bulletPrefab,Spawn.position, rotation);
have not tried the code, but something like this should work.
I don't know if this is what you intended... Line 52 & 53 are now,
var rotation = Quaternion.FromToRotation(bulletPrefab.forward, Spawn.position.forward);
var instanceBullet = Instantiate(bulletPrefab,Spawn.position, rotation);
and it only spawns the bullet left of the spawn's transform. The rotation problem still persists.
Your answer
Follow this Question
Related Questions
shoot bullet on a moving gameobject 0 Answers
Need help with Transform.LookAt 1 Answer
Problem with Shooting Accuracy 0 Answers