Hey guys,
I’ve recently come back to Unity and have been playing with the tools a little bit. Today, I created a new project and put in the following:
- A new cube (represents the player / jumping and falling object)
- A new cube which I flattened (represents the floor)
The two cubes have the default box colliders without the trigger checked.
The first cube (the player) has a rigidbody that does use gravity.
Right now everything works fine, my camera follows my cube, and I can move my cube with my arrow keys.
Additionally my cube does not fall through the floor
However, I wanted to introduce a jumping component to my cube.
When I did this, the object was rotating in the air and after some debugging I realized that this is because the force from the translation is still being applied. To try and fix this, I added a “canMove” boolean so that the object wouldn’t move (rotate) in the air - but now the object moves on the first jump and then stops moving completely. (Probably because I have never set canMove back to true).
Anyways, here is my code:
/
* move.js */
var moveSpeed : float = 5.0;
var jumpAmount : float = 200.0;
var jumpTIme = 1.0;
var nextJump = 0.0;
var canMove : boolean = true;
function Update( ) {
if (Input.GetAxis ("Jump")) {
if (Time.time > nextJump) {
rigidbody.AddForce(0, jumpAmount, 0);
nextJump = Time.time + jumpTime;
canMove = false;
}
}
if(canMove) {
var z = moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
transform.Translate(0, 0, z);
}
}
function OnTriggerEnter(otherObject : Collider) {
canMove = true;
}
What is a better way to doing this? I think I read somewhere that transform.Translate should not be used with rigidbodies or something (I forget).
Thanks for all your help,
Bucco