留在移动平台上

mut*_*y91 5 c# unity-game-engine

我正在Unity中编写2D平台游戏,我正试图让玩家留在移动平台上.我现在已经做了一两天的搜索和修补,我没有运气.

基本上,我被告知要在角色接触时尽量让角色随平台移动.首先,如果我使用与OnTriggerEnter()相关的任何内容,则播放器会直接通过平台.如果我做OnCollisionEnter()(在播放器上有一个CharacterController,在平台上有一个BoxCollider),根本没有任何事情发生.这是我发现最常见的两件事.另一个是用平台养育玩家,但这显然会导致"问题"(经常陈述,从未解释过).

那么,我怎样才能让玩家留在移动平台上?以下是移动平台的代码:

public class MovingPlatform : MonoBehaviour
{
private float useSpeed;
public float directionSpeed = 9.0f;
float origY;
public float distance = 10.0f;

// Use this for initialization
void Start () 
{
    origY = transform.position.y;
    useSpeed = -directionSpeed;
}

// Update is called once per frame
void Update ()
{
    if(origY - transform.position.y > distance)
    {
        useSpeed = directionSpeed; //flip direction
    }
    else if(origY - transform.position.y < -distance)
    {
        useSpeed = -directionSpeed; //flip direction
    }
    transform.Translate(0,useSpeed*Time.deltaTime,0);
}
Run Code Online (Sandbox Code Playgroud)

这里是玩家移动的代码(在更新中):

CharacterController controller = GetComponent<CharacterController>();
    float rotation = Input.GetAxis("Horizontal");
    if(controller.isGrounded)
    {
        moveDirection.Set(rotation, 0, 0); //moveDirection = new Vector3(rotation, 0, 0);
        moveDirection = transform.TransformDirection(moveDirection);

        //running code
        if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
        { running = true; }
        else
        { running = false; }

        moveDirection *= running ? runningSpeed : walkingSpeed; //set speed

        //jump code
        if(Input.GetButtonDown("Jump"))
        {
            //moveDirection.y = jumpHeight;
            jump ();
        }
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
Run Code Online (Sandbox Code Playgroud)

编辑:我认为这可能与我如何定义播放器和平台有关,但我尝试了不同的组合.如果平台是一个触发器(在其对撞机上),则玩家会持续地通过它.如果没有,我不能使用OnTrigger功能.我有一个与玩家和平台相连的刚体,但它似乎没有任何影响.当玩家在一些设置中进入平台时,他会紧张不安并且通常最终会摔倒.

Kay*_*Kay 2

您需要的似乎是平台上的第二个对撞机。主碰撞器必须isTrigger = false确保您的角色控制器正常工作。第二个运行的是isTrigger = true,它的唯一功能是检测玩家在调用时是否连接到平台OnTriggerEnter并离开平台OnTriggerExit

由于您不能拥有 2 个相同类型的碰撞器(我猜您需要盒式碰撞器),因此在您的平台游戏对象下构建一个空子对象并将盒式碰撞器分配给它。我通常会使用一种名为 ActionMaterial 的特殊物理材质来保持我的东西干净。如果您使用图层并调整了碰撞矩阵,请确保您的碰撞由 physX 处理。