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 Posthuman-Wizard · Jan 12, 2014 at 04:38 PM · movementphysicsinputcharactercontrolleraddforce

Movement with AddForce: Wrong Direction

Hey everyone; I have a Player Controller script that is supposed to apply a force to the player GameObject to make it move in a direction based upon user input.

 public void MoveUpdate () {
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         if(h * this.rigidbody.velocity.x < maxSpeed) {
             this.rigidbody.AddForce(Vector3.right * h * moveForce);
         }
         if(Mathf.Abs(this.rigidbody.velocity.x) > maxSpeed) {
             this.rigidbody.velocity = new Vector3(Mathf.Sign(this.rigidbody.velocity.x) * maxSpeed, this.rigidbody.velocity.y, this.rigidbody.velocity.z);
         }
         if(v * this.rigidbody.velocity.z < maxSpeed) {
             this.rigidbody.AddForce(Vector3.forward * v * moveForce);
         }
         if(Mathf.Abs(this.rigidbody.velocity.z) > maxSpeed) {
             this.rigidbody.velocity = new Vector3(this.rigidbody.velocity.x, this.rigidbody.velocity.y, Mathf.Sign(this.rigidbody.velocity.z) * maxSpeed);
         }
         if(h > 0 && !facingRight) {
             Flip();
         }
         else if(h < 0 && facingRight) {
             Flip(); 
         }
     }

When I press "w" or "up," the forward input buttons (a.k.a. Input.GetAxis("Vertical")), the player also moves a little bit to the left or right, though. Why might this be happening? I would like the player to only move forward if only the "w" or "up" inputs are activated.

Edit: I've verified that the "h" value is never anything higher or lower than zero when this happens, and I've tested this script on a basic cube object, with the same results.

Edit #2: I've discovered that the issue is actually because I am using more than one collider to create a compound collider; when turning off one, it works perfectly. What is the best way to get around this? I need multiple colliders to match the shape of my character correctly.

Comment
Add comment · Show 2
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 robertbu · Jan 12, 2014 at 04:45 PM 0
Share

Start by doing:

 Debug.Log(h+", "+v);  

If you are seeing any non-zero value of 'h' when you only press a vertical key, then you may want to adjust the dead zone for the 'Horizontal' axis: Edit > Project Settings > Input. Edit the 'Dead' setting.

avatar image Posthuman-Wizard · Jan 12, 2014 at 04:49 PM 0
Share

Hey robertbu; I am always seeing zero for h when pressing the vertical key.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by agies1 · Jan 12, 2014 at 07:43 PM

Your issue is more than likely due to physics drag. Try going to your projects Physics settings and switch the material to a super smooth Physics material (you can pull it out of the Physics material Asset Package). If your odd movement is fixed, you should consider using a Capsule or Sphere collision box (they have less surface area and will create less drag), instead of a cube, or use the default smooth physics material.

Comment
Add comment · Show 11 · 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 Posthuman-Wizard · Jan 13, 2014 at 01:58 PM 0
Share

Thanks for the suggestion agies1, I'll try that out tonight. I just realized that I'm also using two sphere colliders as "feet" for my game's character, and that they overlap; that might be part of the problem (Though not all of it, as I've had the same issue with simple shapes), and I may need to set them so that they ignore collisions between eachother. I'll play around with the physics materials and get back to this thread with my results.

avatar image Posthuman-Wizard · Jan 18, 2014 at 02:48 PM 0
Share

I've discovered that the issue is actually because I am using more than one collider to create a compound collider; when turning off one, it works perfectly. What is the best way to get around this? I need multiple colliders to match the shape of my character correctly.

avatar image bodec · Jan 18, 2014 at 06:52 PM 0
Share

Use a mesh colided they are more expensive on a object but if just on the player should be no problem. Also a capsule collider is good but you have to be ok with it not being a perfect fit Also double check your followers are facing the same direction you could be pushing against each other

avatar image Posthuman-Wizard · Jan 19, 2014 at 09:05 PM 0
Share

$$anonymous$$y character is a sprite drawn at an angled perspective, so that wouldn't work. I can try using a single collider, such as a sphere or capsule collider, but the character's shape is still somewhat complex; is there any way at all to get this to work with compound colliders?

avatar image bodec · Jan 19, 2014 at 09:58 PM 0
Share

Did you double check your colliders to make sure they are all facing the same direction

Show more comments
avatar image
0

Answer by Grim_Darknight · Jan 20, 2014 at 09:21 AM

The best way that I fould to have compond colliders on objects withou causing Physics issues is to set their collision properties so that they are unable to collide with themselves. The biggest issue with this will be that clones of objects setup like this wont collide with each other.

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 Posthuman-Wizard · Jan 20, 2014 at 04:38 PM 0
Share

I've tried doing that before, but that didn't seem to work.

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

21 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

Related Questions

How can I calibrate phone accelerometer? 0 Answers

Foces only change during collision 1 Answer

How to modify the base movement speed of the character in JS 1 Answer

Mouse movement swings character around in circles 1 Answer

Physics AddForce reduced when 3 objects are colliding 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