thieumao

Unity - Di chuyen theo vi tri click chuot

Aug 31st, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class theLastOneBehaviour : MonoBehaviour {
  5.  
  6.     private Vector3 clickPos;  // vi tri click left mouse
  7.  
  8.     // Use this for initialization
  9.     void Start () {
  10.         clickPos = transform.position;
  11.     }
  12.    
  13.     // Update is called once per frame
  14.     void Update () {
  15.         MovetheLastOne ();
  16.     }
  17.  
  18.     void MovetheLastOne(){
  19.         // bat su kien click chuot trai
  20.         if (Input.GetMouseButton (0)) {
  21.             // Camera.main.ScreenToWorldPoint --> Chuyen Toa do man hinh thanh toa do 3 chieu
  22.             // Input.mousePosition --> Lay toa do man hinh
  23.             clickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  24.         }
  25.  
  26.         // di chuyen tu vi tri hien tai toi clickPos
  27.         transform.position = Vector3.MoveTowards (transform.position, clickPos, Time.deltaTime * 3.0f);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment