• 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
Question by Shogun · May 02, 2011 at 07:21 PM · characterslopediagonalstrafe

Character moves diagonally backwards while moving side to side on slopes

Hi, i seem to be having a problem with moving a simple character left to right while on a slope. The character (A box at the moment) is a cube with a rigidbody as i'm wanting to use physics.

The character moves fine how it's supposed to on the flat (forward/backwards/left/right), but when i go on a bit of a slope and try to strafe (left/right) the character does move left/right but also moves backwards down the slope.

Heres the code that i'm using for controlling the character

var currentPosition : Vector3;

function Awake(){ //Get Start Position currentPosition = transform.position; }

function Update () {

 if(Input.anyKey)
 {
     if(Input.GetKey(KeyCode.UpArrow))
     transform.Translate(0,0,10 * Time.deltaTime);

     if(Input.GetKey(KeyCode.DownArrow))
     transform.Translate(0,0,-10 * Time.deltaTime);

     if(Input.GetKey(KeyCode.LeftArrow))
     transform.Translate(-10 * Time.deltaTime,0,0);

     if(Input.GetKey(KeyCode.RightArrow))
     transform.Translate(10 * Time.deltaTime,0,0);

     currentPosition = transform.position;
 }

 //Stop from sliding down the slope
 transform.position.x = currentPosition.x;
 transform.position.z = currentPosition.z;

 //Stop from falling over (Only temp)
 transform.rotation.x = 0;
 transform.rotation.y = 0;
 transform.rotation.z = 0;

}

Any help on how to solve this would be greatly appreciated :)

Thanks, Shogun

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Bampf · May 02, 2011 at 08:05 PM

The sliding is caused by the rigidbody, and from a physics point of view it makes some sense. When you are on a slope you slide down it. When the player moves forward or backward it's not noticeable but side to side, there's nothing to cancel out the movement downwards. Some possible solutions:

1) You may want to get rid of the rigidbody. Unity's documentation for character controller suggests an alternative:

"If you want to push Rigidbodies or objects with the Character Controller, you can apply forces to any object that it collides with via the OnControllerColliderHit() function through scripting."

2) You could try increasing the drag values on the rigidbody, but that would just make it slide more sluggishly.

3) You could add a physic material to the character's collider and/or the ground. That should let you adjust the amount of total amount of friction.

I note that some FPS games actually allow sliding when the player attempts to walk on a steep slope, so you may not need to completely eliminate it. Depends on your game.

Comment
Shogun

People who like this

1 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 Shogun · May 02, 2011 at 09:31 PM 0
Share

Excellent i've just tried suggestion number one and once i totally get the hang of the Character Controller it will work just how i want it to work, thanks :)

I also tried suggestion number 2 before i posted, but like you say it just made it more sluggish and it just made it slide down the slope slower than before :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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How can I detect the angle of the platform the player is on? 1 Answer

Collision normal angles. 1 Answer

How to make the character strafe while running? 1 Answer

axis change according to the rotation of the character 0 Answers

Unity Strafing Script 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