Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class theLastOneBehaviour : MonoBehaviour {
- private Vector3 clickPos; // vi tri click left mouse
- public float speed = 5.0f;
- // Use this for initialization
- void Start () {
- clickPos = transform.position;
- }
- // Update is called once per frame
- void Update () {
- MovetheLastOne ();
- Rotation();
- }
- void MovetheLastOne(){
- // bat su kien click chuot trai
- if (Input.GetMouseButton (0)) {
- // Camera.main.ScreenToWorldPoint --> Chuyen Toa do man hinh thanh toa do 3 chieu
- // Input.mousePosition --> Lay toa do man hinh
- clickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- // giu toa do z
- clickPos.z = transform.position.z;
- }
- // di chuyen tu vi tri hien tai toi clickPos
- transform.position = Vector3.MoveTowards (transform.position, clickPos, Time.deltaTime * speed);
- }
- void Rotation(){
- // vi tri click chuot tren man hinh
- Vector3 screenPos = Input.mousePosition;
- // doi sang tao do chieu 3 chieu
- Vector3 worldPos = Camera.main.ScreenToWorldPoint (screenPos);
- float dx = transform.position.x - worldPos.x; // do lech truc x
- float dy = transform.position.y - worldPos.y; // do lech truc y
- float radAngle = Mathf.Atan2 (dy, dx);
- float degAngle = radAngle * Mathf.Rad2Deg; // doi radian sang degree (do)
- // xoay - Tinh Quaternion
- Quaternion qua = Quaternion.Euler (new Vector3(0,0,degAngle+90));
- transform.rotation = qua;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment