- Home /
How do I make my player jump on click on the button?
Help me to create jumping script for button. I'm making android inputs, so I need jump button! I found some codes, but it doesn't work. Thank you in advance!
Answer by sachinbirajdar · Aug 26, 2017 at 12:16 PM
When the Jump button is triggered try adding some force in y direction to your player.This code should help. Customize it according to your needs.
public float force; public Rigidbody player;
void Start()
{
player= GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (CrossPlatformInputManager.GetButton ("Jump"))
{
player.AddForce(transform.up * force);
}
}
Thank you so much, this is work with space. How can I use button ins$$anonymous$$d of SPACE?
Answer by PersianKiller · Aug 26, 2017 at 05:24 PM
It worked for me ,hope will help you too.
I created a script like this
public class touch : MonoBehaviour {
public PlayerController player;
void Start(){
player = FindObjectOfType<PlayerController> ();
// I worte jumping scripts in PlayerController .and with player I can access it.
}
public void jump(){
player.Jump = true;
}
then go to unity and attach the script to the canvas or to the button.then click on your button .
and you can see what to do in the image :)
so when you click on the button Jump function of PlayerController will do jumping stuffs. :)
Answer by vanjasretenovic123 · Aug 26, 2017 at 09:41 PM
I made this script and I did what you said, but it does not work again.
it's all you need ,
//this code is for canvas or button
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Button : $$anonymous$$onoBehaviour {
public playerController player;
// Use this for initialization
void Start () {
player = FindObjectOfType<playerController> ();
}
// Update is called once per frame
void Update () {
}
public void makePlayerJump(){
// player = FindObjectOfType<playerController> ();
player.jump ();
}
}
and it's for your player or whatever that you want to make it jump
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerController : $$anonymous$$onoBehaviour {
Rigidbody2D rig;
public float jumpHeight;
// Use this for initialization
void Start () {
rig = GetComponent<Rigidbody2D> ();
}
public void jump(){
rig.velocity = new Vector2 (0,jumpHeight);
//or you can use addforce
}
}
watch this it will help you :) https://www.youtube.com/watch?v=6WCXhQOukFQ&feature=youtu.be