玩家不会坚持移动平台

时间:2014-12-30 19:38:35

标签: unity3d unity3d-2dtools

我为我的游戏创建了一些移动平台。有一个名为PlatformPath1的游戏对象,其中有子节点定义平台周期的开始和结束,然后显然有跟随路径的实际平台。然而,平台移动完美,就像预期的那样,玩家不会随平台移动。我如何让玩家随平台移动?

以下是PlatformPath1的代码

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

public class FollowPath : MonoBehaviour
{
    public enum FollowType
    {
        MoveTowards,
        Lerp
    }

    public FollowType Type = FollowType.MoveTowards;
    public PathDefinition Path;
    public float Speed = 1;
    public float MaxDistanceToGoal = .1f;

    private IEnumerator<Transform> _currentPoint;

    public void Start()
    {
        if (Path == null)
        {
            Debug.LogError("Path cannot be null", gameObject);
            return;
        }

        _currentPoint = Path.GetPathEnumerator();
        _currentPoint.MoveNext();

        if (_currentPoint.Current == null)
            return;

        transform.position = _currentPoint.Current.position;
    }

    public void Update()
    {
        if (_currentPoint == null || _currentPoint.Current == null)
            return;

        if (Type == FollowType.MoveTowards)
            transform.position = Vector3.MoveTowards (transform.position, _currentPoint.Current.position, Time.deltaTime * Speed);
        else if (Type == FollowType.Lerp)
            transform.position = Vector3.Lerp(transform.position, _currentPoint.Current.position, Time.deltaTime * Speed);

        var distanceSquared = (transform.position - _currentPoint.Current.position).sqrMagnitude;
        if (distanceSquared < MaxDistanceToGoal * MaxDistanceToGoal)
            _currentPoint.MoveNext();
    }
}

这是平台的代码

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

public class PathDefinition : MonoBehaviour
{
    public Transform[] Points;

    public IEnumerator<Transform> GetPathEnumerator()
    {
        if(Points == null || Points.Length < 1)
            yield break;

        var direction = 1;
        var index = 0;
        while (true)
        {
            yield return Points[index];

            if (Points.Length == 1)
                continue;

            if (index <= 0)
                direction = 1;
            else if (index >= Points.Length - 1)
                direction = -1;

            index = index + direction;
        }
    }

    public void OnDrawGizmos()
    {
        if (Points == null || Points.Length < 2)
            return;

        for (var i = 1; i < Points.Length; i++) {
            Gizmos.DrawLine (Points [i - 1].position, Points [i].position);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您可以将玩家作为孩子添加到平台。这样,当平台移动时,玩家也会这样 在移动结束时(或在某些用户输入上),您可以打破父母。这是您可以尝试的一些代码。

public GameObject platform; //The platform Game Object
public GameObject player;   //The player Game Object

//Call this to have the player parented to the platform.
//So, say for example, you have a trigger that the player steps on 
//to activate the platform, you can call this method then
void AttachPlayerToPlatform () {
    player.transform.parent = platform.transform;
}

//Use this method alternatively, if you wish to specify which 
//platform the player Game Object needs to attach to (more useful)
void AttachPlayerToPlatform (GameObject platformToAttachTo) {
    player.transform.parent = platformToAttachTo.transform;
}

//Call this to detach the player from the platform
//This can be used say at the end of the platform's movement
void DetachPlayerFromPlatform () {
    player.transform.parent = null;
}

答案 1 :(得分:1)

如果你站在现实世界的移动平台上,你移动平台的原因是因为平台和你的脚之间的摩擦。如果平台被冰覆盖,如果平台开始运动,你可能不会随平台移动!

现在我们可以在游戏的物理中实现这一点。通常情况下,当你不想走路时,你的摩擦力可能会让你的玩家减速到零速。相反,您可以检查撞击时的地面对撞机,看它是否有移动的平台脚本,并获得平台的速度。然后使用此速度作为运动计算的“零点”。

答案 2 :(得分:1)

我认为这很难,但实际上我发现制作一个似乎运行良好的解决方案非常简单。

我游戏中的角色有一个Box Collider。它不是触发器,也不使用物理材料。它有一个质量为1的刚体,0的阻力,0.05的角度阻力,使用重力,不是运动学,不插值,碰撞检测是离散的。它没有约束。对于横向移动,我使用transform.Translate并且为了跳跃我使用Rigidbody的AddForceForceMode.VelocityChange作为最终参数。

移动平台还有一个Box Collider。我根据每个帧上transform.position的调用结果设置Vector3.Lerp。我在移动平台上也有这个脚本:

void OnCollisionEnter(Collision collision) {
    // Attach the object to this platform, but have them stay where they are in world space.
    collision.transform.SetParent(transform, true);
    Debug.Log("On platform.");
}

void OnCollisionExit(Collision collision) {
    // Detach the object from this platform, but have them stay where they are in world space.
    collision.transform.SetParent(transform.parent, true);
    Debug.Log("Off platform.");
}

这就是全部。实际上,脚本中只有两行代码来声明我实现了这两种方法,然后每种方法的实际实现都是一行。一开始的所有信息仅用于碰撞检测在您的项目中没有实际运行的情况(我总是忘记规则是什么做什么和不算作碰撞和/或触发器。)