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 Alexander24 · May 11, 2015 at 07:09 PM · characterbulletrotate object

How to make a bullet follow a 3d character's body rotation using a orthographic view.

Hello,

I have a 3d character seen as 2d using a Orthographic camera and when I try to fire the weapon using a mouse click, the character upper body rotates based on the point of my click, the problem is that the upper body moves weirdly using this code :

     using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
     using UnityEngine.EventSystems;
     
     public class PlayerController : MonoBehaviour {
        
         public float bulletSpeed;
         public Transform ShotSpawm;
         public Transform Player;
         public GameObject bullet;
     
        
         private RaycastHit hit;   
         private float nextFire;
     
     
         void Awake ()
         {   
     
         }
        
         void Update ()
         {
            
        
             //block firing over ui elements
             if (EventSystem.current.IsPointerOverGameObject ())
                 return;
             else {
                 if (Input.GetButton ("Fire1") && Time.time > nextFire && Time.timeScale != 0) {
                     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                    
                     if (Physics.Raycast (ray, out hit)) {
                        
                         if (hit.transform.name == "ShootBoundary") {
                             Debug.Log ("Invalid click");
                         } else {
                             //character rotation based on mouse click
                             Vector3 pos = Camera.main.WorldToScreenPoint (Player.position);
                             Vector3 dir = Input.mousePosition - pos;
                             float anglee = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
                             //anglee -= 90;
                             Player.rotation = Quaternion.AngleAxis (anglee, Vector3.forward);
                            
                             nextFire = Time.time + fireRate;
                            
                             Fire ();
                         }
                     }
                    
                 }
                
             }
         }
        
     
         private void Fire()
         {
        
             GameObject currentBullet = (GameObject)Instantiate(bullet, ShotSpawm.position, ShotSpawm.rotation);
            
             Vector3 angle = Player.eulerAngles;
             currentBullet.transform.eulerAngles = angle;       
             Vector3 forceVector = currentBullet.transform.right;       
             currentBullet.GetComponent<Rigidbody>().AddForce(forceVector * bulletSpeed * 1.5f, ForceMode.Impulse);
            
         }
     }
    



The character rotates only on a top based position as shown in the below image, though the bullets that are fired move in a correct trajectory : alt text

So I noticed that if I decrease the anglee variable by 90, the body will face the mouse click position, but obviously the bullets will be shot in the ground, as shown below: alt text

Last thing I tried was not only to decrease the anglee variable by 90 but also remove the added eulerAngles to the instantiated bullet, which made the bullets go in the right trajectory but they would rotate weirdly and after a while disappear out of the camera view because the z transform value increased too much.

If anyone has a solution for this issue, to make the bullets follow the player's upper body's position and rotation, I would greatly appreciate it. If I need to add any more info regarding the script/game scene let me know.

Thanks, Alexander

screen1.jpg (82.7 kB)
screen2.jpg (81.3 kB)
Comment
Add comment · Show 1
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 AlwaysSunny · May 11, 2015 at 07:19 PM 0
Share

One of the most difficult and rewarding aspects of game design is learning to think algebraically: Using known information to arrive at unknown information. Once you have a solid grasp on what the pieces of data you're working on actually represent, you'll be able to write sensible logic on the first try, or at least not struggle as much as often.

Always break down the problem into what you know and what you need, then deter$$anonymous$$e the best way to get from here to there. Use Debug.DrawRay() to visualize vectors and Debug.Log() to report other data to yourself.

What stands out about your issue is the profuse use of angles. There is a strong likelihood you should be using nothing but vector math in this and similar scenarios. It's generally much more pleasant to think and work in those terms, perhaps ultimately converting your results to a meaningful angle if an angle is what you need.

1 Reply

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

Answer by JigneshKoradiya · May 12, 2015 at 05:27 PM

private void Fire() {

          GameObject currentBullet = (GameObject)Instantiate(bullet, ShotSpawm.position, ShotSpawm.rotation);
         
          Vector3 angle = Player.eulerAngles;
          currentBullet.transform.eulerAngles = angle;       
          Vector3 forceVector = currentBullet.transform.UP;       
          currentBullet.GetComponent<Rigidbody>().AddForce(forceVector * bulletSpeed * 1.5f, ForceMode.Impulse);
         
      }
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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How To Play A Different Animation When Bullet Contacts With A Certain Limb? 0 Answers

Bullet dissapears from screen when I use LookAt? 0 Answers

Bullet holes (decals?) on Characters 1 Answer

How can I tell Unity to wait to do something until after and animation has finished playing? 0 Answers

Make a character rotate around a point. 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