- Home /
This question was
closed Jun 07, 2012 at 10:13 PM by
NorthernEagle for the following reason:
Problem is not reproducible or outdated
Question by
NorthernEagle · Feb 16, 2012 at 05:35 AM ·
physicsgravity
Player movement with Rigidbody only, gravity not working at all.
Howdy, I'm in the middle of a personal project I'm using to learn Unity3d. Any help would be much appreciated here. I got the following C# code for moving a player around on a floor. The x-axis movement works great and so does pressing the spacebar (Jump) but there's no gravity whatsoever in the scene. The player has a rigid body with useGravity on, box collider attached as well.
void Start ()
{
rigidbody.isKinematic = false;
Physics.gravity = new Vector3(0, -50.0F, 0);
}
void FixedUpdate ()
{
posX = player.transform.position.x;
posY = player.transform.position.y;
posZ = player.transform.position.z;
float horizontalInput = Input.GetAxis("Horizontal");
Vector3 a = transform.position;
if (horizontalInput > 0)
transform.position = new Vector3(a.x + (Time.deltaTime * 2), a.y, a.z);
else if (horizontalInput < 0)
transform.position = new Vector3(a.x - (Time.deltaTime * 2), a.y, a.z);
// check for "Jump" here
if (Input.GetButton("Jump"))
transform.position = new Vector3(a.x, a.y + (Time.deltaTime * 5), a.z);
}
Comment