- Home /
Addforce to create a gliding effect?
Right now I'm trying to create a gliding jump, wherein if a player holds the control button during a jump the character will fall slower. Here is the code, which at the moment is crashing Unity consistently.
var Health = 0; var walkSpeed : float = 20.0; var gravity = 20.0; private var moveDirection : Vector3 = Vector3.zero; private var charController : CharacterController; var jumpSpeed = 2;
function Start() { charController = GetComponent(CharacterController); animation.wrapMode = WrapMode.Loop; }
function Update () { if(charController.isGrounded == true) { if(Input.GetAxis("Vertical") > .1) { //if(Input.GetButton("Run")) //{ //animation.CrossFade("run"); // walkSpeed = 20; // } // else //{ //animation["walk"].speed = 1; //animation.CrossFade("walk"); //walkSpeed = 20; //} } else if(Input.GetAxis("Vertical") < -.1) { //animation["walk"].speed = -1; //animation.CrossFade("walk"); // walkSpeed = 20; } else { //animation.CrossFade("idle"); }
// Create an animation cycle for when the character is turning on the spot
if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical"))
{
//animation.CrossFade("walk");
}
transform.eulerAngles.y += Input.GetAxis("Horizontal");
// Calculate the movement direction (forward motion)
moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
} //End Jump
}
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
if (Input.GetButton ("Glide") && charController.isGrounded != true) {
Debug.Log("Glide button Hit");
rigidbody.AddForce (moveDirection.y * 10);
}//End Glide
}
It would be helpful if you could provide a little more details. Is unity crashing or just not compiling? How is the CharacterCotroller defined in your script?
Alright, I've updated the post with the full script.
Unity is crashing as soon as I hit the left control key, which is what is mapped to "glide"
Answer by Jaap Kreijkamp · Mar 16, 2010 at 11:43 PM
I would expect the compiler to give an error on this script, as CharacterController.AddForce
doesn't exist. If you want to work with forces, add a Rigidbody to the character and apply forces on this with rigidbody.AddForce
.
Well, that definitely stopped the errors, and it does not crash anymore, but still no gliding effect.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Why my character wont jump??? 1 Answer
make addforce ignore player controller? 1 Answer
How do you make a characterController jump automatically when it reaches the edge of a platform? 0 Answers
How do I fix my player jumping code? 2 Answers