x


Difficulty with rolling my own platformer script

I'm writing a very simple platformer, and i'm trying to get the script that controls the player right. I have the following so far:

using UnityEngine;
using System.Collections;

public class PlatformerControlScript: MonoBehaviour {
    public float GravityStrength = 5;
    public float JumpStrength = 1000;
    public float MovementSpeed = 500;
    public float XTolerance = 1;

    void FixedUpdate () 
    {
        float forceX = Input.GetAxis("Horizontal") * MovementSpeed;
        rigidbody.AddForce(new Vector3(forceX, 0, 0));
    }
    void OnCollisionStay(Collision collisionInfo)
    {
        if (collisionInfo.gameObject.tag == "Lava")
        {
            Die();
        }
        if (rigidbody.velocity.x <= XTolerance && rigidbody.velocity.x >= -XTolerance) //we are still enough to do.... something
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                rigidbody.AddForce(new Vector3(0, JumpStrength, 0));
            }
        }
    }
    void Die()
    {
        //TODO: add code here
    }
}

There's some piping code for when i add in a lava tile to my platformer, but ignore that for now. Right now, i'm just trying to get my platformer guy to jump: If he is still on a surface, and i press space, he won't jump. The force i'm applying should be enough, yet it doesn't seem to be jumping. What's the recommended way to get something to "jump"?

more ▼

asked Apr 02 '10 at 09:50 AM

RCIX gravatar image

RCIX
108 4 4 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Rather than applying a force you could try directly altering the velocity:

if (Input.GetButtonDown ("Jump") && ground == true) {
        rigidbody.velocity.y = 10;
    }
}

The ground part is to make sure you're on the ground before you can jump (unless you have some kind of double jump). You might also want to stop auto jumping if you hold down the space key.

more ▼

answered Apr 16 '10 at 06:10 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

(comments are locked)
10|3000 characters needed characters left

If I recall correctly, OnCollisionStay events stop happening if the rigidbody has gone to sleep. Use rigidbody.WakeUp() each FixedUpdate to keep it awake.

more ▼

answered Apr 02 '10 at 11:18 AM

duck gravatar image

duck ♦♦
40.9k 92 148 415

Ok, thanks, i'll try that and report back.

Apr 03 '10 at 04:24 AM RCIX

I added that, and it didn't help. Do you have a better suggestion for a jumping method? :)

Apr 04 '10 at 04:23 AM RCIX
(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:

x5055
x335
x185

asked: Apr 02 '10 at 09:50 AM

Seen: 1619 times

Last Updated: Apr 02 '10 at 09:50 AM