【问题标题】:Unity2D - CompanionUnity2D - 伴侣
【发布时间】:2015-08-06 11:51:07
【问题描述】:

阅读本文时,请记住我对编程和 Unity 都是新手,因此我可能会遗漏 Unity 提供的一些术语或工具。请以 ELI5 的方式详细说明您的答案。提前致谢!

我目前正在为一个小型个人项目研究一些游戏物理。目前我已经创建了一个平台,一个角色以及应该是什么,一个跟随伙伴。

但是,由于我还没有达到可以自己创建完美代码的水平,所以我找到了一个“敌人”脚本并尝试对其进行一些修改。 它可以在一定范围内工作,但需要一些调整,希望我能帮助你们了解。

这就是它现在的样子(橙色方块是同伴)

它跟随玩家,我可以调整速度以适应同伴,而不是玩家。然而,正如图片所示,同伴跑向玩家的中心。我想要创建的是一个跟随玩家,但仍然与玩家保持小距离的同伴。

我的第一个想法是创建某种永久偏移,但我无法弄清楚如何在不搞乱跟随功能的情况下做到这一点。

我希望你能帮助我,将不胜感激!

这是供参考的代码。

附加到播放器的代码:

using UnityEngine;
using System.Collections;

public class PlayerCompanion : MonoBehaviour
{

    //In the editor, add your wayPoint gameobject to the script.
    public GameObject wayPoint;

    //This is how often your waypoint's position will update to the player's position
    private float timer = 0.5f;

    void Update ()
    {
        if (timer > 0) {
            timer -= Time.deltaTime;
        }
        if (timer <= 0) {
            //The position of the waypoint will update to the player's position
            UpdatePosition ();
            timer = 0.5f;
        }
    }

    void UpdatePosition ()
    {
        //The wayPoint's position will now be the player's current position.
        wayPoint.transform.position = transform.position;
    }
}

附加到同伴的代码:

using UnityEngine;
using System.Collections;

public class FollowerOffset : MonoBehaviour {

    //You may consider adding a rigid body to the zombie for accurate physics simulation
    private GameObject wayPoint;

    private Vector3 wayPointPos;

    //This will be the zombie's speed. Adjust as necessary.
    private float speed = 10.0f;

    void Start ()
    {
        //At the start of the game, the zombies will find the gameobject called wayPoint.
        wayPoint = GameObject.Find("wayPoint");
    }

    void Update ()
    {
        wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
        //Here, the zombie's will follow the waypoint.
        transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
    }

}

撞,我猜? :)

【问题讨论】:

  • 如果你给两者都添加一个刚体,那么 Unity 的内置物理会阻止跟随者“进入”目标。
  • @MickyDuncan 尝试为两个对象添加刚体,似乎没有任何改变
  • 哦,您正在使用Vector3.MoveTowards() - 覆盖刚体。看看这个answers.unity3d.com/questions/368727/…
  • 所以我正在尝试使用您的链接显示的代码,但是我收到错误:“var dir: Vector3”“:”是一个意外的符号。我把那个代码放错地方了吗?目前我把它都放在了 void Update()
  • 他们的例子是 UnityScript。 c# 代码将是 var dir = targetPos - transform.position; 有关转换的帮助,请参阅 m2h.nl/files/js_to_c.php

标签: c# unity3d


【解决方案1】:

您可以使用平滑跟随脚本。我为您创建了一个示例类。此类具有跟随任何给定游戏对象的功能,具有一些延迟和偏移。您必须根据需要调整一些值。

using UnityEngine;
using System.Collections;

public class PlayerCompanion : MonoBehaviour
{
    [SerializeField]
    private GameObject wayPoint;
    [SerializeField]
    public Vector3 offset;
    public Vector3 targetPos;//Edit: I forgot to declare this on firt time
    public float interpVelocity;
    public float cameraLerpTime = .1f;
    public float followStrength = 15f;

    // Use this for initialization
    void Start ()
    {
        //At the start of the game, the zombies will find the gameobject called wayPoint.
        wayPoint = GameObject.Find("wayPoint");
        offset = new Vector3 (5,0,0);//input amount of offset you need
    }

    void FixedUpdate () {
        if (wayPoint) {
            Vector3 posNoZ = transform.position;

            Vector3 targetDirection = (wayPoint.transform.position - posNoZ);
            interpVelocity = targetDirection.magnitude * followStrength;
            targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); 

            transform.position = Vector3.Lerp (transform.position, targetPos + offset, cameraLerpTime);
        }

    }
}

将这个类附加到你的玩家伴侣身上,玩不同的价值观。

【讨论】:

  • 我尝试使用您的解决方案,因为它看起来很合理。但是,它在目标位置给了我一个错误。它没有任何上下文。
  • 首先,您要我编辑“PlayerCompanion”或“FollowerOffset”中的代码吗?我做了一些糟糕的命名,Followeroffset 是给同伴的,而“PlayerCompanion”是给玩家的。它可能让你感到困惑。但是我现在使它适合 FollowerOffset,但出现错误,targetPos 不在上下文中
  • targetPos 仍有问题:/
  • 您需要将此脚本附加到跟随者游戏对象(Companion)。并且 wayPoint 将是它应该遵循的游戏对象(Player)。您必须调整 cameraLerpTime、followStrength 和 offset 的值才能获得所需的结果。
  • 很抱歉这么麻烦=) 那我应该把wayPoint 作为targetPos 吗?除了 targetPos 错误(CS0103)之外,我还遇到了转换器错误(CS1502 和 CS1503),它在转换浮点数、向量和游戏对象方面存在问题。
【解决方案2】:

为了保持面向对象,你的同伴无论如何都不应该是你主角的孩子。

你的 wayPoint 不需要是一个游戏对象,而是一个变换,你的代码看起来会更好。

如果您的游戏是一个 2D 平台,您和您的同伴需要倒退您的玩家,它可能仅适用于一个轴(X?),因此您可以通过在 UpdatePosition 函数上计算它以更直接的方式减少 waiPoint,例如这个:

wayPoint.position = transform.position * (Vector3.left * distance);

您的“距离”可以是一个易于设置的公共浮动。

所以你的配套脚本更新只是这样做:

transform.position = Vector3.MoveTowards(transform.position, wayPoint.position, speed * Time.deltaTime);

我现在无法对其进行测试,因此您可能会遇到 Vector3 乘法运算的问题,请发表评论,我会尽力修复;)

【讨论】:

  • 尝试了您的解决方案,但在“wayPoint.position”出现错误
  • 和 Vector3.left * 距离处的错误。 (不能在上面加上 *)
  • 其实我觉得不是乘数本身的问题,可能是操作数方向的问题。但是 Neeraj 的解决方案应该可以正常工作我已经支持了他的答案,因为我无法测试自己新航点的计算。
猜你喜欢
  • 2018-08-17
  • 2011-04-11
  • 2011-06-13
  • 2019-12-15
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 2021-04-25
  • 2020-01-16
相关资源
最近更新 更多