- Home /
Making player jump when left mouse clicked
so i have this script error. I want to make the player jump when the left mouse button is clicked but it doesn't work. I think its a really simple solution ,but for some reason i just dont know what to do.
So this is my script
using UnityEngine;
[RequireComponent(typeof(PlatformerCharacter2D))] public class Platformer2DUserControl : MonoBehaviour { private PlatformerCharacter2D character; private bool jump;
void Awake()
{
character = GetComponent<PlatformerCharacter2D>();
}
void Update ()
{
// Read the jump input in Update so button presses aren't missed.
if (CrossPlatformInput.GetMouseButtonDown("Jump")) jump = true;
if (Input.GetButtonDown("Jump")) jump = true;
}
void FixedUpdate()
{
character.Move( 1, false , jump );
jump = false;
}
}
something about this part is wrong
void Update () { // Read the jump input in Update so button presses aren't missed.
if (CrossPlatformInput.GetMouseButtonDown("Jump")) jump = true;
if (Input.GetButtonDown("Jump")) jump = true;
}
Please help me , thanks in advance !
Answer by locrian05 · Feb 24, 2014 at 06:56 PM
Try this, though its on Java Script.
static var jumping : boolean; //make this global
function Update () {
if(!jumping && Input.GetMouseButtonDown(0)) {
//jumpcommand here,
//I would use rigidbody2D.velocity.y = 5
//then tell jumping is true;
jumping = true;
}
}
then add a collider trigger to turn jumping to false. This script is on a platform or ground.
function OnTriggerEnter2D (hitInfo : Collider) {
if(hitInfo.name == "player") {
jumpScript.jumping = false;
}
}
Your answer
Follow this Question
Related Questions
jump with character controller best way? 1 Answer
Visual Studio Code Intellisense 0 Answers
Not able to jump 1 Answer
Can't figure out how to detect ground (2D) 1 Answer
Revise one conflicting code line? 1 Answer