- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
阅读本文时,请记住我是编程和 Unity 的新手,因此我可能会遗漏一些 Unity 提供的术语或工具。请以 ELI5 方式详细说明您的答案。提前致谢!
我目前正在为一个小型个人项目研究一些游戏物理。目前我已经创建了一个平台、一个角色以及应该是什么,一个追随者。
但是,由于我还没有达到可以自己编写完美代码的水平,所以我找到了一个“敌人”脚本并尝试对其进行了一些修改。它可以在一定程度上发挥作用,但需要进行一些调整,我希望我能帮助你们了解这一点。
这是现在的样子(橙色方 block 是同伴)
它跟随玩家,我可以调整速度以适合作为同伴而不是玩家。然而,如图所示,同伴跑向玩家的中心。我想要创建的是一个跟随玩家的同伴,但仍然与玩家保持很小的距离。
我的第一个想法是创建某种永久偏移,但我想不出如何在不弄乱跟随功能的情况下做到这一点。
我希望你能帮助我,我将不胜感激!
这是引用代码。
附加到播放器的代码:
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/
阅读本文时,请记住我是编程和 Unity 的新手,因此我可能会遗漏一些 Unity 提供的术语或工具。请以 ELI5 方式详细说明您的答案。提前致谢! 我目前正在为一个小型个人项目研究一些游戏物理。目
我已经开始使用 GNU radio 并且正在关注这个 YouTube tutorial系列来熟悉 GRC。 在学习教程时,我注意到我缺少导师使用的许多 GUI 小部件,例如变量 slider 小部件。
我已经开始使用 GNU radio 并且正在关注这个 YouTube tutorial系列来熟悉 GRC。 在学习教程时,我注意到我缺少导师使用的许多 GUI 小部件,例如变量 slider 小部件。
我使用以下教程在我的 Android 项目中实现了 Jacoco https://proandroiddev.com/unified-code-coverage-for-android-revisit
我目前正在通过http://ruby.railstutorial.org/它使用命令“mate”通过终端访问Textmate。自从我第一次购买它以来,我的 Macbook Air(Lion OS)上就
我是安卓工作室的新手。 在创建我的第一个 android 应用程序时。 它显示了一些错误.. Gradle sync started 11:00:29 AM Gradle sync failed: P
我是一名优秀的程序员,十分优秀!