Unity3d--物体移动到鼠标点击位置

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /*
 5  *物体移动到鼠标点击位置
 6 */
 7 
 8 public class PlayerMove : MonoBehaviour {
 9 
10     public float speed = 5;
11     private PlayDir dir;
12     private CubeCS cubeCS;
13 
14     void Awake(){
15         dir = this.GetComponent<PlayDir> ();
16         cubeCS = this.GetComponent<CubeCS> ();
17     }
18 
19     // Use this for initialization
20     void Start () {
21     
22     }
23     
24     // Update is called once per frame
25     void FixedUpdate ()
26     {
27         //获取当前物体到目标物体的距离
28         float distance = Vector3.Distance (dir.targetPosition, transform.position);
29 
30         //判断是否超出距离
31         if (distance > 0.1f) {
32             cubeCS.rb.velocity = transform.forward * speed * distance;
33         } else{
34             cubeCS.rb.velocity = transform.forward * 0;
35         }
36     }
37 }
 1 using System.Collections;
 2 
 3 /*
 4  * 根据鼠标点击位置改变物体朝向
 5 */
 6 public class PlayDir : MonoBehaviour {
 7 
 8     public GameObject effect;
 9     private bool isMoving = false; //鼠标左键状态
10     public Vector3 targetPosition;
11 
12     void Awake(){
13         //初始化目标位置为当前物体的位置
14         targetPosition = this.transform.position;
15     }
16 
17     // Update is called once per frame
18     void Update () {
19         //判断鼠标左键是否按下
20         if (Input.GetMouseButtonDown(0)) {
21             //获取由主摄像机位置到鼠标点击位置的一条射线
22             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
23             RaycastHit hitInfo;
24             bool isCollider = Physics.Raycast (ray, out hitInfo);
25             //判断射线是否成功发射且是否触发目标物体
26             if (isCollider && hitInfo.collider.tag == Tags.ground) {
27                 //参数为目标物体的位置信息
28                 ShowClickEffect (hitInfo.point);
29                 isMoving = true;
30                 LookAtTarget (hitInfo.point);
31             }
32         }
33 
34         //判断鼠标左键是否抬起
35         if (Input.GetMouseButtonUp(0)) {
36             isMoving = false;
37         }
38 
39         if (isMoving) {
40             //获取由主摄像机位置到鼠标点击位置的一条射线
41             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
42             RaycastHit hitInfo;
43             bool isCollider = Physics.Raycast (ray, out hitInfo);
44             //判断射线是否成功发射且是否触发目标物体
45             if (isCollider && hitInfo.collider.tag == Tags.ground) {
46                 LookAtTarget (hitInfo.point);
47             }
48         }
49     }
50 
51     void ShowClickEffect(Vector3 hitPoint){
52         hitPoint = new Vector3 (hitPoint.x, hitPoint.y + 0.3f, hitPoint.z);
53         GameObject.Instantiate (effect, hitPoint, Quaternion.identity);
54     }
55         
56     void LookAtTarget(Vector3 hitPoint){
57         //获取触发目标物体的位置信息
58         targetPosition = hitPoint;
59         //将目标位置的y轴修改为当前物体的y轴
60         targetPosition = new Vector3 (targetPosition.x, transform.position.y, targetPosition.z);
61 
62         //当前物体朝向目标位置
63         this.transform.LookAt (targetPosition);
64     }
65 }
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /*
 5  *控制刚体的移动与旋转 
 6  */
 7 
 8 public class CubeCS : MonoBehaviour {
 9 
10     /*
11      * 在场景中创建一个Cube刚体
12      * 将刚体的位置和角度进行坐标约束,位置约束Y,方向约束x z
13     */
14     public Rigidbody rb;
15     public float speed = 5;
16     public float angularSpeed = 3;
17     // Use this for initialization
18 
19     void Awake(){
20         rb = this.GetComponent<Rigidbody> ();
21     }
22 
23     /*
24     void FixedUpdate(){
25         Move ();
26     }
27 
28     void Move(){
29         //获取垂直数值
30         float v = Input.GetAxis ("Vertical"); 
31         //设置刚体速度为 自身前方向*垂直数值*速度  控制刚体移动
32         rb.velocity = this.transform.forward * v * speed;
33 
34         //获取水平数值
35         float h = Input.GetAxis ("Horizontal");
36         //设置刚体角速度为 自身上方向*垂直数值*速度  控制刚体旋转
37         rb.angularVelocity = this.transform.up * h * angularSpeed;
38     }
39     */
40 }
posted @ 2016-06-16 15:32  yuge790615  阅读(8171)  评论(0编辑  收藏  举报