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 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM