简体   繁体   中英

unity VR- how to stop player move after it reaches the last waypoint?

While Unity was developing VR content, I want to create a script that stops at the last Waypoint while moving to the Waypoint route, But I have very rudimentary skills in C# Coding, (I tried my best, but I failed) so I would like to ask you for help...

This is the script that goes back to the first Waypoint, Could you help me Please?? I'd really appreciate your help.

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

public class MoveCtrl : MonoBehaviour
{

    //이동방식 열거형 변수 선언
    public enum MoveType
    {
        WAY_POINT,
        LOOK_AT,
        DAYDREAM

    }

    //이동방식
    public MoveType moveType = MoveType.WAY_POINT;
    //이동속도
    public float speed = 1.0f;
    //회전속도
    public float damping = 3.0f;
    public int zero = 0;

    //모든 웨이포인트를 저장할 배열
    public Transform[] points;

    //트랜스톰 컴포넌트를 저장할 변수
    private Transform tr;
    //다음에 이동해야할 위치 인덱스 변수
    private int nextIdx = 1;

    void Start()
    {

        //트랜스폼 컴포넌트 추출후 변수 저장
        tr = GetComponent<Transform>();

        //포인트 게임오브젝트를 검색해 변수에 저장
        GameObject WayPointGroup = GameObject.Find("WayPointGroup");

        if(WayPointGroup != null)
        {

            //웨이포인트 하위에 모든 게임 오브젝트 Transfotm 컴포넌트 추출
            points = WayPointGroup.GetComponentsInChildren<Transform>();
        }
        
    }

    // Update is called once per frame
    void Update()
    {
        switch (moveType)
        {
            case MoveType.WAY_POINT:
                MoveWayPoint();
                break;

            case MoveType.LOOK_AT:
                break;

            case MoveType.DAYDREAM:
                break;

        }
        
    }

    //웨이포인트 경로로 이동하는 로직

    void MoveWayPoint()
    {
        //현재 위치에서 다음 웨이포인트로 향하는 벡터를 계산
        Vector3 direction = points[nextIdx].position - tr.position;
        //산출된 벡터의 회전 각도를 쿼터니언 타입으로 산출
        Quaternion rot = Quaternion.LookRotation(direction);
        //현재 각도에서 회전해야 할 각도까지 부드럽게 회전처리
        tr.rotation = Quaternion.Slerp(tr.rotation, rot, Time.deltaTime * damping);

        //전진 방향으로 이동처리
        tr.Translate(Vector3.forward * Time.deltaTime * speed);
    }

    void OnTriggerEnter(Collider coll)
    {
        //웨이포인트 - 포인트 게임 오브텍트 충돌여부 판단
        if (coll.CompareTag("WAY_POINT"))
        {
            //맨 마지막 웨이포인트에 도달했을 때 처음 인덱스로 변경
            nextIdx = (++nextIdx >= points.Length) ? 1 : nextIdx;
           
        }

        
      

    }

 
}

Ok i am not sure if this script is a predefined one.

But the strategy behind your problem is quite simple.

  • Create a variable containing always the last waypoint
  • Write logic to overwrite this when reaching the next waypoints
  • also set it to null after reaching the last waypoint.
  • And while a waypoint is set (waypoint.= null) then move towards the waypoint.

I described it like so because there are plenty ways to accomplish this waypoint system - be creative.

Also..

To Simplify your code i would use Vector3.moveTowards() and transform.lookAt() methods. With those you can easily reach your goal.

Vector3.MoveTowards() moves a object from one point to another point by given time*speed

transform.LookAt(DestTransform) makes this current Transforms positive z axis look at the direction of the transfrom. If this does not fit your needs try to modify the transform afterwards instead of complex rotation actions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM