Skip to content

Instantly share code, notes, and snippets.

@smallrice45
Created March 19, 2014 14:14
Show Gist options
  • Save smallrice45/9642480 to your computer and use it in GitHub Desktop.
Save smallrice45/9642480 to your computer and use it in GitHub Desktop.
Transform target;
float rotateSpeed = 10;
// Use this for initialization
void Start () {
// 將 target指定名稱為Imege的物件,並取得物件的Transform
target = GameObject.Find("Image").GetComponent<Transform>();
}
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Physics.Raycast(ray, out hit);
Debug.Log("This hit at " + hit.point );
Vector3 pos;
pos = hit.point;
pos.z = 0; // 忽略z軸
float angle = Vector3.Angle( pos - target.position, Vector3.down);
if(pos.x > target.position.x) {
angle*=-1;
}
target.rotation = Quaternion.Slerp(target.rotation,Quaternion.Euler(0,0,180-angle),rotateSpeed*Time.deltaTime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment