Juego de Policias y Ladrones Parte 2 Joysticks

Bueno aquí lo que vamos hacer es la segunda parte de nuestro juego. como poner animación a nuestro personaje que en este caso es el Policía, movimiento de Joysticks


bien moviendo el Joysticks moveremos a nuestro personaje. ahora os contate con hemos hecho esto es algo sencillo de hacer para poder controlar a nuestro personaje con el móvil.

Bien en nuestro Unity vamos air Asset Store y hay vamos a poner lo siguiente JoyStick Pack y lo importamos a nuestro juego ,

Una vez hecho esto vamos a crear un Canvas

y en nuestro Canvas vamos a Canvas Scaler (Script) y ponemos «Scale With Screen Size»


Bien y así debería de quedar

Una vez hecho esto  vamos a la pestaña de buscar y ponemos joystick y pinchamos sobre la carpeta Joystick Pack despues sobre la carpeta de Prefabs y una vez hecho esto arrastramos Fixed Joystick al camvas que hemos creado

Bien dentro de Fixed Joystick  tenemos un Handle al que y dentro de Handle vamos a buscar una imagen para nuestro  Joystick

y buscamos la imagen, una vez hecho esto creamos una Script en nuestro Handle

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class joystickbuttoon : MonoBehaviour, IPointerUpHandler,IPointerDownHandler
{
    [HideInInspector]
    protected bool Pressed;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Pressed = true;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Pressed = false;
    }
}

Bien aquí tenemos el Escript de 
Handle  ahora vamos a poner un Script a nuestro personaje el Policía.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class scripandroid : MonoBehaviour
{
    protected Joystick joystick;
    protected joystickbuttoon joystickbutton;

    // Start is called before the first frame update
    void Start()
    {
        joystick = FindObjectOfType<Joystick>();
        joystickbutton = FindObjectOfType<joystickbuttoon>();
        
    }

    // Update is called once per frame
    void Update()
    {
        var rb = GetComponent<Rigidbody2D>();
        rb.velocity = new Vector2(joystick.Vertical * 3f, rb.velocity.y);
        rb.velocity = new Vector2(joystick.Horizontal * 3f, rb.velocity.x);
        
    }
}

ahora ya tendriamos nuestro Joysticks

Continuar escribiendo

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *