- Home /
rigid body working wierd
am doing a side scroller game demo, in which my player is a ball. he has to move according to keyboard inputs and rotate too. Everything is working fine until he collides with another collider. The movement of the player gets wierd as he jumps, rotates and moves randomly. need help.
using UnityEngine;
using System.Collections;
public class player_script : MonoBehaviour {
public float speed = 10f;
public float rotationSpeed = 30f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
float amtToMove = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
transform.Translate(Vector3.right * amtToMove);
transform.Rotate(amtToMove * rotationSpeed, 0, 0);
if(Input.GetKeyDown("space"))
{
rigidbody.velocity = new Vector3(0, 10, 0);
}
}
}
What collider are you using on your ball, and if it's mesh collider what's the polycount of your ball.
iam using sphere collider that came with the sphere when it was created.
okey, and what are the other objects its colliding with, cubes ?
I think line 23 rigidbody.velocity = new Vector3(0, 10, 0) gets a problem if the ball has rotated when colliding. As y is no longer up. If that is the case, try to reparent the ball under a transform that is not rotated. And do the jumping on the parent transform. Guess that does not work well if jumping by using physix though.
This http://answers.unity3d.com/questions/260191/getting-rigidbody-to-jump-by-changing-the-velocity.html might help then
Answer by $$anonymous$$ · Jul 22, 2013 at 10:18 AM
Maybe you need to lock constrains in rigidbody?
thanks mate ur answer worked out. the problem was when the player collided with another collider its axis got rotated due to the impact.but when i freezed the rotation the axis got freezed resulting in no rotation due to impact. thanks once again.
Your answer
Follow this Question
Related Questions
On raycast hit remove Rigidbody 1 Answer
Incomprehensive auto death on 2D runner with energy depleting mechanics [Debugging] 1 Answer
Downsize A 3DS Model? 1 Answer
Nested Coroutines Waiting for Each Other 1 Answer
Lane moving vehicle 1 Answer