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 3kWikiGames · Oct 20, 2020 at 10:15 PM · vector3controllerturning

Right Stick Look reverts back to Vector(0,0,0)?

This is a 2D game, and I'm trying to replace this code to work with a controller instead of using the mouse position.

So I have an issue where my character is supposed to move the flashlight in the direction of the right stick, and when the person stops moving the right stick it should keep its last position, but instead it always reverts back to the vector(0,0,0) (or straight down). This seemed fairly straightforward issue, my answer was storing the last known input not at 0, then saving that in a temp variable, but it still isn't working. Any ideas? Thanks!

  private void turnFlashlight()
     {
         Vector3 Target = new Vector3(Input.GetAxis("rHorizontal")*360,-Input.GetAxis("rVertical")*360, 0f);
         Vector3 Temp = new Vector3(0f, 0f, 0.0f);
         if((Target.x != 0) && (Target.y != 0)){
             Temp = Target;
         }
         if (!CharacterSettings.paused)
         {
             flashlight.transform.LookAt(Temp);
         }
     }
 
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
1
Best Answer

Answer by rainChu · Oct 21, 2020 at 07:45 PM

Instead of setting the flashlight directly to the position of the axis, you should add the stick's values to the direction each frame. You might consider refactoring it so you work with euler angles instead of using LookAt, but I don't know the actual implications of your game or how else you're using the look target, so it's not my call.

Something along these lines might be suitable:

 [SerializeField] float m_flashlightLookSpeed = 1.0f;
 Vector3 m_lookPosition;

 [SerializeField, Space] float m_lookRangeX;
 [SerializeField] float m_lookRangeY;

 private void turnFlashlight( ) // Called every Update()
 {
     Vector3 Offset = new Vector3( Input.GetAxis( "rHorizontal" ) * 360, -Input.GetAxis( "rVertical" ) * 360, 0f );
     m_lookPosition.x += Mathf.Clamp( Offset.x * m_flashlightLookSpeed * Time.deltaTime, -m_lookRangeX, m_lookRangeX );
     m_lookPosition.y += Mathf.Clamp( Offset.y * m_flashlightLookSpeed * Time.deltaTime, -m_lookRangeY, m_lookRangeY );

     if ( !CharacterSettings.paused )
         flashlight.transform.LookAt( m_lookPosition );
 }

Edit 1: Oops! I just re-read the question, and I just noticed you said it's a 2D game. I'm sorry I missed that. You're on the right lines with trying to keep track of the last position, but you need to store it earlier, before the stick is fully centered.

 [SerializeField] float m_deadzone = 0.5f; 

 // In the method
 if ( Target.sqrMagnitude > m_deadzone * m_deadzone )
     Temp = Target;

Edit 2: I see. It's pointing down because if it's within the deadzone, temp becomes 0,0,0. You should hang onto the last viable look position:

 [SerializeField] float m_deadzone = 0.5f; 
 Vector3 m_lookTarget = Vector3.zero; // Hang onto it here, between method calls

 // In the method
 if ( Target.sqrMagnitude > m_deadzone * m_deadzone )
     m_lookTarget = Target;
 // Don't set it to 0 if inside the deadzone

 if ( !CharacterSettings.paused )
         flashlight.transform.LookAt( m_lookPosition );

 

Comment
Add comment · Show 3 · 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 3kWikiGames · Oct 22, 2020 at 05:38 PM 0
Share

Hopefully a visual may make this a bit clearer, as you can (sort of) see in the picture, the light of the flashlight is defaulted to pointing down, after pressing the right stick up, then letting it go, it then reverts back to pointing down. I checked all my other scripts to make sure nothing else was altering it but didn't find anything so it must be in this code. After trying to set the temporary flashlight position with the code you tried it still did not seem to register and keeps going to that point... not quite sure what to try next. Thanks for the help though!

alt text

screenshot-141.png (178.4 kB)
avatar image rainChu · Oct 22, 2020 at 08:05 PM 1
Share

Ah, I see. Let me edit my answer, seeing that it always points straight down has shown me the problem.

avatar image 3kWikiGames rainChu · Oct 22, 2020 at 10:46 PM 0
Share

Awesome thank you so much it works perfectly! On to the next bug :P

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

180 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

Related Questions

Slow falling when using Rigidbody set velocity as playercontroller 0 Answers

Problem with adjusting vector. 2 Answers

XBox Controller Angle of Fire Incorrect 0 Answers

Airplane Control, Aim ... Lerp and Vectors 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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