转自http://www.manew.com/thread-50195-1-1.html
cube = GameObject.FindGameObjectWithTag(Tags.Cube).transform;
cube1 = GameObject.FindGameObjectWithTag(Tags.Cube1).transform;
cube2 = GameObject.FindGameObjectWithTag(Tags.Cube2).transform;
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
GetComponent ().MovePosition (transform.position - new Vector3 (h, 0, v) * speed * Time.deltaTime);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//控制转向,朝向鼠标
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 200, groundLayerIndex)) {
Vector3 target = hitInfo.point;
target.y = transform.position.y;
transform.LookAt(target);
}
//鼠标左键点击围绕旋转
RaycastHit hit;
if (Input.GetMouseButton (0)) {
//Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
Debug.Log (hit.collider.name);
//print("hello !");
//cube旋转
if (hit.collider.name == "Cube") {
cube.Rotate (0, 1, 0);
cube1.RotateAround (cube.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
cube2.RotateAround (cube.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
}
if (hit.collider.name == "Cube1") {
cube1.Rotate (0, 1, 0);
cube.RotateAround (cube1.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
cube2.RotateAround (cube1.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
}
if (hit.collider.name == "Cube2") {
cube2.Rotate (0, 1, 0);
cube1.RotateAround (cube2.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
cube.RotateAround (cube2.position, new Vector3 (0, 5, 0), 60 * Time.deltaTime);
}
}
}