该脚本使用逼真的重力使GameObject跳转并到达特定目标。 GameObject将 relative 跳转到目标,从而调整其轨迹。当目标点移动时它也可以工作。
当GameObject到达目标时,目标会移到更高的位置(position.y += 50
)并且GameObject会向那里跳跃。因此,我们有一个“连续跳跃周期”。
脚本:
using UnityEngine;
using System.Collections;
public class Jumper : MonoBehaviour
{
public Vector3 _gravity = 9.8f * Vector3.down;
public bool stillJumping = true;
public GameObject Target1;
public void SetVelocityToJump(GameObject goToJumpTo, float timeToJump)
{
StartCoroutine(jumpAndFollow(goToJumpTo, timeToJump));
}
private IEnumerator jumpAndFollow(GameObject goToJumpTo, float timeToJump)
{
var startPosition = transform.position;
var targetTransform = goToJumpTo.transform;
var lastTargetPosition = targetTransform.position;
var initialVelocity = getInitialVelocity(lastTargetPosition - startPosition, timeToJump);
var progress = 0f;
while (progress < timeToJump)
{
progress += Time.deltaTime;
if (targetTransform.position != lastTargetPosition)
{
lastTargetPosition = targetTransform.position;
initialVelocity = getInitialVelocity(lastTargetPosition - startPosition, timeToJump);
}
transform.position = startPosition + (progress * initialVelocity) + (0.5f * Mathf.Pow(progress, 2) * _gravity);
yield return null;
}
OnFinishJump(target, timeToJump);
}
private void OnFinishJump(GameObject target, float timeToJump)
{
if (stillJumping)
{
Vector3 temp1 = Target1.transform.position;
temp1.y += 12.5f;
Target1.transform.position = temp1;
StartCoroutine(jumpAndFollow(target, timeToJump));
}
}
private Vector3 getInitialVelocity(Vector3 toTarget, float timeToJump)
{
return (toTarget - (0.5f * Mathf.Pow(timeToJump, 2) * _gravity)) / timeToJump;
}
}
使用上面的脚本,GameObject总是从它到达的位置跳转。这就是我所说的“连续跳跃周期”
JumpTarget的脚本:
public class JumpTarget : MonoBehaviour
{
public Jumper Jumper;
public float TimeToJump;
public void Start()
{
if (Jumper != null) Jumper.SetVelocityToJump(gameObject, TimeToJump);
}
}
问题在于:我们如何让GameObject每次都继续跳(Input.GetMouseButtonUp(0))
(而不仅仅是第一次跳转)?我无法安静搞清楚。
以下代码应该会发生这种情况,但GameObject甚至没有跳过!
if (Input.GetMouseButtonUp(0) && Jumper != null) Jumper.SetVelocityToJump(gameObject, TimeToJump);
我尝试过调试,但不幸的是,我的Unity编辑器每次都崩溃了。
编辑:我曾抱怨我的脚本正常工作(Input.GetMouseButtonUp(0)
如果我的脚本适合您,请上传一个示例项目,我的脚本正常运行,作为答案。我怀疑我的其他脚本正在干扰...谢谢。
答案 0 :(得分:0)
如果您想“继续”跳跃,请使用GetMouseButton(...)
代替GetMouseButtonDown(...)
,因为后者只会触发一次。
using UnityEngine;
using System.Collections;
public class Jumper : MonoBehaviour
{
public Vector3 _gravity = 9.8f * Vector3.down;
public GameObject Target1;
public void SetVelocityToJump(GameObject goToJumpTo, float timeToJump)
{
StartCoroutine(jumpAndFollow(goToJumpTo, timeToJump));
}
private IEnumerator jumpAndFollow(GameObject goToJumpTo, float timeToJump)
{
var startPosition = transform.position;
var targetTransform = goToJumpTo.transform;
var lastTargetPosition = targetTransform.position;
var initialVelocity = getInitialVelocity(lastTargetPosition - startPosition, timeToJump);
var progress = 0f;
while (progress < timeToJump)
{
progress += Time.deltaTime;
if (targetTransform.position != lastTargetPosition)
{
lastTargetPosition = targetTransform.position;
initialVelocity = getInitialVelocity(lastTargetPosition - startPosition, timeToJump);
}
transform.position = startPosition + (progress * initialVelocity) + (0.5f * Mathf.Pow(progress, 2) * _gravity);
yield return null;
}
OnFinishJump(target, timeToJump);
}
private void OnFinishJump(GameObject target, float timeToJump)
{
if (Input.GetMouseButton(0))
{
Vector3 temp1 = Target1.transform.position;
temp1.y += 12.5f;
Target1.transform.position = temp1;
StartCoroutine(jumpAndFollow(target, timeToJump));
}
}
private Vector3 getInitialVelocity(Vector3 toTarget, float timeToJump)
{
return (toTarget - (0.5f * Mathf.Pow(timeToJump, 2) * _gravity)) / timeToJump;
}
}
public class JumpTarget : MonoBehaviour
{
public Jumper Jumper;
public float TimeToJump;
public void Start()
{
if (Jumper != null && Input.GetMouseButton(0)) Jumper.SetVelocityToJump(gameObject, TimeToJump);
}
}