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 alexanderflink · Mar 23, 2013 at 12:36 AM · rotationinputcharactercontrollerdirectiontop-down

Rotation of character resets when joystick is released

Hello! My problem is the following: I'm trying to create a top down controllable character using the CharacterController. My main focus here is making it work with a gamepad (xbox 360 controller). The character rotates and moves perfectly according to left stick input. However, whenever the left stick is released, the character seems to snap right, left, up or down depending on it's current direction. What I want is to have something similar to Dark Souls or Zelda: Wind Waker, where the character maintains it's direction when the joystick is released. Here is the Update function of the player object:

 function Update () {
     horizontal = Input.GetAxis("Horizontal");
     vertical = Input.GetAxis("Vertical");
     if (controller.isGrounded) { // are we grounded?
         direction = Vector3(horizontal,0,vertical); // set direction
         moveDirection = Vector3(horizontal,moveDirection.y,vertical); // set moveDirection
         
     } else {
         moveDirection.y -=0.1; // apply gravity
     }
     controller.transform.LookAt(transform.position+direction);
     controller.Move((moveDirection*speed)*Time.deltaTime);
 }

Any help is greatly appreciated! /Alex

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 alexanderflink · Mar 23, 2013 at 10:20 AM 0
Share

Okay, so I narrowed the problem down to the Joystick Input. It seems that if I hold the left stick up and to the left (northwest, or any other direction) and then let go, one of the axes, Horizontal or Vertical, always drops down to about 0.1 ins$$anonymous$$d of keeping it's value, making the character "snap" roughly 90 degrees. Any ideas on how to fix this?

3 Replies

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

Answer by whydoidoit · Mar 23, 2013 at 10:51 AM

The joystick must return to 0,0 when it centres - that's what is has to do otherwise you'd never be able to tell what was going on :)

You could:

  • Add the horizontal and vertical values up and store them in a variable

  • Store the largest magnitude value (ignoring sign) and then 0 it if the sign changes.

That last lets you tap one way and tap the other - not sure if that's what you are after.

However it appears that you want to use it to rotate the character- I'm not 100% sure I quite get my head around how. Perhaps it would be better for you to just not do the LookAt if the magnitude of the vector is small:

   if(direction.sqrMagnitude > 0.1f)
        controller.transform.LookAt(transform.position + direction);
Comment
Add comment · Show 5 · 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 alexanderflink · Mar 24, 2013 at 12:03 PM 0
Share

Wow, that last piece of code works like a charm! Thank you. I'm not sure I understand it though. Why do you square the magnitude of the vector? And also, what does the "f" in 0.1f mean? Thank you again. :)

avatar image whydoidoit · Mar 24, 2013 at 02:46 PM 1
Share

I took the sqr of the magnitude because that's faster than getting the square root and is O$$anonymous$$ for this kind of calculation. The 0.1f is me thinking in C# (that's how you specify a float) sorry about that. Could you tick the answer?

avatar image whydoidoit · Mar 24, 2013 at 02:47 PM 0
Share

Basically it says don't look at the thing unless you are actually moving the joystick, as in, when the joystick is centered just ignore the direction and leave it like it was the last time.

avatar image alexanderflink · Mar 24, 2013 at 03:02 PM 0
Share

Ah, I understand. Very good answer. Thanks!

avatar image suniltelistr · Jan 06, 2020 at 02:00 PM 0
Share

I need its complete code please help me. I am beginer in coding and unity.

avatar image
0

Answer by Toddweiss · Feb 19, 2019 at 09:32 AM

thanks a lot, that really helps me understand thanks a lot guys basically if we are zero on vertical or horizontal then don't face the start direction and stay in the last know place direction.

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 nextage575 · Nov 07, 2019 at 06:03 AM

i am moving gameObject like this

     float moveHorizontal = CrossPlatformInputManager.GetAxis("Horizontal");
     float moveVertical = CrossPlatformInputManager.GetAxis("Vertical");
     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    transform.rotation = Quaternion.LookRotation(movement);
     transform.Translate(movement * Time.deltaTime * carSpeed, Space.World);

how to fix this same issue it reset when i release joystick.

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

15 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

Related Questions

Third person Character Controller slowing down when camera is pointing towards the ground 2 Answers

Quaternion amount/value of rotation? 2 Answers

Getting a direction arrow to rotate on click 2 Answers

Cant get my character to rotate properly 0 Answers

After adding script for rotation to look up down, look left right stopped working,After writing code for the rotation for looking up down, looking right left stopped 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