- Home /
rotate 2d circle on z axis ?
i wrote a code that rotates circle on z axis with mouse touch , but the result is like the circle looks at mouse cursor which is not what im looking for , so im going to show you the code samples , code 1 and code 2 : code1 : which what is the one i use for 2D circle or wheel , and its the one that has result of which the wheel rotates by looking at mouse
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class pss_wheel : MonoBehaviour
 {
     [Header("Degree of rotation offset. *360")]
     public float offset = 180.0f;
     Vector3 mPrevPos = Vector3.zero;
     Vector3 mPosDelta = Vector3.zero;
     bool mIsTouching = false;
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             if (!mIsTouching)
             {
                 mPrevPos = Input.mousePosition;
             }
             mIsTouching = true;    
             mPosDelta = Input.mousePosition - mPrevPos;
             transform.Rotate(transform.up, -Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.Self);   
             Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
             difference.Normalize(); 
             float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;   
             transform.rotation = Quaternion.Euler(0f, 0f, rotationZ - (90 + offset));
         }
         else
         {
             mIsTouching = false;
         }
         mPrevPos = Input.mousePosition;
     }
 }
Code 2 : this one works perfectly fine but for a 3d wheel on a 2d view
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Wheel_physics : MonoBehaviour
 {
     Vector3 mPrevPos = Vector3.zero;
     Vector3 mPosDelta = Vector3.zero;
     bool mIsTouching = false; 
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             if (!mIsTouching)
             {
                 mPrevPos = Input.mousePosition;
             }
             mIsTouching = true;    
             mPosDelta = Input.mousePosition - mPrevPos;
             transform.Rotate(transform.up, -Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
         }
         else
         {
             mIsTouching = false;
         }
 
         mPrevPos = Input.mousePosition;
     }      
     void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.name == "yellow_s")
         {              
             Destroy(collision.gameObject);
         }
     }
 };
 
what im looking for is i want to achieve the results of code 2 but using the 2D wheel , or a way to improve code to give me the same results as code 2 , sorry for the long request , but i'll be appreciated !
Answer by eni_cay · Jul 28, 2019 at 04:31 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class final_wheel : MonoBehaviour { Vector3 mPrevPos = Vector3.zero; Vector3 mPosDelta = Vector3.zero; bool mIsTouching = false;
 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         mPrevPos = Input.mousePosition;
     }
     else if (Input.GetMouseButton(0))
     {
         mPosDelta = Input.mousePosition - mPrevPos;
         Vector3 proj = Camera.main.WorldToScreenPoint(transform.position);
         if (proj.y > Input.mousePosition.y)
         {
             transform.Rotate(transform.forward, Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
         }
         else
         {
             transform.Rotate(transform.forward, -Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
         }
         if (proj.x > Input.mousePosition.x)
         {
             transform.Rotate(Camera.main.transform.forward, -Vector3.Dot(mPosDelta, Camera.main.transform.up), Space.World);
         }
         else
         {
             transform.Rotate(Camera.main.transform.forward, Vector3.Dot(mPosDelta, Camera.main.transform.up), Space.World);
         }
         mPrevPos = Input.mousePosition;
     }
 }
}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                