x


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


}
more ▼

asked Mar 16 '10 at 11:25 PM

karl_ gravatar image

karl_
2.4k 41 53 70

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?

Mar 17 '10 at 12:57 AM KvanteTore

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"

Mar 17 '10 at 02:04 AM karl_
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Mar 16 '10 at 11:43 PM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

Well, that definitely stopped the errors, and it does not crash anymore, but still no gliding effect.

Mar 17 '10 at 02:08 AM karl_
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x676
x336
x245
x9

asked: Mar 16 '10 at 11:25 PM

Seen: 2223 times

Last Updated: Mar 17 '10 at 02:05 AM