Hey, im relatively new to scripting and have been working on a simple 2D move and jump script, im trying to find a way to prevent double/multiple jumps without ray or line casting, so far i have this
var moveSpeed: float = 5.0;
var jumpHeight: float = 500;
function Start()
{
}
function Update()
{
//move player left
if (Input.GetKey("left") || Input.GetKey("a")) {
transform.position -= Vector3.right * moveSpeed * Time.deltaTime;
}
//move player right
if (Input.GetKey("right") || Input.GetKey ("d")) {
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
}
{
//if over min jump height do nothing
if(jumpHeight >= 10)
{
}
}
//if below min jump height then jump
if(jumpHeight <= 10)
{
if (Input.GetButtonDown("Jump") || Input.GetKey("w")) {
rigidbody2D.AddForce(new Vector3(0, jumpHeight,0)) ;
}
}
any advice on why this doesnt work would be appreciated
All of your Jump code is outside of your Update() function. In Monodevelop, if you place your cursor at one brace, it will highlight the matching brace. So if you place your cursor at the first brace after ‘{’, you will see the matching brace is at line 29.
I recommend you pick a bracketing and indentation style, and then be vary maticulous about following it. You will find many of your own issues this way, and avoid others. Also on line 48, ‘GetKey’ should be ‘GetKeyDown’. Here is your code boiled down a bit:
var moveSpeed: float = 5.0;
var jumpHeight: float = 500;
function Update()
{
//move player left
if (Input.GetKey("left") || Input.GetKey("a"))
{
transform.position -= Vector3.right * moveSpeed * Time.deltaTime;
}
//move player right
if (Input.GetKey("right") || Input.GetKey ("d"))
{
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
//if below min jump height then jump
if(jumpHeight <= 10)
{
if (Input.GetButtonDown("Jump") || Input.GetKeyDown("w"))
{
rigidbody2D.AddForce(new Vector3(0, jumpHeight,0)) ;
}
}
}