• 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 epic1legend · Apr 07, 2014 at 02:26 PM · bulletfiring

2D bullet angle question. Please Help.

I'm trying to make a 2D sidescroller with shooting, where moving the mouse up and down moves the gun the player is holding but only until a certain height (similar to the game metal slug.) The player has an invisible object "the shoulder" as a child, this is what rotates upa dn down. The gun is a child of this and is slightly in front of it. The bullets shoot forward from the gun. The player has a texture flip script on him, this makes him and all his children flip over. Everything works perfectly fine on the initial direction the character starts in, however wen he moves the other way, the gun rotates fine, but the bullets shoot downwards more as I move the gun up and upwards more as I move the gun down. I have searched all over my script and tried many things to fix this. I am fairly new to unity and scripting. Please help.

alt text

This is the initial way he is facing, debugged using raycast...

alt text

This is the opposite way...

 var X : float;
 var right : boolean;
 
 function Start () {
     //Gathering normal object scale 
     X = transform.localScale.x;
 }
 
 function FixedUpdate () {
     if(Input.GetKey('a')){
         right = false;
         }
         
      
     if(Input.GetKey('d')){
         right = true;
         }
      
      if(right == true){    
          transform.localScale.x = -X;
          }
      else{
      transform.localScale.x = X;
      }
 }

Texture Flip Script, JavaScript

 public class Fire : MonoBehaviour {
 
     public GameObject BulletPrefab;
 
     void Start () {
     
     }
 
     void Update () {
         if(Input.GetMouseButtonDown(0)){
             Instantiate(BulletPrefab, transform.position, transform.rotation);
         }
     }
 }

Fire Projectile Script, C#

 public class MouseLook : MonoBehaviour {
     
     public float sensitivityY = 15F;
     public float minimumY = -60F;
     public float maximumY = 60F;
     public bool right = true;
     
     float rotationY = 0F;
     
     Quaternion originalRotation;
     
     void Update ()
     {
         if(Input.GetKey("a")){
             right = false;
         }
         if(Input.GetKey ("d")){
             right = true;
         }
     
 
         if(right == false){
             rotationY += Input.GetAxis("Mouse Y") * -sensitivityY;
         
             rotationY = ClampAngle (rotationY, minimumY, maximumY);
         
             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
         
             transform.localRotation = originalRotation * yQuaternion;
         }
         if(right == true){
             rotationY += Input.GetAxis("Mouse Y") * -sensitivityY;
             
             rotationY = ClampAngle (rotationY, minimumY, maximumY);
             
             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
             
             transform.localRotation = originalRotation * yQuaternion;
         }
     }
     void Start ()
     {
         if (rigidbody)
             rigidbody.freezeRotation = true;
         originalRotation = transform.localRotation;
     }
     
     public static float ClampAngle (float angle, float min, float max)
     {
         if (angle < -360F)
             angle += 360F;
         if (angle > 360F)
             angle -= 360F;
         return Mathf.Clamp (angle, min, max);
     }
     }

Modified Version of MouseLook script this makes the gun move with the mouse but only to a certain height, C#

 ublic class Projectile : MonoBehaviour {
 
     public float ProjectileSpeed;
     public bool right;
 
     private MouseLook shoulder;
 
 
     void Start () {
         shoulder = GameObject.FindGameObjectWithTag("shoulder").GetComponent<MouseLook>();
     
         if(shoulder.right == true){
             right = true;
         }
 
         if(shoulder.right == false){
             right = false;
         }
     }
 
     void Update () {
 
         if(right == true){
 
             float amountToMove = ProjectileSpeed * Time.deltaTime;
             transform.Translate(Vector3.forward * amountToMove);
         }
     
         if(right == false){
 
             float amountToMove = ProjectileSpeed * Time.deltaTime;
             transform.Translate(-Vector3.forward * amountToMove);
         }
 
 
     }
 }
 

Projectile Script that fires the bullet prefab, C#

1.png (35.4 kB)
2.png (32.2 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

Answer by andrew_196 · Apr 07, 2014 at 11:22 PM

In the unity editor you could always attach the bullet to the gun which rotates I believe, this would mean when it is instantiate as a copy of that object you would not have to rotate it as it will be the same rotation as the gun/hand.

(havent tested but should work)

Note: if that doesn't work you may also have to set the point of rotation to the center of the character.

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 epic1legend · Apr 08, 2014 at 01:10 PM 0
Share

The bullet is attached to the gun, it shoots a prefab of the bullet when a button is pressed. Also the shoulder is what rotates and is attached to the player, the player does not rotate.

avatar image
0

Answer by Andres-Fernandez · Apr 08, 2014 at 01:43 PM

Try setting the initial direction of the bullet as the vector from the shoulder position to the gun position (normalized) and use that vector to update the bullet. That way you don't need to worry about rotations and stuff.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

How to avoid speed change in bullets while moving? 1 Answer

Turret Firing problem [C#] 2 Answers

Issues with bullet not subtracting properly 1 Answer

Problem adding force to a bullet 1 Answer

Help! Can't destroy my bullet object because is a rigidbody 1 Answer


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