Joystick mobile
3 participantes
Página 1 de 1
Joystick mobile
Eu to fazendo um jogo para Android de fps, script do joystick ta certo só que o player n se move e n tem erro
Acho que tenho que colocar alguma coisa no script do player só que eu n sei oque.
using UnityEngine;
public class Player : MonoBehaviour {
CharacterController controller;
Vector3 forward;
Vector3 strafe;
Vector3 vertical;
float forwardSpeed = 5f;
float strafeSpeed = 5f;
float gravity;
float jumpSpeed;
float maxJumpHeight = 2f;
float timeToMaxHeight = 0.5f;
public VirtualJoystick joystick;
void Start() {
controller = GetComponent<CharacterController>();
gravity = (-2 * maxJumpHeight) / (timeToMaxHeight * timeToMaxHeight);
jumpSpeed = (2 * maxJumpHeight) / timeToMaxHeight;
}
void Update() {
float forwardInput = Input.GetAxisRaw("Vertical");
float strafeInput = Input.GetAxisRaw("Horizontal");
// force = input * speed * direction
forward = forwardInput * forwardSpeed * transform.forward;
strafe = strafeInput * strafeSpeed * transform.right;
vertical += gravity * Time.deltaTime * Vector3.up;
if(controller.isGrounded) {
vertical = Vector3.down;
}
if(Input.GetKeyDown(KeyCode.Space) && controller.isGrounded) {
vertical = jumpSpeed * Vector3.up;
}
if (vertical.y > 0 && (controller.collisionFlags & CollisionFlags.Above) != 0) {
vertical = Vector3.zero;
}
Vector3 finalVelocity = forward + strafe + vertical;
controller.Move(finalVelocity * Time.deltaTime);
}
}
Acho que tenho que colocar alguma coisa no script do player só que eu n sei oque.
using UnityEngine;
public class Player : MonoBehaviour {
CharacterController controller;
Vector3 forward;
Vector3 strafe;
Vector3 vertical;
float forwardSpeed = 5f;
float strafeSpeed = 5f;
float gravity;
float jumpSpeed;
float maxJumpHeight = 2f;
float timeToMaxHeight = 0.5f;
public VirtualJoystick joystick;
void Start() {
controller = GetComponent<CharacterController>();
gravity = (-2 * maxJumpHeight) / (timeToMaxHeight * timeToMaxHeight);
jumpSpeed = (2 * maxJumpHeight) / timeToMaxHeight;
}
void Update() {
float forwardInput = Input.GetAxisRaw("Vertical");
float strafeInput = Input.GetAxisRaw("Horizontal");
// force = input * speed * direction
forward = forwardInput * forwardSpeed * transform.forward;
strafe = strafeInput * strafeSpeed * transform.right;
vertical += gravity * Time.deltaTime * Vector3.up;
if(controller.isGrounded) {
vertical = Vector3.down;
}
if(Input.GetKeyDown(KeyCode.Space) && controller.isGrounded) {
vertical = jumpSpeed * Vector3.up;
}
if (vertical.y > 0 && (controller.collisionFlags & CollisionFlags.Above) != 0) {
vertical = Vector3.zero;
}
Vector3 finalVelocity = forward + strafe + vertical;
controller.Move(finalVelocity * Time.deltaTime);
}
}
maxwellvale- Iniciante
- PONTOS : 2142
REPUTAÇÃO : 0
Respeito as regras :
Re: Joystick mobile
Script do joystick
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;
public GameObject player;
public float speed;
private void Start()
{
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
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);
inputVector = new Vector3(pos.x * 3, 0, pos.y * 3);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
// Move Joystick IMG
joystickImg.rectTransform.anchoredPosition =
new Vector3(inputVector.x * (bgImg.rectTransform.sizeDelta.x / 3)
, inputVector.z * (bgImg.rectTransform.sizeDelta.y / 3));
}
}
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;
public GameObject player;
public float speed;
private void Start()
{
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
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);
inputVector = new Vector3(pos.x * 3, 0, pos.y * 3);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
// Move Joystick IMG
joystickImg.rectTransform.anchoredPosition =
new Vector3(inputVector.x * (bgImg.rectTransform.sizeDelta.x / 3)
, inputVector.z * (bgImg.rectTransform.sizeDelta.y / 3));
}
}
maxwellvale- Iniciante
- PONTOS : 2142
REPUTAÇÃO : 0
Respeito as regras :
Tópicos semelhantes
» [DÚVIDA] Ajuda com joystick em mobile...
» Joystick Mobile do Marcos Schult
» CONTROLES JOYSTICK PARA MOBILE (ANDROID e IOS)
» [TUTORIAL] Joystick virtual para mobile
» Como Dar Play Em Uma Animaçao No Joystick mobile do Unity
» Joystick Mobile do Marcos Schult
» CONTROLES JOYSTICK PARA MOBILE (ANDROID e IOS)
» [TUTORIAL] Joystick virtual para mobile
» Como Dar Play Em Uma Animaçao No Joystick mobile do Unity
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos