Joystick Mobile do Marcos Schult
Página 1 de 1
Joystick Mobile do Marcos Schult
Boa noite senhores eu gostaria como posso adaptar esse script que eu fiz com o Josytick Mobile do Marcus
Meu script
Script do Joystick do Marcus
Meu script
- Código:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovimentoBeta : MonoBehaviour
{
public Camera camera;
private float rotateSpeed = 20;
private float ray_d = 0.3f;
private float movimento = 0;
public float WalkSpeed = 20;
public float RunSpeed = 25;
Rigidbody rb;
private bool onMove;
private float gravity = -7;
public float jump = 10;
public bool Pulando = true;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
rb = GetComponent<Rigidbody>();
rb.interpolation = RigidbodyInterpolation.Extrapolate;
}
private void Update()
{
////Crea un rayo para detectar el piso
RaycastHit Ray_Wall;
Vector3 PhysicsCentre = this.transform.position + this.GetComponent<CapsuleCollider>().center;
if (Physics.Raycast(PhysicsCentre, transform.forward, out Ray_Wall, ray_d)) { if (Ray_Wall.transform.gameObject.tag != "Player") { onMove = false; } } else { onMove = true; }
Debug.DrawRay(PhysicsCentre, transform.forward * ray_d, Color.green);
}
// Update is called once per frame
void FixedUpdate()
{
var vel = transform.forward * 0;
vel.y = gravity;
rb.velocity = vel;
//Controle de Pulo
if (Input.GetButton("Pulo") && Pulando == true)
{
transform.position += (Vector3.up * jump * Time.deltaTime);
gravity = 0;
StartCoroutine(coldown());
}
if (Input.GetKey("f"))
{
Pulando = true;
}
//Controle de Movimento
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
Quaternion direction = new Quaternion();
Vector3 camera3 = camera.transform.rotation.eulerAngles;
camera3 = new Vector3(0, camera3.y, 0);
direction = Quaternion.Euler(camera3);
transform.rotation = Quaternion.Slerp(transform.rotation, direction, rotateSpeed * Time.deltaTime);
vel = transform.forward * movimento;
vel.y = gravity;
rb.velocity = vel;
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.LeftArrow))
{
Quaternion direction = new Quaternion();
Vector3 camera3 = camera.transform.rotation.eulerAngles;
camera3 = new Vector3(0, camera3.y, 0);
camera3 += new Vector3(0, 90, 0);
direction = Quaternion.Euler(camera3);
transform.rotation = Quaternion.Slerp(transform.rotation, direction, rotateSpeed * Time.deltaTime);
vel = transform.forward * movimento;
vel.y = gravity;
rb.velocity = vel;
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.RightArrow))
{
Quaternion direction = new Quaternion();
Vector3 camera3 = camera.transform.rotation.eulerAngles;
camera3 = new Vector3(0, camera3.y, 0);
camera3 += new Vector3(0, -90, 0);
direction = Quaternion.Euler(camera3);
transform.rotation = Quaternion.Slerp(transform.rotation, direction, rotateSpeed * Time.deltaTime);
vel = transform.forward * movimento;
vel.y = gravity;
rb.velocity = vel;
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
Quaternion direction = new Quaternion();
Vector3 camera3 = camera.transform.rotation.eulerAngles;
camera3 = new Vector3(0, camera3.y, 0);
camera3 += new Vector3(0, 180, 0);
direction = Quaternion.Euler(camera3);
transform.rotation = Quaternion.Slerp(transform.rotation, direction, rotateSpeed * Time.deltaTime);
vel = transform.forward * movimento;
vel.y = gravity;
rb.velocity = vel;
}
//Controle de Correr
if (Input.GetKey(KeyCode.LeftShift))
{
movimento = RunSpeed;
}
else
{
movimento = WalkSpeed;
}
Vector2 movement = new Vector2(Mathf.Abs(Input.GetAxis("Vertical")), Mathf.Abs(Input.GetAxis("Horizontal")));
Debug.Log(movement);
gravity = -7;
}
public IEnumerator coldown()
{
Pulando = true;
yield return new WaitForSeconds(0.1f);
Pulando = false;
}
}
Script do Joystick do Marcus
- Código:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
[RequireComponent(typeof(RectTransform))]
public class MSJoystick : UIBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
[SerializeField]
RectTransform _joystickGraphic;
Vector2 _axis;
bool _isDragging;
[HideInInspector]
public float joystickY;
[HideInInspector]
public float joystickX;
private bool walk;
RectTransform _rectTransform;
public RectTransform rectTransform
{
get
{
if (!_rectTransform)
{
_rectTransform = transform as RectTransform;
}
return _rectTransform;
}
}
public void OnBeginDrag(PointerEventData eventData)
{
if (!IsActive())
{
return;
}
EventSystem.current.SetSelectedGameObject(gameObject, eventData);
Vector2 newAxis = transform.InverseTransformPoint(eventData.position);
newAxis.x /= rectTransform.sizeDelta.x * 0.5f;
newAxis.y /= rectTransform.sizeDelta.y * 0.5f;
SetAxisMS(newAxis);
_isDragging = true;
}
public void OnEndDrag(PointerEventData eventData)
{
_isDragging = false;
}
public void OnDrag(PointerEventData eventData)
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out _axis);
_axis.x /= rectTransform.sizeDelta.x * 0.5f;
_axis.y /= rectTransform.sizeDelta.y * 0.5f;
SetAxisMS(_axis);
}
void OnDeselect()
{
_isDragging = false;
}
void LateUpdate()
{
if (!_isDragging)
{
if (_axis != Vector2.zero)
{
Vector2 newAxis = _axis - (_axis * Time.deltaTime * 25.0f);
if (newAxis.sqrMagnitude <= 0.1f)
{
newAxis = Vector2.zero;
}
SetAxisMS(newAxis);
}
}
}
public void SetAxisMS(Vector2 axis)
{
_axis = Vector2.ClampMagnitude(axis, 1);
UpdateJoystickGraphicMS();
joystickY = _axis.y;
joystickX = _axis.x;
}
void UpdateJoystickGraphicMS()
{
if (_joystickGraphic)
{
_joystickGraphic.localPosition = _axis * Mathf.Max(rectTransform.sizeDelta.x, rectTransform.sizeDelta.y) * 0.2f;
{
walk = true;
}
}
}
#if UNITY_EDITOR
protected override void OnValidate() {
base.OnValidate();
UpdateJoystickGraphicMS();
}
#endif
}
- Código:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class MSJoystickController : MonoBehaviour
{
public MSJoystick joystick;
public static Vector2 joystickInput;//é possível acessar através de MSJoystickController.joystickInput
void Update()
{
if (joystick)
{
joystickInput = new Vector2(joystick.joystickX, joystick.joystickY);
}
}
}
teos626- Membro
- PONTOS : 1930
REPUTAÇÃO : 0
Respeito as regras :
Tópicos semelhantes
» Joystick mobile
» Pessoal como posso modificar esses script para Joystick feito pelo Marcos Schultz
» [DÚVIDA] Ajuda com joystick em mobile...
» CONTROLES JOYSTICK PARA MOBILE (ANDROID e IOS)
» [TUTORIAL] Joystick virtual para mobile
» Pessoal como posso modificar esses script para Joystick feito pelo Marcos Schultz
» [DÚVIDA] Ajuda com joystick em mobile...
» CONTROLES JOYSTICK PARA MOBILE (ANDROID e IOS)
» [TUTORIAL] Joystick virtual para mobile
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos