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?
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);
}