- Home /
The question is answered, right answer was accepted
Rotate sphere based on texture coordinates
I have sphere with a Texture2D. I have one camera which looks at the sphere. I have a script which returns the coordinates from where i click with the mouse.
Is it possible to rotate the sphere object when the mouse clicks, so that the coordinates is what the camera looks at?
Answer by robertbu · Oct 23, 2013 at 01:48 AM
Here is a bit of example code. This is not based on the texture coordinate, but based on the Raycast hit position. To test:
Start with a new scene
Add a sphere with a texture
Add the script to the sphere
pragma strict
public var speed = 45.0; private var qTo : Quaternion; private var trans : Transform;
function Start() { trans = transform; qTo = trans.rotation; }
function Update() { if (Input.GetMouseButtonDown(0)) { var hit : RaycastHit; var ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, hit) && hit.transform == trans) { qTo = Quaternion.FromToRotation(hit.point - trans.position, Camera.main.transform.position - trans.position); qTo = qTo * transform.rotation; } }
transform.rotation = Quaternion.RotateTowards(trans.rotation, qTo, Time.deltaTime * speed); }