gpt4 book ai didi

c# - Unity2D - 伴侣

转载 作者:行者123 更新时间:2023-11-30 17:42:40 25 4
gpt4 key购买 nike

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

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

但是,由于我还没有达到可以自己编写完美代码的水平,所以我找到了一个“敌人”脚本并尝试对其进行了一些修改。它可以在一定程度上发挥作用,但需要进行一些调整,我希望我能帮助你们了解这一点。

这是现在的样子(橙色方 block 是同伴)

enter image description here

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

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

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

这是引用代码。

附加到播放器的代码:

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);
}

}

碰撞,我猜? :)

最佳答案

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

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);
}

}
}

将这个类附加到你的玩家伙伴,玩不同的值。

关于c# - Unity2D - 伴侣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com