Hi Guys,
I am developing this interactive where I have two meshes. Overall I am quite satisfied but it occurs that sometimes (can’t say differently) when I pick the object with the mouse and rotate it, it starts flickering… and I can’t really figure it out why. can you please watch the video and may be you can have a better understanding on what’s happening.
Many Thansk for the Help!
https://docs.google.com/open?id=0B6tKxXosCXQgLVNtU3lNLTlSVTQ
[RequireComponent(typeof(Collider))]
public class RotateObject : MonoBehaviour {
public Camera c_mainCamera;
public float speed = 0.5f;
private Quaternion _initQuat;
private Quaternion _offsetQuat;
private Quaternion _currentQuat;
private Quaternion _rotation;
private RaycastHit hit;
// Use this for initialization
void Start () {
speed = 2.0f;
}
Quaternion ShootRay() {
Ray ray = c_mainCamera.ScreenPointToRay(Input.mousePosition) ;
Quaternion _tmp_Quat = new Quaternion();
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 2000.0f) ){
_tmp_Quat.x = hit.point.x;
_tmp_Quat.y = hit.point.y;
_tmp_Quat.z = hit.point.z;
} else
return(Quaternion.identity);
return _tmp_Quat;
}
//Called only when the Left Button Mouse is pressed
void OnMouseDrag() {
Quaternion _tQuat = ShootRay();
if (_tQuat != Quaternion.identity) {
_currentQuat = _tQuat * _offsetQuat;
Quaternion _endQuat = Quaternion.Slerp(_initQuat, _currentQuat, Time.time * speed);
transform.rotation = _endQuat;
_initQuat = _currentQuat;
}
}
// Update is called once per frame
void Update () {
}
//Clicked on the object and grab its rotation quaternion.
//The mouse is on the object's collider so it's time
//to save its initial position.
//Called only when the Left Button Mouse is pressed
void OnMouseDown() {
_initQuat = transform.rotation;
_offsetQuat = ShootRay() * _initQuat;
}
void OnMouseExit() {
_currentQuat = transform.rotation;
_initQuat = _currentQuat;
}
//Called only when the Left Button Mouse is pressed
void OnMouseUp(){
_currentQuat = transform.rotation;
}
}