본문 바로가기
KDT/유니티 기초

23/08/04 유니티짱 직선으로 움직이기

by 잰쟁 2023. 8. 4.
728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UnitychanController : MonoBehaviour
{ 
    public Animator anim;
    float moveSpeed = 1;
    private bool isMove;

    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
        //Debug.Log(this.transform.forward);  //지역좌표의 앞방향
        //Debug.Log(Vector3.forward);  //월드좌표에서 앞방향 0,0,1
    }

    // Update is called once per frame
    void Update()
    {
        if (isMove)
        {
            if (this.transform.position.z >= 2.0)
            {
                //stop
                this.anim.SetInteger("State", 0);
                this.isMove = false;
            }
            else
            {
                //이동      
                //this.transform.Translate(방향*속도*시간);
                this.transform.Translate(Vector3.forward * this.moveSpeed * Time.deltaTime);
                Debug.Log(this.transform.position);
                this.anim.SetInteger("State", 1);
            }
        }     
    }

    public void MoveStart()
    {      
        Debug.Log("MoveStart");
        this.isMove = true;
    }
}