2022년 6월 8일 수요일

unity move smooth

 


unity 

타겟지점까지 움직이고(smooth)

lookat을 처다보기



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VideoClick : MonoBehaviour
{
    // Start is called before the first frame update

   
    public Camera ActiveVideoCam;

   
    public GameObject TargetPos;
    public GameObject LookAtPoint;


    PlayerMouseLook PlayLook;
    public Vector3 InitPos;

    public GameObject[] UnActiveGameObjects;


    public float moveSpeed = 1;
    private Vector3 velocity = Vector3.zero;
    IEnumerator moveCam;


    private void Start()
    {
        PlayLook = FindObjectOfType<PlayerMouseLook>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("1"))
        {
            ZoomIn();
        }

        if (Input.GetKeyDown("2"))
        {
            Out();
        }
    }


    public void ZoomIn()
    {

        foreach (var x in UnActiveGameObjects)
        {
            x.SetActive(false);
        }

        PlayLook.enabled = false;
        if (moveCam != null)
        {
            StopCoroutine(moveCam);
            moveCam = null;
        }
        moveCam = SetZoomIn();
        StartCoroutine(moveCam);

    }

    public void Out()
    {

        foreach (var x in UnActiveGameObjects)
        {
            x.SetActive(true);
        }

        PlayLook.enabled = true;
        if (moveCam != null)
        {
            StopCoroutine(moveCam);
            moveCam = null;
        }
        ActiveVideoCam.transform.position = InitPos;
    }

    IEnumerator SetZoomIn()
    {

       
        InitPos = ActiveVideoCam.transform.position;

       
       
       
        //Vector3 originPosition = ActiveVideoCam.transform.position;
        Vector3 target = TargetPos.transform.position;

        while (true)
        {
            ActiveVideoCam.transform.position = Vector3.SmoothDamp(ActiveVideoCam.transform.position, target, ref velocity, moveSpeed);
            ActiveVideoCam.transform.LookAt(LookAtPoint.transform.position);

            if (Mathf.Abs(ActiveVideoCam.transform.position.x - target.x) < 0.1f)
                break;

            yield return new WaitForEndOfFrame();
        }
       
    }

}

댓글 없음:

댓글 쓰기

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...