c# - 我的播放器不会坚持使用平台,我似乎无法找到解决方案
问题描述
所以我试图让玩家成为移动平台的孩子,通过寻找航路点并前往它们移动平台。但是当我让玩家与平台发生碰撞时,碰撞输入没有检测到玩家,因此不会让玩家停留在平台上,而是滑行。在此处输入图像描述
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);
}
}
解决方案
这不会使玩家成为平台的孩子:
controller.transform.parent = other.gameObject.transform;
因为这里controller
是 CharacterController 实例,并且other
指的是玩家对撞机。所以他们最后都指的是玩家变换。
用类似的东西替换它
other.transform.parent = transform;
或者
controller.transform.parent = transform;
(这里transform
指的是平台变换。)
如果OnTriggerEnter()
没有检测到,请检查您的对撞机是否已isTrigger
启用。或将方法更改为OnCollisionEnter()
.
如果您正在制作 2D 游戏,请将这些方法切换到 2D 版本(=>OnCollisionEnter2D
或OnTriggerEnter2D
)。
推荐阅读
- primefaces - Primefaces 选项列表具有旋转按钮
- excel - 将 UTC 时间/日期转换为本地时间/日期保持毫秒
- r - 根据每列的有效值列表计算每行的无效值 (R)
- javascript - JS/CSS:第二类的背景颜色
- python - 如何阻止我的前任被当作病毒对待?
- python - 我将如何在 python 中提取水平数据和垂直格式化数据?
- c++ - 如何继承从一个基类继承的不同类的特定变量?
- http - 为什么 HTTP 请求会返回给出 1984 年到期日期的行?
- python - Kivy:为什么 Button 在案例 2 中失去了它的属性?
- java - Spring Boot API 响应返回重复的嵌套 JSON