• 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 static_cast · Nov 11, 2013 at 04:01 AM · movementplayerbouncebounces

Player Bouncing off Walls

Whenever I try to move forward so that I'm up against an object, the player seems to bounce off it, like it's saying "Do not want." (XD)

I have tried several techniques to fix this, such as bounce damping.

Here is my current code: #pragma strict var movementSpeed = 1.0; var jump : KeyCode;

 private var x = 0.0;
 private var z = 0.0;
 private var grounded = true;
 
 function Start()
  {
  rigidbody.freezeRotation = true;
  }
 
 function Update()
  {
  if(grounded)
   {
   x = Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
   z = Input.GetAxis("Vertical")   * movementSpeed * Time.deltaTime;
   }
  grounded = Physics.Raycast(transform.position, Vector3.down, 0.1);
 
  transform.Translate(x, 0, z);
 
  if(Input.GetKeyDown(KeyCode.Space) && grounded)
   rigidbody.velocity = Vector3(0, Mathf.Sqrt(20), 0);
  }

Here is my player's specs as well:

 Player (Empty game object, has the script, as well as box collider)
   - Graphics (empty game object)
     - Body (Cube)
     - Head (Cube)
       - Main Camera (Has the mouse looking script)

Thanks in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Shayfen · Nov 11, 2013 at 06:57 AM

I'm not very experienced with Unity yet but the first thing popping into my mind is maybe you're using physic materials on stuff. The character shouldn't be bouncing off of walls or objects unless you've changed things to cause it to do so.

Comment
Add comment · Show 2 · 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 static_cast · Nov 11, 2013 at 02:35 PM 0
Share

Sorry, I haven't used any physic materials...

If you want I can play around with that.

avatar image Shayfen · Nov 11, 2013 at 03:16 PM 0
Share

I just started using Unity so that's pretty much all I can think of which would potentially cause issues. If you're not using any then I probably wouldn't suggest starting. From what I understand of them is they add certain physics capabilities to objects, such as bouncing.

Edit: I just saw this topic about the same issue. $$anonymous$$aybe it would help. http://answers.unity3d.com/questions/393669/avoid-character-bouncing-off-of-obstacles-1.html

avatar image
0

Answer by MarekRimal · Feb 21, 2019 at 09:55 PM

Use playerRigidbody.velocity = velocity * moveSpeed; instead of transform.Translate(x, 0, z);.

If your player now dosent move at all, be sure to NOT multiply the velocity by Time.deltaTime as it is shown here playerRigidbody.velocity = velocity * moveSpeed * Time.deltaTime;

This should fix the problem.

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 Cornelis-de-Jager · Feb 21, 2019 at 10:45 PM

What @MarekRimal said is a possible solution. The main issue is how movement works when moving transform.Translate(). So what Translate does is instantly move an object a certain distance. In this case your velocity multiple Time.DeltaTime. It's a small amount, but enough to clip it into a different object. When its clipped the Collision system Pushes the object away, or out of it - this causes that pushback effect you are getting. There are serveral ways around this, below are jsut some.

RigidBody Movement The simplest way around it is to use Rigidbody Movement, where you tell where the player should go, but you leave Unity to figure out how to do it. The real difference is there is an extra step Unity takes to decide how it reacts to physics.

Instead of Translate use:

 RigidBody rb;

 void Start () {
     // The object must have a rigidbody
     rb = GetComponent<RigidBody> ();
 }

 void Move () {
     ...
     rb.MovePosition (x, 0, z);
 }

A Second method I am quite a fan of is using Linear Interpolation (Lerp) because it works well when doing networking. What lerp does is it selects to points and moves the character to that point, first fast then slower. Again you just tell Unity what should happen and it figures it out on its own. This works well with RigidBody even though we don't utilize the movement really.

 void Move () {
     ...
     transform.position = Vector3.Lerp (transform.position, transform.position + new Vector3(x, 0, z), 1f);
 }
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.

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

19 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

Related Questions

Circular movement of the player 0 Answers

Player movement for iOS? 1 Answer

can anyone find the issue with this code? 2 Answers

Multiple Camera Angles, Choseable 1 Answer

How can i smooth out the rotation? 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