[英]Unity2D - Companion
阅读本文时,请记住我是编程和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);
}
}
碰撞,我猜? :)
您可以使用流畅的跟随脚本。 我为你创建了一个示例类。 这个类具有跟随任何给定游戏对象的功能,具有一些延迟和偏移。 您必须根据需要调整一些值。
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);
}
}
}
将此课程附加到您的播放器伴侣,使用不同的值。
为了保持面向对象,你的同伴不应该是你主角的孩子。
您的wayPoint不需要是GameObject而是Transform,而您的代码看起来会更好。
如果您的游戏是2D平台,您和您的同伴需要向后移动您的播放器它可能仅适用于一个轴(X?),因此您可以通过在UpdatePosition函数上计算它来更直接地减少您的waiPoint:
wayPoint.position = transform.position * (Vector3.left * distance);
你的“距离”可以是一个容易设置的公共浮动。
所以你的伴侣脚本更新只是做:
transform.position = Vector3.MoveTowards(transform.position, wayPoint.position, speed * Time.deltaTime);
我现在无法测试它,所以你可能会遇到Vector3乘法操作的问题,只是注释,我会尽可能地修复;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.