Rotate object to joystick and position direction
i want to do rotate object to (joystick) position direction.
using UnityEngine; using System.Collections;
public class ipuclari : MonoBehaviour {
joystick giriliVAl;
Rigidbody2D mybody;
public float speed;
public float rotSpeed = 90f;
void Start ()
{
giriliVAl = GameObject.FindGameObjectWithTag("Joystick").GetComponent<joystick>();
}
void Update()
{
Quaternion rot = transform.rotation;
float z = rot.eulerAngles.z;
z -= Input.GetAxis("Vertical") * Time.deltaTime * rotSpeed;
rot = Quaternion.Euler( 0, 0, z );
transform.rotation = rot;
Vector3 pos = transform.position;
transform.position += giriliVAl.giriliV * Time.deltaTime * speed;
}
}
`using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections;
public class joystick : MonoBehaviour, IPointerDownHandler,IPointerUpHandler,IDragHandler { Image joyArka; Image joys;
public Vector3 giriliV;
void Start()
{
joyArka = gameObject.transform.GetComponent<Image>();
joys = gameObject.transform.GetChild(0).GetComponent<Image>();
}
public void OnDrag(PointerEventData ped)
{
Vector2 pozisyon;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(joyArka.rectTransform,ped.position,ped.enterEventCamera, out pozisyon))
{
pozisyon.x = (pozisyon.x / joyArka.rectTransform.sizeDelta.x);
pozisyon.y = (pozisyon.y / joyArka.rectTransform.sizeDelta.y);
giriliV = new Vector3 (pozisyon.x * 2, pozisyon.y * 2);
giriliV = (giriliV.magnitude > 1.0f) ? giriliV.normalized : giriliV;
joys.rectTransform.anchoredPosition = new Vector3(giriliV.x * joys.rectTransform.sizeDelta.x/3, giriliV.y * joys.rectTransform.sizeDelta.y/3);
}
}
public void OnPointerUp(PointerEventData ped)
{
giriliV = Vector3.zero;
joys.rectTransform.anchoredPosition = Vector3.zero;
}
public void OnPointerDown (PointerEventData ped)
{
OnDrag(ped);
}
}
Comment
Your answer
Follow this Question
Related Questions
ROTATION STOPS when i move the char on the air! 0 Answers
How to not execute certain line of code. 2 Answers
have one object rotate another object on the y axis 0 Answers
how i rotate my player suing the same joystick that i use for moveing 0 Answers
How to rotate an object according to the camera's view? 1 Answer