- Home /
How to make a gameobject face the same direction the joystick is pointing to?
I'm working on a 3d game and I want a capsule to face the same direction the joystick is pointing to, I've tried for days now and this single thing is driving me nuts. This is the code of the Joystick I use:
using UnityEngine;
using UnityEngine.EventSystems;
public class FloatingJoystick : Joystick
{
Vector2 joystickCenter = Vector2.zero;
void Start()
{
background.gameObject.SetActive(false);
}
public override void OnDrag(PointerEventData eventData)
{
Vector2 direction = eventData.position - joystickCenter;
inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
}
public override void OnPointerDown(PointerEventData eventData)
{
background.gameObject.SetActive(true);
background.position = eventData.position;
handle.anchoredPosition = Vector2.zero;
joystickCenter = eventData.position;
}
public override void OnPointerUp(PointerEventData eventData)
{
background.gameObject.SetActive(false);
inputVector = Vector2.zero;
}
}
The thing is that i'm just a begginer and I don't even know which values does this code give, I've tried with Horizontal and Vertical values to rotate the character, and altough it works with the arrow keys it doesn't with the Joystick I use, this is my current "rotation" code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour {
public Transform Rotate;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
Vector3 direction = transform.eulerAngles = new Vector3(0, Mathf.Atan2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")) * 180 / Mathf.PI, 0);
}
}
Rotate is the name of the Joystick in the game. If you could give me a full code in C# so that the player faces the direction the joystick is pointing to, I would thank you so much, I've tried with MANY different codes but they didn't work, help pleasee!!!!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Player Sprint button not working properly... 2 Answers
UnityScript to C# conversion help? 1 Answer