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
0
Question by neatgadgets · May 11, 2014 at 01:56 AM · directionbulletshoot

Change bullet direction with player direction

I have been reading for ages about this and each question I find suggests what I aready have but not sure why my code is not working.

 using UnityEngine;
 using System.Collections;
 
 public class shoot : MonoBehaviour {
 
     public GameObject bullet;
 
     //Forces to apply to our projectile 
     public float projectileForwardForce; 
     public float projectileUpForce; 
     
 
 
     // Update is called once per frame
     void Update () {
 
         //If our left mouse button is clicked 
         if (Input.GetMouseButtonDown(0)) 
         { 
             Vector3 offsetForward = transform.forward * 2; 
             Vector3 offsetUp = transform.up; 
             
             //Calculate our offset position 
             Vector3 finalPosition = transform.position + offsetForward + offsetUp; 
 
             //Creates a new object at our current position and rotation 
             GameObject myObject = Instantiate(bullet, finalPosition,bullet.transform.rotation) as GameObject; 
 
             //Apply a force to our tank shell to make it fly into the distance 
             myObject.rigidbody.AddRelativeForce(myObject.transform.forward * projectileForwardForce, ForceMode.Impulse); 
 
             //Add a small force upwards to make it fly into the arc a little 
             myObject.rigidbody.AddRelativeForce(myObject.transform.up * projectileUpForce, ForceMode.Impulse); 
             Destroy(bullet,2.0f);
 
         } 
     }
 }
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

1 Reply

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

Answer by robertbu · May 11, 2014 at 01:58 AM

You have some problems here. First, you are setting the bullet's rotation to the rotation of the prefab, not the rotation of the player. Assuming this script is on the player, you need to change line 27:

      GameObject myObject = Instantiate(bullet, finalPosition, transform.rotation) as GameObject; 

Your other problem is that you are using world vectors to apply force in the local coordinate system. Try this instead:

      Vector3 vForce = transform.forward * projectileForwardForce + transform.up * projectileUpForce;
      myObject.rigidbody.AddForce(vForce, ForceMode.Impulse); 
 

Since the rotation of the player object and the rotation of the projectile match, we an use the player's 'forward' and 'up'.

Comment
Add comment · Show 5 · 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 neatgadgets · May 11, 2014 at 11:07 PM 0
Share

Cool, that worked to get the bullet moving in the direction the player is facing, but the bullet itself has not rotated. I took out the lines

    //Apply a force to our tank shell to make it fly into the distance 
      myObject.rigidbody.AddRelativeForce(myObject.transform.forward * projectileForwardForce, Force$$anonymous$$ode.Impulse); 
 
      //Add a small force upwards to make it fly into the arc a little 
      myObject.rigidbody.AddRelativeForce(myObject.transform.up * projectileUpForce, Force$$anonymous$$ode.Impulse); 

And put in the code you suggested

Vector3 vForce = transform.forward projectileForwardForce + transform.up projectileUpForce; myObject.rigidbody.AddForce(vForce, Force$$anonymous$$ode.Impulse); Was this what you meant or is the bullet not rotating because I implemented the code incorrectly?

avatar image robertbu · May 12, 2014 at 03:47 AM 0
Share

Assu$$anonymous$$g you've built your projectile so that the front of the projectile faces positive 'z' when the rotation is (0,0,0), then you can add this line:

  myObject.transform.rotation = Quaternion.LookRotation(vForce);

If you have gravity enabled, then you probably want to scratch the previous line, and add a script to the projectile that has this line:

   transform.rotation = Quaternion.LookRotation(rigidbody.velocity);

This will keep the front of the projectile point in the direction of movement as it arcs to its destination.

avatar image neatgadgets · May 12, 2014 at 12:41 PM 0
Share

I used myObject.transform.rotation = Quaternion.LookRotation(vForce); I have gravity enabled and this worked although the bullet is shooting out vertical ins$$anonymous$$d of horizontal. The other was making the player tilt.

avatar image robertbu · May 12, 2014 at 05:32 PM 0
Share

The second line will only make the player tilt if you put it in the same script. Quoting from my comment:

add a script to the projectile

The script with this line needs to be on the projectile, not in the shooting code. This line will change the projectile's rotation as it curves through the scene.

As for the projectile being vertical, you've authored your mesh wrong. You need the forward of the projectile to face positive 'z' when the rotation is (0,0,0).

avatar image neatgadgets · May 13, 2014 at 03:06 AM 0
Share

Yep, all good, I had the shape of the bullet having the length going in the y direction and not in the z direction as you said. All good now. Than you very much for your help!

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

21 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

Related Questions

Shoot an object and have it move based on rotation 1 Answer

Bullets Based on Orientation 1 Answer

How to make a gun shoot? 2 Answers

Shoot with ray cast angle 1 Answer

Problem with Shooting Accuracy 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