유니티 기초

3D 유니티 LookAt, 유니티 짱, Raycast, 클릭한 위치로 이동

다모아 2023. 8. 4. 16:24

transform.Lookat(target);

[목표를 향해 간다]

 

[버튼을 누르면 앞으로 가고 벽을 만나면 멈추는 기능]

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

public class PlayerController : MonoBehaviour
{
    private Animator anim;
    private bool isMove = false;
    private Rigidbody rBody;
    public float moveSpeed = 1f;
    public Transform target;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = GetComponent<Animator>();
        this.rBody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (isMove)
        {
            this.MoveStart();
            Debug.Log("실행중");
        }
        else
        {
            Debug.Log("State 01");
            this.anim.SetInteger("State", 0);
        }
            this.transform.LookAt(target);
    }

    public void MoveStart()
    {
        this.isMove = true;
        this.transform.Translate(Vector3.forward * this.moveSpeed * Time.deltaTime);
        this.anim.SetInteger("State", 1);
    }

    public void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Finish")
        {
            Debug.Log("finish");
            isMove = false;
        }
    }
}


RayCast

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

public class PlayerController : MonoBehaviour
{
    private Animator anim;
    private bool isMove = false;
    private Rigidbody rBody;
    public float moveSpeed = 1f;
    public Transform target;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = GetComponent<Animator>();
        this.rBody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f);
            RaycastHit hitData;
            //ray와 충돌되면
            if (Physics.Raycast(ray.origin, ray.direction * 1000f, out hitData))
            {
                //충돌된 정보가 hitData에 담긴다
                //hitData.point : 충돌된 지점의 좌표 (월드)
                this.transform.position = hitData.point;
            }
        }
    }
    public void MoveStart()
    {
        this.isMove = true;
        this.transform.Translate(Vector3.forward * this.moveSpeed * Time.deltaTime);
        this.anim.SetInteger("State", 1);
    }
}


클릭한 위치로 이동

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

public class PlayerController : MonoBehaviour
{
    private Animator anim;
    private bool isMove = false;
    public float moveSpeed = 1f;
    public Transform target;
    private Vector3 targetPosition;

    // Start is called before the first frame update
    void Start()
    {
        this.anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f);
            RaycastHit hitData;
            //ray와 충돌되면
            if (Physics.Raycast(ray.origin, ray.direction, out hitData))
            {
                //충돌된 정보가 hitData에 담긴다
                //hitData.point : 충돌된 지점의 좌표 (월드)
                //쳐다보고
                this.targetPosition = hitData.point;
                this.transform.LookAt(hitData.point);

                this.anim.SetInteger("State", 1);
                this.isMove = true;

            }
        }

        if (this.isMove)
        {
            this.transform.Translate(Vector3.forward * this.moveSpeed * Time.deltaTime);
            float distance = Vector3.Distance(this.transform.position, this.targetPosition);
            if (distance <= 0.1f)
            {
                this.isMove = false;
            }
        }
        else
            this.anim.SetInteger("State", 0);
    }
}

 

'유니티 기초' 카테고리의 다른 글

챕터 8 AppleCatch  (0) 2023.08.07
주말과제  (1) 2023.08.06
챕터7 밤송이 [수정필]  (0) 2023.08.04
pixel_sword  (0) 2023.08.03
챕터6 고양이 점프[점프막기, 카메라 아래 최소치 정하기 미완]  (0) 2023.08.02