- Home /
Question by
alwaysivar · Oct 06, 2017 at 03:40 PM ·
uibuttonbutton trigger events
Buttons not working properly.
i am working on a 2d project and want to make it for mobile but for some reason my code on my MoveRight button works fine but my MoveLeft button doesn't.
Playercontroller script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityStandardAssets.CrossPlatformInput;
public class playercontroller : MonoBehaviour {
public float speedForce= 50f;
public Vector2 jumpVector;
public bool isGrounded;
public bool canMove;
float speed;
public Transform grounder;
public float radiuss;
public LayerMask ground;
Animator anim;
// Use this for initialization
void Start () {
canMove = true;
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate () {
isGrounded = Physics2D.OverlapCircle (grounder.transform.position, radiuss, ground);
}
public void PMoveLeft (){
speed = isGrounded ? speedForce : speedForce * 0.7f;
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-speed, 0);
transform.localScale = new Vector3 (-1, 1, 1);
anim.SetInteger ("AnimationState", 1);
}
public void PMoveRight (){
speed = isGrounded ? speedForce : speedForce * 0.7f;
GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, 0);
transform.localScale = new Vector3 (1, 1, 1);
anim.SetInteger ("AnimationState", 1);
}
public void PJump(){
GetComponent<Rigidbody2D>().AddForce (jumpVector, ForceMode2D.Force);
}
public void PNotMove(){
GetComponent<Rigidbody2D> ().velocity = Vector2.zero;
anim.SetInteger ("AnimationState", 0);
}
void OnDrawGizmos()
{
Gizmos.color = Color.white;
Gizmos.DrawWireSphere (grounder.transform.position, radiuss);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "deadly") {
Debug.Log("Dead!");
Application.LoadLevel(0);
}
}
void OnTriggerStay2D(Collider2D other)
{
if (other.tag == "deadly") {
Debug.Log("Dead!");
Application.LoadLevel(0);
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "deadly") {
Debug.Log("Dead!");
Application.LoadLevel(0);
}
}
}
MoveLeft script (attached to left button):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MoveLeft : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
bool pointerDown = false;
bool pointerUp = true;
public GameObject Character;
public void Start()
{
}
public void OnPointerDown(PointerEventData eventData)
{
pointerDown = true;
pointerUp = false;
}
public void OnPointerUp(PointerEventData eventData)
{
pointerDown = false;
pointerUp = true;
}
void Update()
{
GameObject Character = GameObject.Find("Character");
playercontroller PlayerController = Character.GetComponent<playercontroller>();
if (pointerDown)
{
PlayerController.PMoveLeft ();
}
if (pointerUp) {
PlayerController.PNotMove ();
}
}
}
MoveRight Script(attached to right button):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MoveRight : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
bool pointerDown = false;
bool pointerUp = true;
public GameObject Character;
public void Start()
{
}
public void OnPointerDown(PointerEventData eventData)
{
pointerDown = true;
pointerUp = false;
}
public void OnPointerUp(PointerEventData eventData)
{
pointerDown = false;
pointerUp = true;
}
void Update()
{
GameObject Character = GameObject.Find("Character");
playercontroller PlayerController = Character.GetComponent<playercontroller>();
if (pointerDown)
{
PlayerController.PMoveRight ();
}
if (pointerUp) {
PlayerController.PNotMove ();
}
}
}
Help would be appreciated.
Comment
Your answer
Follow this Question
Related Questions
Highlight images in buttons children 0 Answers
UI button in Panel not clickable 2 Answers
Problem with button On Click 1 Answer
why won't my add button script work?!? (help appreciated) 2 Answers
Issue with spawning buttons and assigning listeners 1 Answer