- Home /
 
               Question by 
               MooGaming227 · Oct 18, 2016 at 05:14 AM · 
                mobilejoysticktopdownshooter  
              
 
              Make joystick rotate object
Hi i just had a problem trying to make a 2d top down shooter. How can i get the joystick position to change the rotation of the graphic as its turned(moving is done). This is the joysticks code:
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections;
public class JoystickController : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler { private Image backgroundImg; private Image JoystickImg;
 public Vector3 InputDirection{ set; get; }
 public float angle;
 public GameObject Graphic;
 private Vector3 rotationDirection;
 private Quaternion rotation;
 private void Start(){
     backgroundImg = GetComponent<Image> ();
     JoystickImg = transform.GetChild(0).GetComponent<Image> ();
 }
 public virtual void OnDrag(PointerEventData ped){
     Vector2 pos = Vector2.zero;
     if (RectTransformUtility.ScreenPointToLocalPointInRectangle (backgroundImg.rectTransform, ped.position, ped.pressEventCamera, out pos)) {
         pos.x = (pos.x / backgroundImg.rectTransform.sizeDelta.x);
         pos.y = (pos.y / backgroundImg.rectTransform.sizeDelta.y);
         float x = (backgroundImg.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
         float y = (backgroundImg.rectTransform.pivot.y == 1) ? pos.y * 2 + 1 : pos.y * 2 - 1;
         InputDirection = new Vector3 (x, 0, y);
         InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
         JoystickImg.rectTransform.anchoredPosition = new Vector3 (InputDirection.x * (backgroundImg.rectTransform.sizeDelta.x / 3), InputDirection.z * (backgroundImg.rectTransform.sizeDelta.y / 3));
     }
 }
 public virtual void OnPointerDown(PointerEventData ped){
     OnDrag (ped);
 }
 public virtual void OnPointerUp(PointerEventData ped){
     InputDirection = Vector3.zero;
     JoystickImg.rectTransform.anchoredPosition = Vector3.zero;
 }
 void Update(){
 }
}
Thank you!
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                