hi everybody, I want to rotate my player with my virtual joystick along the Y axis and I don’t know how to do it. Can you help me please?
sorry for my bad English
finally I found the solution
######my Virtual Joystick script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class VirtualJoystick2 : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;
private void Start() {
bgImg = GetComponent();
joystickImg = transform.GetChild(0).GetComponent();
}
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.x2+1: pos.x2-1;
float y = (bgImg.rectTransform.pivot.y == 1)?pos.y2+1: pos.y2-1;
inputVector = new Vector3(pos.x2 + 1,0,pos.y2 - 1);
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));
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector3.zero;
joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}
public Vector3 GetDirection() {
return new Vector3(inputVector.x,0,inputVector.z);
}
public float Horizontal() {
if (inputVector.x != 0)
return inputVector.x;
else
return Input.GetAxis (“Horizontal”);
}
public float Vertical() {
if (inputVector.z != 0)
return inputVector.z;
else
return Input.GetAxis (“Vertical”);
}
}
######my script :
using UnityEngine;
using System.Collections;
public class turning: MonoBehaviour
{
public VirtualJoystick2 joystick2;
public float speed = 6f; // The speed that the player will move at.
public VirtualJoystick joystick;
Rigidbody playerRigidbody; // Reference to the player's rigidbody.
void Awake ()
{
playerRigidbody = GetComponent <Rigidbody> ();
}
public void FixedUpdate ()
{
// Turn the player to face the mouse cursor.
Turning ();
}
void Turning ()
{
float h = joystick2.Horizontal ();
float v = joystick2.Vertical ();
float angle = Mathf.Atan2(h, v )* Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, angle, 0));
}
}