- Home /
Question by
Valentinon13 · Oct 17, 2016 at 08:42 AM ·
playerplayerprefsplayer movement
joystick script not working need some help !
I made a Joystick controller script but if i toggel the box in game for left move it works but if i push the button that i supost to hit it is not working ?????
using UnityEngine;
using System.Collections;
public class JoyStickController : MonoBehaviour {
public delegate void OnMove(Vector3 vec3);
public event OnMove OnCommandMove;
public WachButton Left;
public WachButton Right;
public WachButton Backward;
public WachButton Forward;
[HideInInspector]
public GameObject playerObj;
public bool leftMove;
public bool rightMove;
public bool backMove;
public bool frontMove;
void Start()
{
ActionJoystick();
playerObj = new GameObject();
}
public void ActionJoystick()
{
Left.OnPress += OnPress;
Right.OnPress += OnPress;
Backward.OnPress += OnPress;
Forward.OnPress += OnPress;
}
void OnPress (GameObject unit, bool state)
{
switch ( unit.name)
{
case "Left":
LeftMove(state);
break;
case "Right":
RightMove(state);
break;
case "Backward":
BackMove(state);
break;
case "Forward":
FrontMove(state);
break;
}
Debug.Log(unit.name);
}
private void LeftMove(bool state)
{
leftMove = state;
}
private void RightMove(bool state)
{
rightMove = state;
}
private void BackMove(bool state)
{
backMove = state;
}
private void FrontMove(bool state)
{
frontMove = state;
}
void Update ()
{
Transform tranf = playerObj.transform;
if( leftMove )
{
playerObj.transform.position = new Vector3(tranf.position.x - (2f * Time.deltaTime), tranf.position.y, tranf.position.z);
if( OnCommandMove != null)
{
OnCommandMove(playerObj.transform.position);
}
}
if (rightMove)
{
playerObj.transform.position = new Vector3(tranf.position.x + (2f * Time.deltaTime), tranf.position.y, tranf.position.z);
if (OnCommandMove != null)
{
OnCommandMove(playerObj.transform.position);
}
}
if (backMove)
{
playerObj.transform.position = new Vector3(tranf.position.x , tranf.position.y, tranf.position.z - (2f * Time.deltaTime));
if (OnCommandMove != null)
{
OnCommandMove(playerObj.transform.position);
}
}
if (frontMove)
{
playerObj.transform.position = new Vector3(tranf.position.x , tranf.position.y, tranf.position.z + (2f * Time.deltaTime));
if (OnCommandMove != null)
{
OnCommandMove(playerObj.transform.position);
}
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;
public class WachButton : MonoBehaviour {
public delegate void OnActionPress(GameObject unit, bool state);
public event OnActionPress OnPress;
EventTrigger eventTrigger;
void Start()
{
Debug.Log(this.gameObject.name);
eventTrigger = this.gameObject.GetComponent<EventTrigger>();
AddEventTrigger(OnPointDown, EventTriggerType.PointerDown);
AddEventTrigger(OnPointUp, EventTriggerType.PointerUp);
}
void AddEventTrigger( UnityAction action, EventTriggerType triggerType)
{
EventTrigger.TriggerEvent trigger = new EventTrigger.TriggerEvent();
trigger.AddListener((eventData) => action());
EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigger, eventID = triggerType };
eventTrigger.triggers.Add(entry);
}
void OnPointDown()
{
if( OnPress != null)
{
OnPress(this.gameObject, true);
}
}
void OnPointUp()
{
if(OnPress != null)
{
OnPress(this.gameObject, false);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
player not moving over the bridge 1 Answer
How to disable writing to Windows registry? 0 Answers
Player won't stop moving while blocking 1 Answer
Player cannot jump 0 Answers