Rotate Disc Problem - Help needed

Hi,

I have a disc that will rotate when the user drags the mouse around it, however the disc will not fully rotate with my script, it works if you drag from the center of the disc to the left/right edge but not if you try to drag around the radius of the disc. I need the disc to fully rotate following the radius of the disc/mouse drag.

The script I am using is below:

using UnityEngine; 
using System.Collections; 


public class RotateDisc : MonoBehaviour { 

   // Use this for initialization 
   void Start () 
   { 
    
   } 
    
   void Update () 
   { 
   	  bool turtle = false;
      RaycastHit hit; 

      if (Input.GetMouseButton(0)) 
      { 
         if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) 
         { 
          turtle = false;
            float x = -Input.GetAxis("Mouse X"); 
            float y = -Input.GetAxis("Mouse Y"); 
            float speed = 10; 
            transform.rotation *= Quaternion.AngleAxis(x*speed, Vector3.forward); 
            transform.rotation *= Quaternion.AngleAxis(y*speed, Vector3.forward); 
            
         } 
      } 
   } 
    
}

Any suggestions or help would really be appreciated. 8) 8)

can’t you just use LookAt on the disc?

  • so it looks at the raycasted position of the mouse.

I could try that, would this make the camera look at the object to make it appear to be rotating?

I may have 2 objects rotating.

So the LookAt script would be edited to include a raycast ?
The rotation in the example script almost does the object rotation correctly, but stops half way.

no, the LookAt will make the object itself look at something, not the camera. So if you do a raycast and make an invisible gameobject move to where the hit position is, then you can make your disc look at the invisible gameobject…

Sounds convoluted, but will work very well…

You don’t need an invisible game object; you can just use a world position directly in LookAt.

–Eric

Thanks for the tips.

I am playing with the script.
If I want to add the LookAt part, would it be a seperate script, or could I just change the line in the code above from:

transform.rotation = Quaternion.AngleAxis(yspeed, Vector3.forward);

to:

transform.LookAt = Quaternion.AngleAxis(yspeed, Vector3.forward);

P.S. I have been trying to figure out the offset / tiling option in Unity so that I can choose what part of my image/texture would be visible on my UV mapped disc (GameObject).

If I understand correctly, it should enable me to show the right hand side (or any other part) of the image instead of the whole image if desired?

Just thought about it some more relised that I should probably be using the raycast hit as the target for the lookat.

using UnityEngine; 
using System.Collections; 

public class RotateCircleLook : MonoBehaviour { 

    
   void Update () 
   { 
   

      if (Input.GetMouseButton(0)) 
      { 
      	      	
            RaycastHit hit; 
            transform.LookAt(hit.point);  
            float x = -Input.GetAxis("Mouse X"); 
            float y = -Input.GetAxis("Mouse Y"); 
            float speed = 10; 
      }  
   } 
    
}

However I keep getting the following error, and am not too hot on C.

Assets/RotateCircleLook.cs(15,30): error CS0165: Use of unassigned local variable `hit’

You’re declaring a variable (hit) of type RaycastHit, but you’re never actually doing a raycast.

Thanks, I have removed the raycast hit now, Am new to c.

I suppose what I am after is a 2d trackball effect for rotating my disc 360 degrees based on mouse drag around the radius.

Check out this post; at the bottom u find a script,
I’m pretty sure this is kind of what you’re looking for.
You just need tot adjust it to your preferences.

http://forum.unity3d.com/viewtopic.php?p=272124#272124

@Halo212: you’ve posted this question in several different places now. In future, please start only one thread for a given problem. We don’t mind if you bump that thread in the case that you don’t get a response. However, it is a nuisance to everyone if they are responding to you unaware of what other people have said in the other threads.

Ok, no problem, just wanted to get a few different opinions.
Apollogies.