如果我在构建模式下运行游戏,当我跳到平台上时,玩家会随之移动。但每当玩家死亡并变得不活动、重置位置并再次活动时,它就不起作用。奇怪的是,当我处于攻击模式时,它起作用,但当我处于常规移动模式时,它不起作用。
以下是我的平台的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformMovement : MonoBehaviour
{
public int PlatformDirection = 0;
public int MoveSpeed;
void Update()
{
if (PauseMenu.Paused == 0)
{
if (PlatformDirection == 0)
{
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}
if (PlatformDirection == 1)
{
transform.Translate(Vector3.back * MoveSpeed * Time.deltaTime);
}
}
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Wall")
{
if (PlatformDirection == 0)
{
PlatformDirection = 1;
}
else
{
PlatformDirection = 0;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformDetector : MonoBehaviour
{
public GameObject Player;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == Player)
{
Player.transform.parent = transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject == Player)
{
Player.transform.parent = null;
}
}
}
我的播放器的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float _moveSpeed;
public float _gravity;
public float _jumpSpeed;
public float _bounceSpeed;
public static int movemode;
public GameObject Camera;
public Animator animator;
public AudioSource audioSource;
public AudioClip JumpSound;
public AudioClip BoingSound;
public AudioClip HitSound;
public static int OnIce;
private CharacterController _controller;
private Rigidbody rb;
private float horizontalInput;
private float verticalInput;
public static float _directionY;
// Start is called before the first frame update
void Start()
{
_controller = GetComponent<CharacterController>();
audioSource = gameObject.GetComponent<AudioSource>();
movemode = 0;
PauseMenuController.ExitingLevel = 0;
}
// Update is called once per frame
void Update()
{
if (movemode == 0)
{
_moveSpeed = 5f;
_gravity = 9.81f;
_jumpSpeed = 4f;
if (OnIce == 0)
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
}
if (OnIce == 1)
{
horizontalInput = Input.GetAxis("HorizontalIce");
verticalInput = Input.GetAxis("VerticalIce");
}
Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
direction = Camera.transform.TransformDirection(direction);
if (_controller.isGrounded)
{
if(Input.GetButtonDown("Jump"))
{
audioSource.PlayOneShot(JumpSound, 1);
_directionY = _jumpSpeed;
}
}
if (!_controller.isGrounded)
{
_directionY -= _gravity * Time.deltaTime;
direction.y = _directionY;
}
else
{
_directionY -= 0 * Time.deltaTime;
direction.y = _directionY;
}
_controller.Move(direction * _moveSpeed * Time.deltaTime);
animator.SetBool("isWalking", verticalInput != 0 || horizontalInput != 0);
animator.SetBool("isGrounded", _controller.isGrounded == true);
animator.SetBool("Attacking", movemode == 3);
animator.SetBool("Swimming", movemode == 1);
animator.SetBool("LevelComplete", movemode == 4);
}
if (movemode == 1)
{
_moveSpeed = 3f;
_gravity = 4f;
_jumpSpeed = 1.5f;
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
direction = Camera.transform.TransformDirection(direction);
if(Input.GetButtonDown("Jump"))
{
_directionY = _jumpSpeed;
}
_directionY -= _gravity * Time.deltaTime;
direction.y = _directionY;
_controller.Move(direction * _moveSpeed * Time.deltaTime);
animator.SetBool("Swimming", movemode == 1);
animator.SetBool("Attacking", movemode == 3);
}
if (movemode == 3)
{
animator.SetBool("Attacking", movemode == 3);
}
if (movemode == 4)
{
animator.SetBool("LevelComplete", movemode == 4);
}
}
void OnTriggerEnter (Collider col)
{
if(col.gameObject.name == "Spike")
{
if (Pos_reset.invincibilityframe > 2)
{
HealthCounter.Health -= 1;
if (HealthCounter.Health > 0)
{
audioSource.PlayOneShot(HitSound, 1);
_directionY = _jumpSpeed;
}
Pos_reset.invincibilityframe = 0;
}
}
if(col.gameObject.name == "Trampoline")
{
audioSource.PlayOneShot(BoingSound, 1);
_directionY = _bounceSpeed;
}
}
}
你知道为什么会发生这种情况吗?
问题似乎是因为玩家在重生或重置时失去了其父平台(如果我没有错的话)。 重生后尝试将玩家重新设置为平台的父级。
private void Update()
{
// reparent the player to the parent if in bounds of the platform
if (Player.transform.parent != transform && IsPlayerWithinBounds())
{
Player.transform.parent = transform;
}
}
private bool IsPlayerWithinBounds()
{
Collider platformCollider = GetComponent<Collider>();
return platformCollider.bounds.Contains(Player.transform.position);
}
在
PlatformDetector