- Home /
2D mobile: Need my character to rotate Z axis towards touch position
Hi, I'm new to Unity, programming and stuff. My math understanding is very basic.
I have created code ( some taken from tutorials, some made by me ) that creates a virtual joystick.
The virtual joystick works like this: It stores the initial touch location ( TouchPhase.Began ) and calculates the direction where the user is moving or has moved the finger ( TouchPhase.Moved, TouchPhase.Stationary ) relative to the initial touch location.
It then moves the GameObject accordingly.
Now, I need to rotate the GameObject so that it faces the direction of the touch relative to the initial touch location.
This is the code I have for the Joystick. It works nice, feel free to use it if you need it, but please help me figure out this rotation issue.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class InvisibleMovingJoystick : MonoBehaviour
 {
     public GameObject phage;
     public float speed = 1.0f;
     private bool touchStart = false;
     Vector2 pointA;
     Vector2 pointB;
  
 
     // Update is called once per frame
     void Update()
     {
         if(Input.touchCount == 1)
         {
 
             Touch touch = Input.GetTouch(0);
 
             if (touch.phase == TouchPhase.Began)
             {
                 //I'm taking the screen coordinates instead of the world coordinates of the touch
                //So that even if the camera moves, which it does following the character
                //The initial position will never change
                 pointA = touch.position;
                 
             }
 
             if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
             {
 
 
                 pointB = touch.position;
                 touchStart = true;
                 
             }
 
           
             else 
             {
                 touchStart = false;
             }
             
             
         }
         
     }
     private void FixedUpdate()
     {
         if(touchStart == true)
         {
             Vector2 offset = pointB - pointA;
             Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
             
             moveCharacter(direction * 1);
 
             //rotateCharacter();
 
             
             
 
         }
     }
 
     void moveCharacter(Vector2 direction)
 
     {
         phage.transform.Translate(direction * speed * Time.deltaTime);
         
 
         
     }
 
 
 
     void rotateCharacter()
     {
        
       //pls help
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
How to control a 2d character in an top-down android game with the standard joystick? 1 Answer
How to smoothly rotate object in direction of analog joystick? 1 Answer
how do I rotate the player to face the user input smoothly? 1 Answer
Rotating a 2D object when the phone is rotated 1 Answer
Rotate GameObject Z axis only relative to Joystick 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                