在阅读这篇文章时,请记住,我对编程和统一都是新手,所以我可能错过了某些术语或工具。请用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);
}
}我猜是颠簸吧?)
发布于 2015-08-03 14:18:11
您可以使用流畅的跟随脚本。我已经为您创建了一个示例类。该类具有跟踪任何给定的游戏对象的特性,并具有一定的延迟和偏移量。你将不得不根据你的需要调整一些价值观。
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);
}
}
}将此类附加到您的玩家同伴,使用不同的值进行游戏。
发布于 2015-08-03 12:02:02
为了保持目标定向,你的同伴不应该是你的主要角色的孩子。
您的wayPoint不需要是一个GameObject,而是一个转换,您的代码看起来会更好。
如果你的游戏是一个2D平台,你和你的同伴需要向后,你的玩家,它可能只适用于一个轴(X?)因此,您可以通过在您的waiPoint函数上这样计算它,从而更直接地减少您的UpdatePosition:
wayPoint.position = transform.position * (Vector3.left * distance);在那里,您的“距离”可以是一个公共浮点,以方便地设置。
因此,在您的配套脚本更新,只需做:
transform.position = Vector3.MoveTowards(transform.position, wayPoint.position, speed * Time.deltaTime);我现在不能测试它,所以您可能会遇到Vector3乘运算的问题,只需注释一下,我将尽量修复;)
https://stackoverflow.com/questions/31779550
复制相似问题