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 djordr · May 08, 2018 at 10:54 PM · speedaxiswalkingxboxcontrollersensitivity

Make the RigidBody detect sensitivity of the x box controller axises?

Here is my script, it works great for keyboard movement but if you use a xbox controller with it the player moves at a constant speed at all times. You would want him to go at a walking pace if the axis is tilted by 25% running speed at 50%. Right now if the axis is tilted just one degree the character moves at full speed which even makes him move when you're not touching the stick at all because of how sensitive it is. How would I modify this code to be sensitive to the axises on the x box controller?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class scr : MonoBehaviour {
 
     bool canPlayerMove = true;
     Rigidbody rb;
     public float speed = 5f;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         float moveHorizontal = -Input.GetAxisRaw("LeftJoyStickHorizontal");
         float moveVertical = Input.GetAxisRaw("LeftJoyStickVertical");
 
         if (canPlayerMove)
         {
             Vector3 movement = new Vector3(moveVertical * Time.deltaTime, 0.0f, moveHorizontal);
             movement.Normalize();
 
             if (movement != Vector3.zero)
             {
                 rb.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15f);
                 rb.MovePosition(transform.position + transform.forward * speed * Time.fixedDeltaTime);
             }
         }
     }
 }
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 rodrybest · May 08, 2018 at 11:20 PM

You are not using your movement vector in the MovePosition function. Instead, you are moving always at the same speed with transform.forward.

Changing transform.forward with movement vector should do the trick.

Best regards, Rodri

Comment
Add comment · Show 4 · 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 djordr · May 08, 2018 at 11:30 PM 0
Share

Thanks but that didn't seem to do it, still continuously moving even if the x box stick is only tilted to 1 degree. I think more is required, or I'm missing something. Works fine with the keys since if you don't press the button the horizontal vertical axes are 0. But on a xbox controller they're always going to be something, or very sensitive. Perhaps you need to give the character a velocity, I'm very new to this so any hints are much appreciated. ;)

avatar image rodrybest · May 08, 2018 at 11:39 PM 0
Share

If you comment the line movement.Normalize() changes something when you move the player with the joystick?

Another thing that I don't undestand is why are you using Time.deltaTime in the x component off youre movement vector. You are applying the Time.fixedDeltaTime after in the $$anonymous$$ovePosition, so Time.deltaTime is not needed here.

Rodri

avatar image djordr rodrybest · May 08, 2018 at 11:55 PM 0
Share

@rodrybest Thanks for the heads up on the Time.deltaTime. No commenting out that line does nothing.

If you care to bother you can put the script on a cube and add a rigid body to it. Just have to change LeftStickHorizontal and LeftStickVertical for Horizontal and Vertical. It probably has something to do with transform.forward. That's 1 and doesn't take into account the axises. Changing that to movement does nothing however, hmmm not sure how to resolve this. :/

avatar image djordr rodrybest · May 09, 2018 at 12:03 AM 0
Share

FIGURED THAT OUT TOO, change the dead zone in the input manager.

@rodrybest Wait I figured it out, had to remove normalize and change the forward for movement. Now he moves around correctly, still an issue though, the rate at which he rotates and the rate at which he moves is very sensitive. A slight tilt to the right is going to make him face right. Is there a way to restrict is so if only the stick is tilted a certain amount of degrees the character starts moving or rotating?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class scr : $$anonymous$$onoBehaviour {
 
     bool canPlayer$$anonymous$$ove = true;
     Rigidbody rb;
     public float speed = 5f;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         float moveHorizontal = -Input.GetAxisRaw("LeftJoyStickHorizontal");
         float moveVertical = Input.GetAxisRaw("LeftJoyStickVertical");
         Debug.Log(moveHorizontal + moveVertical);
 
         if (canPlayer$$anonymous$$ove)
         {
             Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);
 
             if (movement != Vector3.zero)
             {
                 rb.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15f);
                 rb.$$anonymous$$ovePosition(transform.position + movement * speed * Time.fixedDeltaTime);
             }
         }
     }
 }

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

141 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

Related Questions

Huge drift Issue with xbox controllers. 0 Answers

Pressing both vertical and horizontal axis slows down my move speed 0 Answers

Script that plays animation when player is moving? 0 Answers

Xbox One Cursor 0 Answers

Rotate an object respectively to other object's rotation on z-axis 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