Search Unity

Problem with 2D Rotation

Discussion in 'General Discussion' started by Senkrigar, Jul 30, 2019.

  1. Senkrigar

    Senkrigar

    Joined:
    Jul 17, 2019
    Posts:
    8
    I am making a game where I need to draw a Straight line using touch control. I wrote this script, but the rotation is weird, it seems reversed,could someone help me with that please ? :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlatformSpawn : MonoBehaviour
    6. {
    7.     public GameObject platform;
    8.     public float speed = 10.0f;
    9.     private GameObject createdPlatform;
    10.     private Vector2 firstPosition;
    11.     private Vector2 movePosition;
    12.  
    13.     void Update()
    14.     {
    15.         if(Input.touchCount>0)
    16.         {
    17.             Touch touch = Input.GetTouch(0);
    18.  
    19.             if (touch.phase == TouchPhase.Began)
    20.             {
    21.                 firstPosition = Camera.main.ScreenToWorldPoint(touch.position);
    22.                 createdPlatform = Instantiate(platform, firstPosition, Quaternion.identity);
    23.  
    24.                
    25.             }
    26.  
    27.             else if (touch.phase == TouchPhase.Moved)
    28.                 {
    29.                 movePosition = Camera.main.ScreenToWorldPoint(touch.position);
    30.  
    31.                 float hypotenuse = Mathf.Sqrt(Mathf.Pow((firstPosition.x - movePosition.x), 2) + Mathf.Pow((firstPosition.y - movePosition.y), 2));
    32.                 createdPlatform.transform.localScale = new Vector3(hypotenuse*2,0.3f, 1.0f);
    33.  
    34.  
    35.                 float deltaPosition_x = movePosition.x - firstPosition.x;
    36.                 float deltaPosition_y = movePosition.y - firstPosition.y;
    37.                 float platformRotation = Mathf.Atan2(deltaPosition_x, deltaPosition_y) * Mathf.Rad2Deg;
    38.                 createdPlatform.transform.rotation = Quaternion.AngleAxis(platformRotation,Vector3.forward);
    39.  
    40.                 createdPlatform.transform.parent = transform;
    41.                 }
    42.  
    43.         }
    44.     }
    45. }
    46.