I’m having a lot of problems because I’m bad programming with a code. You can put a UI Button and it works as if it were the “A” or “LeftArrow” and put another UI Button and that works like “D” or “RightArrow”?
For example: Onclick is true then it is equal to “A” pressed.
Thanks a lot.
Hmm… what exactly are you trying to do?
Why not just call the target functions directly based on input from the keyboard?
do you have the basic keycode working? do you have
if (Input.GetKeyDown("A"))
{
print("A key was pressed down");
//do some more code
}
if so then you can make a function to receive the button click
something like this
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
print("A key was pressed down");
AbtnPressed();
}
if (Input.GetKeyDown(KeyCode.D))
{
print("D key was pressed down");
DbtnPressed();
}
if (Input.GetKeyDown(KeyCode.W))
{
print("W key was pressed down");
WbtnPressed();
}
if (Input.GetKeyDown(KeyCode.S))
{
print("S key was pressed down");
SbtnPressed();
}
}
public void AbtnPressed() //this is public, so the UI button can use it with an OnClick event
{
//do some code
}
public void DbtnPressed() //this is public, so the UI button can use it with an OnClick event
{
//do some code
}
public void WbtnPressed() //this is public, so the UI button can use it with an OnClick event
{
//do some code
}
public void SbtnPressed() //this is public, so the UI button can use it with an OnClick event
{
//do some code
}
I want my left button and my right button to act like the “A” and “D” for this code:
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Part of my code:
//UI buttons
if(botonIzquierda.pulsado) //left button
{
¿?¿?¿?¿?¿?¿?¿?¿?¿?¿?¿
}
else if(botonDerecha.pulsado)//right button
{
¿?¿?¿?¿?¿?¿?¿?
}
else
{
}
//Move and animation
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 inputDir = input.normalized;
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
if (inputDir != Vector2.zero)
{
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
}
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
velocityY += Time.deltaTime * gravity;
controller.Move(velocity * Time.deltaTime);
float animationSpeedPercent = ((running) ? 2 : .5f) * input.magnitude;
animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}
I do not understand how to do it and I’m desperate. I’ve been looking for a solution for 2 weeks.
I’m not surprised. You’re trying to use code when Unity expects you to do it through the Input Manager.
watch this video about UI buttons to learn how to get the Onclick event working
Ok ok, I’m not a newbie at all, I understand the code when I read it, I only have this problem on time because this way I can not do it and I would like to do it. It’s not the first game I play, but it’s in this style.I’ve tried to simplify my question. Why does not my character move?The joystick works perfect, it is added in the inspector to the character script.
My joystick button code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
public Vector3 InputDirection { set; get;}
private void Start()
{
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
InputDirection = Vector3.zero;
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos = Vector2.zero;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImg.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
pos.y = (pos.y / bgImg.rectTransform.sizeDelta.y);
float x = (bgImg.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
float y = (bgImg.rectTransform.pivot.y == 1) ? pos.y * 2 + 1 : pos.y * 2 - 1;
InputDirection = new Vector3(x, 0, y);
InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
joystickImg.rectTransform.anchoredPosition = new Vector3(InputDirection.x * (bgImg.rectTransform.sizeDelta.x / 3), InputDirection.z* (bgImg.rectTransform.sizeDelta.y / 3));
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
InputDirection = Vector3.zero;
joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}
}
My character controller code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class movimientopj : MonoBehaviour {
public float runspeed = 6;
public float walkspeed = 2;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
float velocityY;
public float gravity = -12;
public float jumpHeight = 1;
[Range (0,1)]
public float airControlPercent;
private Rigidbody controlador;
public ElementosInteractivos botonIzquierda;
public ElementosInteractivos botonDerecha;
public VirtualJoystick moveJoystick;
Animator animator;
CharacterController controller;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
controlador = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
//UI buttons
//Move and animation
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"),0);
Vector3 inputDir = input.normalized;
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
if (inputDir != Vector3.zero)
{
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
}
bool running = Input.GetKey(KeyCode.LeftShift);
//bool jumping = Input.GetKey(KeyCode.Space);
float targetSpeed = ((running) ? runspeed : walkspeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
velocityY += Time.deltaTime * gravity;
controller.Move(velocity * Time.deltaTime);
if (controller.isGrounded == false)
{
enelaire();
}
float animationSpeedPercent = ((running) ? 2 : .5f) * input.magnitude;
animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
input.x = Input.GetAxisRaw("Horizontal");
input.z = Input.GetAxisRaw("Vertical");
}
void Jump()
{
if (controller.isGrounded)
{
float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
velocityY = jumpVelocity;
}
}
This is my proyect: