如何在 3D unity 中更新重生点/检查点?

问题描述 投票:0回答:1

我目前正在尝试开发重生系统,但我似乎无法更新检查点。

代码有这些目标

-当玩家的健康状况为 0

时重生玩家

-当玩家踏上新的保存点(锚点)对象时更新检查点

当玩家踩到保存点(锚点)时,我似乎无法更新保存点(锚点),而当玩家死亡时,他们又回到了原来的重生点。

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

public class ResetPlayer : MonoBehaviour  {
         public float playerHeight;
         public float playerHealth;
 
     [Header("Border")]
     public LayerMask whatIsBorder;
     public bool touchBorder;
 
     [Header("Save Point")]
     public LayerMask whatIsSavePoint;
     public bool touchAnchor;
 
     Vector2 p_resetPosition;
 
     Rigidbody rb;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
 
         touchBorder = false;
         touchAnchor = false;
 
         p_resetPosition = gameObject.transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         // border check 
         touchBorder = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsBorder);
 
         // anchor check (should respawn player to the respected anchor)
         touchAnchor = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsSavePoint);
     }
 
     private void FixedUpdate()
     {
         Reset_Player();
         PlayerToSpawnpoint();
         ResetPlayerFromBorder();
     }
 
     // Condition to reset player - when player is dead
     private void Reset_Player()
     {
         if(playerHealth is 0)
         {
             playerHealth = playerHealth + 100;
 
             TransportPlayer();
         }
     }
 
     // if the player touches the border of the world they die
     private void ResetPlayerFromBorder()
     {
         if (touchBorder)
         {
             playerHealth = playerHealth - playerHealth;
             Debug.Log(gameObject.name + " touched Border");
         }
     }
 
     // To check if a player has steped on a save point (anchor) and update the players respawn point
     private void PlayerToSpawnpoint()
     {
         if (touchAnchor)
         {
             Debug.Log(gameObject.name + " touched Anchor");
         }
     }
 
     // transport player to respawn point
     private void TransportPlayer()
     {
         if (touchAnchor)
         {
             // I know this doesn't work, but I hope this gives you an idea of my problem
             rb.transform.position = touchBorder.transform.position;
         }
 
         else
         {
             //Resets the player the start position
             rb.transform.position = p_resetPosition;
 
             //Resets the player the start Rotation
             rb.transform.rotation = Quaternion.identity;
         }
         
     }  }
c# unity3d
1个回答
0
投票

首先,您没有保存新检查点的参考或位置。

Physics.Raycast
只返回一个布尔值是否击中了什么东西。最好为您的播放器添加一个对撞机。最简单的方法是使用
Physics.OverlapSphere

// Store a reference to the current checkpoint
Transform currentCheckpoint;

// Check for colliding checkpoints
Collider[] hitColliders = Physics.OverlapSphere(
    transform.position, 
    playerHeight * 0.5f + 0.2f, 
    whatIsSavePoint
);

// Overwrite checkpoint if we collided
if (hitColliders.Count) currentCheckpoint = hitColliders[0].transform;

currentCheckpoint
中定义你的
Start()
为默认值,并将碰撞代码放入update。请注意,如果玩家返回到前一个检查点,这也会覆盖当前检查点。

© www.soinside.com 2019 - 2024. All rights reserved.