首页 > 解决方案 > 我的播放器不会坚持使用平台,我似乎无法找到解决方案

问题描述

所以我试图让玩家成为移动平台的孩子,通过寻找航路点并前往它们移动平台。但是当我让玩家与平台发生碰撞时,碰撞输入没有检测到玩家,因此不会让玩家停留在平台上,而是滑行。在此处输入图像描述

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

public class movePlatform : MonoBehaviour
{
    public GameObject[] waypoints;
    float rotSpeed;
    int current = 0;
    public float speed;
    float WPradius = 1;
    private GameObject target = null;
    private Vector3 offset;
    public GameObject Player;
CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}


void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        //This will make the player a child of the Obstacle
        controller.transform.parent = other.gameObject.transform;
    }
}

void OnTriggerExit(Collider other)
{
    controller.transform.parent = null;
}

// Update is called once per frame
void Update()
{

    if (Vector3.Distance(waypoints[current].transform.position, transform.position) < WPradius)
    {

    current++;
        if (current >= waypoints.Length)
        {
            current = 0;
        }
    }

    transform.position = Vector3.MoveTowards(transform.position, waypoints[current].transform.position, Time.deltaTime * speed);

}

}

标签: c#unity3d

解决方案


这不会使玩家成为平台的孩子:

controller.transform.parent = other.gameObject.transform;

因为这里controller是 CharacterController 实例,并且other指的是玩家对撞机。所以他们最后都指的是玩家变换。

用类似的东西替换它

other.transform.parent = transform;

或者

controller.transform.parent = transform;

(这里transform指的是平台变换。)


如果OnTriggerEnter()没有检测到,请检查您的对撞机是否已isTrigger启用。或将方法更改为OnCollisionEnter().

如果您正在制作 2D 游戏,请将这些方法切换到 2D 版本(=>OnCollisionEnter2DOnTriggerEnter2D


推荐阅读