1

ゲーム用にいくつかの動くプラットフォームを作成しました。プラットフォームのサイクルの開始と終了を定義する子を持つ 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);
        }
    }
}
4

3 に答える 3

2

Player を子としてプラットフォームに追加できます。そうすれば、プラットフォームが移動すると、プレイヤーも移動します。
移動の最後 (または何らかのユーザー入力) で、親子関係を壊すことができます。試してみることができるいくつかのコードを次に示します。

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;
}
于 2014-12-31T10:51:44.793 に答える
1

現実世界で動いている台の上に立った場合、台と一緒に動くのは、台と足の間の摩擦のためです。プラットフォームが氷で覆われている場合、プラットフォームに乗っているときにプラットフォームが動き始めると、プラットフォームと一緒に動かない可能性があります。

これで、ゲームの物理演算にこれを実装できます。通常、歩行しようとしていないときは、摩擦によってプレーヤーの速度がゼロになるだけです。代わりに、地面コライダーの衝突をチェックして、動くプラットフォーム スクリプトがあるかどうかを確認し、プラットフォームの速度を取得できます。次に、この速度をモーション計算の「ゼロ ポイント」として使用します。

于 2015-01-05T21:06:12.617 に答える