Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class UnitControll_CC : MonoBehaviour
- {
- public CharacterController UnitController;
- public GameObject MoveTargetSprite;
- public float speed;
- public float gravity;
- Vector3 grav;
- Ray ray;
- RaycastHit hit;
- public Vector3 MovePoint;
- public Camera MainCamera;
- public float UnitRotSpeed;
- public bool setTarget;
- public bool canMove;
- public LayerMask MoveTriggerMask;
- public bool endRotate;
- void Update()
- {
- MouseClick();
- Gravity();
- FreezeRotation();
- if (setTarget) SmoothLookAt(); //Надо выключать поворот
- }
- void FixedUpdate()
- {
- Movement();
- if (endRotate)
- {
- setTarget = false;
- canMove = true;
- }
- }
- void MouseClick()
- {
- if (Input.GetMouseButtonDown(1))
- {
- ray = MainCamera.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit))
- {
- if (hit.transform.tag == "ground")
- {
- Debug.Log(hit.point);
- MovePoint = hit.point;
- MoveTargetSprite.transform.position = hit.point;
- setTarget = true;
- }
- }
- }
- }
- void Gravity()
- {
- grav.y += gravity * Time.deltaTime;
- UnitController.Move(grav * Time.deltaTime);
- }
- void Movement()
- {
- if (!setTarget & canMove)
- {
- Vector3 direction = transform.forward * speed;
- UnitController.Move(direction * Time.deltaTime);
- }
- bool onMovePoint;
- Ray ray;
- RaycastHit hit;
- ray = new Ray(transform.position, -transform.up);
- if (Physics.Raycast(ray, out hit))
- {
- if (hit.transform.tag == "MovePoint")
- {
- Debug.Log("Ur on MovePoint");
- canMove = false;
- setTarget = false;
- }
- }
- }
- void SmoothLookAt()
- {
- Vector3 direction = MoveTargetSprite.transform.position - transform.position;
- Quaternion rotation = Quaternion.LookRotation(direction);
- transform.rotation = Quaternion.Lerp(transform.rotation, rotation, UnitRotSpeed * Time.deltaTime);
- endRotate = Physics.Raycast(transform.position, transform.forward, 10000000, MoveTriggerMask);
- }
- void FreezeRotation()
- {
- this.transform.eulerAngles = new Vector3(0.0f, transform.eulerAngles.y, 0.0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement