- Home /
Problem with CrossPlaformInputManager.GetButton ()
Basically want an object to jump when I hit d space bar or a UIbutton (Wen Mobile Input is enabled). So i used if(CrossPlaformInputManager. GetButton ("Jump")) {n the function to be performed}.
And i added ButtonHandler script to d UIButton and named it Jump.
But wt happens is wen i hit d space bar ' Jump(); "is performed i. e. D object jumps (so there is probably no issue with d Jump(); method) BUT wen i Click on d UIBUTTON ( after enabling Mobile Input)... Nothing Happens. Only the Button gets highlighted, that' s it.
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;
public class Movement2 : MonoBehaviour
{
public static Movement2 singleton;
public float movespeed;
CharacterController charactercont;
public float gravity;
RaycastHit hit;
internal float Horizontal;
public float JumpSpeed;
internal float Fallspeed;
public float rotspeed;
void Awake()
{
if (singleton != null)
{
Destroy(gameObject);
}
else
{
singleton = this;
}
}
// Use this for initialization
void Start()
{
charactercont = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Fall();
Movex();
Jump();
Respawn();
}
public void Jump()
{
if (charactercont.isGrounded == true && CrossPlatformInputManager.GetButton("Jump"))
{
Fallspeed = -JumpSpeed;
charactercont.Move(new Vector3(0, -Fallspeed, 0));
}
}
void Fall()
{
if (charactercont.isGrounded == false)
{
Fallspeed += gravity * Time.deltaTime;
}
else
{
Fallspeed = 0;
}
}
void Movex()
{
charactercont.Move(new Vector3(CrossPlatformInputManager.GetAxis("Horizontal") * movespeed * Time.deltaTime, -Fallspeed, 0));
transform.Rotate(new Vector3(0, 0, -rotspeed * Time.deltaTime * CrossPlatformInputManager.GetAxis("Horizontal")));
}
void Respawn()
{
if (transform.position.y <= -40)
{
transform.position = checkpoint.point;
}
}
}
With nothing you mean what, the button is not even clicked?
I am sorry for being less precise or missing out any of the details.Actually I am new to Unity and posted here for the 1st time. Well the Button is highlighted but the function is not executed.I have added my script so u can check.
Won't help. If the button responds, have you set it up properly? The OnClick event I mean?
But if I am using CrossPlatformInput$$anonymous$$anager then do i need to use OnClick events and set functions and all....??! Because I had just assigned AxisTouchButton($$anonymous$$ept OnClick remain Empty) script to 2 arrow buttons(Changed its axis values accordingly) and it worked. Similarly I added ButtonHandler script to Jump UIButton but there seems to be no response apart from highlight.
I font know what those scripts do. Can you post them if they're not too long?
Answer by yjaveri99 · Jun 22, 2017 at 08:22 AM
I added EventTrigger' s PointDownEvent and set the SetUp and SetDown both states to the button.And now it works exactly how i wanted.