Files
MI-38/Heli_with_panels/Assets/Scripts/UI/ClickGruzCube2.cs
2022-07-06 12:36:49 +03:00

105 lines
2.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickGruzCube2 : MonoBehaviour
{
[SerializeField]
private float z;
private Vector3 startpos, endpos;// = new Vector3(-5.4f, 2.5f, 1.5f);
private Coroutine coroutine, coroutine2;
float cameraMoveCurTime, cameraMoveMaxTime = 1f;
float scrollSpeed = 0.5f;
Renderer rend;
float eulerAngZ;
[HideInInspector] public Manager manager;
bool flag_rev;
public Texture2D SampleTexture;
public Texture2D NormalTexture;
private void Awake()
{
manager = GameObject.Find("Manager").GetComponent<Manager>();
endpos =new Vector3(-5.4f, 2.5f,z);
rend = GetComponent<Renderer>();
rend.enabled = false;
eulerAngZ = transform.localEulerAngles.z;
RotationArrow();
flag_rev = false;
}
void OnMouseEnter()
{
RotationArrow();
rend.enabled = true;
coroutine2 = StartCoroutine(GoToArrow());
}
void OnMouseExit()
{
rend.enabled = false;
if (coroutine2 != null) StopCoroutine(coroutine2);
}
private void OnMouseDown()
{
if (manager.viewmode == 1) return;
cameraMoveCurTime = 0;
startpos = Camera.main.transform.position;
coroutine = StartCoroutine(GoToCube());
}
IEnumerator GoToCube()
{
float d;
while (cameraMoveCurTime < cameraMoveMaxTime)
{
cameraMoveCurTime += Time.deltaTime;
d = cameraMoveCurTime / cameraMoveMaxTime;
Camera.main.transform.position = Vector3.Lerp(startpos, endpos, d);
yield return null;
}
}
void RotationArrow()
{
if (Camera.main.transform.localEulerAngles.y > -90 && Camera.main.transform.localEulerAngles.y < 90|| Camera.main.transform.localEulerAngles.y > 270)
{
flag_rev = true;
}
else
{
flag_rev = false;
}
}
IEnumerator GoToArrow()
{
while (rend.enabled)
{
float offset = -Time.time * scrollSpeed;
if (flag_rev)
{
rend.material.SetTexture("_MainTex", SampleTexture);
rend.material.SetTextureOffset("_MainTex", new Vector2(-offset, 0));
}
else
{
rend.material.SetTexture("_MainTex", NormalTexture);
rend.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
yield return null;
}
}
public void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
if (coroutine != null) StopCoroutine(coroutine);
}
}
}