- Home /
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!
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.
Sorry, I haven't used any physic materials...
If you want I can play around with that.
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
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.
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);
}
Your answer