Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Xerather · Feb 26, 2019 at 02:14 PM · rotationscripting problemmovementnewbiethird-person

how to shoot on different direction while moving away

so i'm making a third person shooter on android, where left analog will move and rotate the character as i move it, and right analog to shoot with direction.

while standing still,the right analog does the job of shooting with the right direction, but while moving, the rotation created by the left analog can't be overwrite by the right one.

can somebody help me with this ?

i did try using public static bool on_shoot, but when Debug it, it always false on the player script. while it's okay on the bullet_spawner

you can see Supercell's brawlstar for their analog and shooting movement for reference.

 // this is player script
 void Update()
 Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);
 
         if (moveVector != Vector3.zero)
         {
             Debug.Log(sc_Bullet_Mouth.on_shoot);
             transform.rotation = Quaternion.LookRotation(moveVector);
             transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
         }
 }
 
 //this is bulletSpawner script
 
 
 Public class sc_Bullet_Mouth : MonoBehaviour
 {
 
     public Joystick right_joystick;
     public GameObject Bullet;
     public GameObject player;
 
     public float fireRate;
     public float nextFire;
 
     public bool canshoot;
 
     public static bool on_shoot;
 
     // Start is called before the first frame update
     void Start()
     {
         on_shoot = false;
         fireRate = 0.75f;
         nextFire = 0.0f;
 
         canshoot = true;
     }
 
     // Update is called once per frame
     void Update()
     {       
 
         Vector3 rightMoveVector = (Vector3.right * right_joystick.Horizontal + Vector3.forward * right_joystick.Vertical);
 
         if (rightMoveVector != Vector3.zero)
         {
 
             on_shoot = true;
             Debug.Log("masuk" + on_shoot);
             player.transform.rotation = Quaternion.LookRotation(rightMoveVector);
 
             if( Time.time > nextFire)
             {
                 
 
 
                 StartCoroutine(fire());
                 nextFire = Time.time + fireRate;
 
             }
 
             on_shoot = false;
            
             
         }
 
         
 
     }
 
     public IEnumerator fire()
     {
         Instantiate(Bullet, transform.position, transform.rotation);
         
         yield return new WaitForSeconds(0.09f);
        
         Instantiate(Bullet, transform.position, transform.rotation);
 
         yield return new WaitForSeconds(0.09f);
 
         Instantiate(Bullet, transform.position, transform.rotation);
     }
 
 }
 


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 Skaster87 · Feb 28, 2019 at 05:32 PM

I'm guessing your immediately overwriting any changes in your update loops, and this is why I'm not a fan of changing the player rotation from the SC_Bullet_Mouth script, but assuming sc_Bullet_Mouth works how I think, you are correct in saying you need a bool. Something like this should work.

 public class PlayerScript: MonoBehaviour {
     sc_Bullet_Mouth bullet_Mouth; // Create reference to bullet_mouth component
     void Start() {
         bullet_Mouth = GetComponent<sc_Bullet_Mouth>(); 
     }
     void Update() {
      Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);
 
         if (moveVector != Vector3.zero)
         {
             transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);

             if (!bullet_Mouth.isShooting)// if not shooting
             {
                 transform.rotation = Quaternion.LookRotation(moveVector);
             }
         }
 
     }
 }

//this is bulletSpawner script

  public class sc_Bullet_Mouth : MonoBehaviour
 {
     public Joystick right_joystick;
     public GameObject Bullet;
     public GameObject player;
 
     public float fireRate;
     public float nextFire;
 
     public bool canshoot;
 
     public bool isShooting;
 
     // Start is called before the first frame update
     void Start()
     {
         isShooting = false;
         fireRate = 0.75f;
         nextFire = 0.0f;
 
         canshoot = true;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         Vector3 rightMoveVector = (Vector3.right * right_joystick.Horizontal + Vector3.forward * right_joystick.Vertical);
 
         if (rightMoveVector != Vector3.zero)
         {
 
             isShooting = true;
             Debug.Log("masuk" + isShooting);
             player.transform.rotation = Quaternion.LookRotation(rightMoveVector);
 
             if (Time.time > nextFire)
             {    
                 StartCoroutine(fire());
                 nextFire = Time.time + fireRate;
 
             }
         }
         else // no rightstick movement detected, will rotate towards player moveVector
         {
             isShooting = false;
         }
     }
 
     public IEnumerator fire()
     {
         Instantiate(Bullet, transform.position, transform.rotation);
 
         yield return new WaitForSeconds(0.09f);
 
         Instantiate(Bullet, transform.position, transform.rotation);
 
         yield return new WaitForSeconds(0.09f);
 
         Instantiate(Bullet, transform.position, transform.rotation);
     }
 
 }


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 Xerather · Jun 03, 2020 at 04:01 AM 0
Share

I'm sorry for the late reply, actually i remembered replying to this. anyway you did helped me back then. thanks for the 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

306 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I determine which direction the player is moving based on mouse rotation direction? 1 Answer

Movement of a sphere 0 Answers

how do I rezolve "look rotation viewing vector is zero"? 1 Answer

Problem with player direction using virtual joysticks 0 Answers

Camera Rotation every time a key is pressed 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