Hey guys!
Im having an issue I hope you can help me out with, currently I'm making a game that has a ball that you can control and jump. The problem is that when I jump on a wall it glides all the way up!
I tried using tags for the ground and such but that does not help as there will be obstacles you must jump over so you can still glide over those.
I also tried to use a character controller, but this just broke the entire thing and it just floats there?
The object I'm trying to control has the attached script, a rigidbody and sphere collider
Thanks for taking a look at my problem!
-mike
var power = 50.0; //speed of the ball
var drag = 2.0; //how much to slow down when not controlling
private var grounded = false;
var once = 0; //var to set the direction of ball jump only once
var jumpForce; //var to set how high the ball should bounce
function FixedUpdate () {
// Only apply forces if the ball is on the floor
if (grounded) {
// Calculate the direction in which we want to roll the ball
// * It is relative to the camera
// * we remove the y component because we really want to apply forces only on the 2D plane
var forward = Camera.main.transform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Scale the force by the users input and apply it to the rigidbody
var forwardForce = forward * Input.GetAxis("Vertical") * power;
rigidbody.AddForce(forwardForce);
// Calculate the direction in which we want to roll the ball
// * It is relative to the camera
// * we remove the y component because we really want to apply forces only on the 2D plane
var right = Camera.main.transform.TransformDirection(Vector3.right);
right.y = 0;
right = right.normalized;
// Scale the force by the users input and apply it to the rigidbody
var rightForce = right * Input.GetAxis("Horizontal") * power;
rigidbody.AddForce(rightForce);
//this sets the direction of force to be applied when jumping
if (once==0){
jumpForce = rigidbody.transform.TransformDirection(Vector3.up);
once = 1;
}
//jump
if (Input.GetAxis("Jump")&&once==1){
rigidbody.AddForce(jumpForce*35);
}
// Apply drag only if the user is not pressing any keys
if (Mathf.Approximately (Input.GetAxis("Horizontal"), 0) && Mathf.Approximately(Input.GetAxis("Vertical"), 0))
rigidbody.drag = drag;
else
rigidbody.drag = 0;
}
// Every frame set grounded to false. OnCollisionStay will enable it again if
// the rigidbody collides with anything
grounded = false;
}
function OnCollisionStay(collision:Collision) {
if(collision.gameObject.tag=="ground") {
grounded = true;
}
}
asked
Jun 17 '11 at 04:48 PM
mike670
1
●
1
●
1
●
2
Just a quick side note for your code, instead of changing grounded every frame, would it not just be easier to set ground to false using oncollisionexit or when you jump?
very true thanks!