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 /
avatar image
0
Question by Mc837 · Jul 10, 2017 at 01:36 PM · rotationinputdirectionreference

Getting a direction arrow to rotate on click

Hi, I'm new to Unity and I'm playing around with ways of getting user input.

My aim is to have an arrow rotate around an immobile ball (player) while left click is held down, and later I'd like to right click to hit the ball in the direction the arrow is pointing, but first:

I have a PointerRotator class which moves relative to the ball, and makes an arrow spin around it at a given speed . In the editor, I have this script applied to my Pointer GameObject referencing the Player GameObject.

 public class PointerRotator : MonoBehaviour {
 
     public GameObject player;
     private float speed = 0;
     private Vector3 offset;
 
     void Start()
     {
         offset = transform.position - player.transform.position;
     }
 
     void LateUpdate ()
     {
         transform.Rotate(new Vector3(0, 30, 0) * speed * Time.deltaTime);
         transform.position = player.transform.position + offset;
     }
 
     public void setSpeed(int x)
     {
         speed = (float)x;
     }
 
     public float getDirection()
     {
         return this.transform.eulerAngles.y;
     }
 }

Now, my PlayerController Class, which is applied to my Player GameObject, and in the Editor references my Pointer GameObject as prot .

 public class PlayerController : MonoBehaviour {
 
     public Rigidbody rb;
     public PointerRotator prot;
     private float direction = 0;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         prot = GetComponent<PointerRotator>();
     }
     
     private void FixedUpdate()
     {
         if (rb.velocity.magnitude > 0.1)
         {
             prot.setSpeed(0);
         }
         else
         {
             while (Input.GetMouseButtonDown(0) == true)
             {
                 prot.setSpeed(10);
                 direction = prot.getDirection();
             }
             prot.setSpeed(0);
         }
     }
 }

Obviously the ball cannot move yet as I haven't implemented any way of it doing so. My issue is that when I press down my left mouse button the Pointer doesn't start rotating. I have a feeling I'm missing some sort of referencing, but I'm not sure.

I'm not getting any errors in the editor (other than PlayerController.direction not being used), and the references during runtime are as I set them.

Any help would be much appreciated. Thank you in advance!

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
1
Best Answer

Answer by Jwizard93 · Jul 11, 2017 at 04:00 AM

You have to remove the private keyword from FixedUpdate in PlayerController.cs. But wait don't do it just yet! Otherwise it will be CTRL-ALT_DELETE for you.

Lines 21 -25 of PlayerController.cs.

A while loop is not suitable here. The while loop executes because the mouse button went down in this frame. It will never register that the mosuebutton was released that would require another frame, but you are stuck in this one. So you are spinning your wheels. Setting prot.speed to ten and returning it's direction (the same direction) over and over again at high speeds. For forever... not really but you can either wait to see if your memory gets full or you can force quit the app.

switch while to if.

change prot.setSpeed(0); to

 if ( Input.GetMouseButtonUp(0) )
 {
     prot.setSpeed(0);
 }

Otherwise right after you set it to ten you set it right to 0 and it will again never move.

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

Answer by Mc837 · Jul 13, 2017 at 01:16 AM

Thank you @Jwizard93! Worked great. Need to get into the per-frame-update mindset.

For posterity, this is the correct FixedUpdate method that allows the Pointer to rotate only on mouse button held down:

     void FixedUpdate()
     {
         if (rb.velocity.magnitude > 1)
         {
             prot.setSpeed(0);
         }
         else
         {
             // set arrow pointer active
             if (Input.GetMouseButton(0) == true)
             {
                 // activate rotating arrow
                 prot.setSpeed(10);
 
                 // update direction
                 direction = prot.getDirection();
             }
             else
             {
                 // stop rotating arrow if button not pressed
                 prot.setSpeed(0);
             }
         }

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

94 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

Related Questions

Quaternion amount/value of rotation? 2 Answers

Rotation of character resets when joystick is released 3 Answers

Need help on the 3ds max style camera control 0 Answers

[Solved] Formation System - Rotate Vector3 Positions around a point? 2 Answers

How do I turn 1 objects rotation into another objects movement direction? 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