- Home /
Can't get gravity to work proberly (transform)
So I'm using a transform as a player with a camera following it. Gravity works fine at start, also when I hit the floor mostly. Sometimes the transform starts to rotate in these weird directions and suddenly my player can fly around rotating forever in the void. Here is a picture:
As you can see the cube had begun to rotate and floating.
It's very random when it happens sometimes it works for just a while and then it screws up.
Here is my code:
var speed : float = 5.00;
var yVelocity : float = 10.00;
var gravity : float = 5.00;
var onFloor : boolean = false;
function Update () {
plyMove();
if(onFloor == false) {
transform.position.y -= gravity * Time.deltaTime;
} else {
return;
}
}
function plyMove () {
if(Input.GetKey("s")) {
transform.Translate(-Vector3.forward * speed * Time.deltaTime);
}
if(Input.GetKey("w")) {
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if(Input.GetKey("d")) {
transform.Rotate(Vector3(0,15,0) * Time.deltaTime);
}
if(Input.GetKey("a")) {
transform.Rotate(Vector3(0,-15,0) * Time.deltaTime);
}
if(Input.GetKeyDown("space")) {
transform.position.y += yVelocity * Time.deltaTime;
}
}
function OnCollisionEnter(theCollision : Collision) {
if(theCollision.gameObject.tag == "floor") {
onFloor = true;
}
}
function OnCollisionExit(afterCollision : Collision) {
if(onFloor == true) {
onFloor = false;
}
}
also I believe it could be something about the rotation, but im not sure.
You know Unity has both rigidbody physics and Character Controller Physics?
Program$$anonymous$$g a physics engine from scratch was a great learning experience for me. Then I threw up my hands and appreciated how much easier it was to use Unity's physics engine.
Answer by Garth-Smith · Apr 03, 2013 at 10:37 PM
Are you combining your own physics with some colliders? Maybe the built-in collisions in Unity are knocking your player around in weird directions when it hits the floor? Hitting the floor could cause your object to get some kind of momentum causing it to rotate forever or start moving on its own.
Answer by Pulsar-Regions · Apr 04, 2013 at 06:20 AM
I'm not sure what you mean, as you can see in my code I've combined my own physics with colliders. Basicly this is all the code I have in my project, except the camera that follow the cube.
Here is the camera code if needed: var target : Transform; var smooth : float = 400;
function Update () {
camMove();
}
function camMove () {
transform.position.x = Mathf.Lerp(transform.position.x, target.position.x, Time.deltaTime * smooth);
}
EDIT: I'll just use rigidbody Thanks for the answers
Your answer
Follow this Question
Related Questions
move camera when it collides with a trigger 1 Answer
How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers
Object "crawling" over floor, how? 2 Answers
How to move a Game Object from a script not attached to it. 1 Answer
Character Transform shifting after colliding with objects 0 Answers