Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SHG · Sep 07, 2014 at 04:17 PM · rotationshootingbullet

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... alt text

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" );
     }
 }

example.png (92.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SHG · Sep 07, 2014 at 05:27 PM 0
Share

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?

avatar image JusticeAShearing · Sep 08, 2014 at 04:47 PM 1
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SHG · Sep 07, 2014 at 05:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

shoot bullet on a moving gameobject 0 Answers

Need help with Transform.LookAt 1 Answer

Problem with Shooting Accuracy 0 Answers

Shoot bullet in direction which player looks 0 Answers

Why is my projectile's rotation sideways? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges