Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class UnitControll_CC : MonoBehaviour
  6. {
  7.     public CharacterController UnitController;
  8.     public GameObject MoveTargetSprite;
  9.  
  10.     public float speed;
  11.     public float gravity;
  12.     Vector3 grav;
  13.     Ray ray;
  14.     RaycastHit hit;
  15.     public Vector3 MovePoint;
  16.     public Camera MainCamera;
  17.     public float UnitRotSpeed;
  18.     public bool setTarget;
  19.     public bool canMove;
  20.     public LayerMask MoveTriggerMask;
  21.     public bool endRotate;
  22.     void Update()
  23.     {
  24.         MouseClick();
  25.         Gravity();
  26.        
  27.         FreezeRotation();
  28.         if (setTarget) SmoothLookAt(); //Надо выключать поворот  
  29.     }
  30.     void FixedUpdate()
  31.     {
  32.         Movement();
  33.         if (endRotate)
  34.         {
  35.             setTarget = false;
  36.             canMove = true;
  37.         }
  38.     }
  39.     void MouseClick()
  40.     {    
  41.         if (Input.GetMouseButtonDown(1))
  42.         {
  43.             ray = MainCamera.ScreenPointToRay(Input.mousePosition);
  44.             if (Physics.Raycast(ray, out hit))
  45.             {
  46.                 if (hit.transform.tag == "ground")
  47.                 {
  48.                     Debug.Log(hit.point);
  49.                     MovePoint = hit.point;
  50.                     MoveTargetSprite.transform.position = hit.point;
  51.                     setTarget = true;                  
  52.                 }
  53.             }    
  54.         }
  55.     }
  56.     void Gravity()
  57.     {
  58.         grav.y += gravity * Time.deltaTime;
  59.         UnitController.Move(grav * Time.deltaTime);
  60.     }
  61.     void Movement()
  62.     {
  63.         if (!setTarget & canMove)
  64.         {
  65.             Vector3 direction = transform.forward * speed;
  66.             UnitController.Move(direction * Time.deltaTime);
  67.         }
  68.         bool onMovePoint;
  69.         Ray ray;
  70.         RaycastHit hit;
  71.         ray = new Ray(transform.position, -transform.up);
  72.         if (Physics.Raycast(ray, out hit))
  73.         {
  74.             if (hit.transform.tag == "MovePoint")
  75.             {
  76.                 Debug.Log("Ur on MovePoint");
  77.                 canMove = false;
  78.                 setTarget = false;
  79.             }
  80.  
  81.         }
  82.  
  83.        
  84.     }
  85.     void SmoothLookAt()
  86.     {
  87.         Vector3 direction = MoveTargetSprite.transform.position - transform.position;
  88.         Quaternion rotation = Quaternion.LookRotation(direction);
  89.         transform.rotation = Quaternion.Lerp(transform.rotation, rotation, UnitRotSpeed * Time.deltaTime);
  90.  
  91.         endRotate = Physics.Raycast(transform.position, transform.forward, 10000000, MoveTriggerMask);
  92.  
  93.        
  94.     }
  95.     void FreezeRotation()
  96.     {
  97.         this.transform.eulerAngles = new Vector3(0.0f, transform.eulerAngles.y, 0.0f);
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement