使用原地跳跃动画移动
Move with InPlace Jump Animation
我是新手。这是一个非常简单的疑问,但我不知道该怎么做。我想让我的角色向前跳 1.And 我这里的动画是 Inplace(他在 y 上升然后回来)。所以我必须在播放动画时以编程方式向前移动我的角色,它应该看起来很流畅.所以这是我附加到角色的脚本。
if(Input.GetKeyDown(KeyCode.Space))
{
this.gameObject.GetComponent<Animator>().SetTrigger("shouldJump?");
transform.position += 1*transform.forward;
}
但我的情况是他前进 1,然后播放 JumpAnimation。
请帮忙!!
试试多线程吧。使用一名工人制作跳跃动画,另一名工人制作运动。不过,这可能会导致其他问题,尤其是在平滑度方面……也许请查看文档。
您在这里所做的是将角色位置设置为在 z 轴上向前移动一个单位,并告诉动画师在玩家每次按下 space 按钮时开始跳跃动画。要在多个帧的范围内移动变换,您必须逐渐修改更新方法中的位置或使用物理引擎。
您可以在 Unity 中检查 Unify community wiki for some examples or go through some of the many tutorials 玩家移动。
来自维基:
- RigidbodyFPSWalker - 我最喜欢的脚本之一,在用移动干扰任何游戏时参考。使用物理引擎,因此您的角色需要
RigidBody
和 Collider
(CapsuleCollider
、SphereCollider
或 BoxCollider
)
// From: http://wiki.unity3d.com/index.php/RigidbodyFPSWalker
// Creative Common's Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0).
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (CapsuleCollider))]
public class CharacterControls : MonoBehaviour {
public float speed = 10.0f;
public float gravity = 10.0f;
public float maxVelocityChange = 10.0f;
public bool canJump = true;
public float jumpHeight = 2.0f;
private bool grounded = false;
void Awake () {
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
void FixedUpdate () {
if (grounded) {
// Calculate how fast we should be moving
Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
// Apply a force that attempts to reach our target velocity
Vector3 velocity = rigidbody.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
// Jump
if (canJump && Input.GetButton("Jump")) {
rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(new Vector3 (0, -gravity * rigidbody.mass, 0));
grounded = false;
}
void OnCollisionStay () {
grounded = true;
}
float CalculateJumpVerticalSpeed () {
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
}
我是新手。这是一个非常简单的疑问,但我不知道该怎么做。我想让我的角色向前跳 1.And 我这里的动画是 Inplace(他在 y 上升然后回来)。所以我必须在播放动画时以编程方式向前移动我的角色,它应该看起来很流畅.所以这是我附加到角色的脚本。
if(Input.GetKeyDown(KeyCode.Space))
{
this.gameObject.GetComponent<Animator>().SetTrigger("shouldJump?");
transform.position += 1*transform.forward;
}
但我的情况是他前进 1,然后播放 JumpAnimation。 请帮忙!!
试试多线程吧。使用一名工人制作跳跃动画,另一名工人制作运动。不过,这可能会导致其他问题,尤其是在平滑度方面……也许请查看文档。
您在这里所做的是将角色位置设置为在 z 轴上向前移动一个单位,并告诉动画师在玩家每次按下 space 按钮时开始跳跃动画。要在多个帧的范围内移动变换,您必须逐渐修改更新方法中的位置或使用物理引擎。
您可以在 Unity 中检查 Unify community wiki for some examples or go through some of the many tutorials 玩家移动。
来自维基:
- RigidbodyFPSWalker - 我最喜欢的脚本之一,在用移动干扰任何游戏时参考。使用物理引擎,因此您的角色需要
RigidBody
和Collider
(CapsuleCollider
、SphereCollider
或BoxCollider
)
// From: http://wiki.unity3d.com/index.php/RigidbodyFPSWalker
// Creative Common's Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0).
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (CapsuleCollider))]
public class CharacterControls : MonoBehaviour {
public float speed = 10.0f;
public float gravity = 10.0f;
public float maxVelocityChange = 10.0f;
public bool canJump = true;
public float jumpHeight = 2.0f;
private bool grounded = false;
void Awake () {
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
void FixedUpdate () {
if (grounded) {
// Calculate how fast we should be moving
Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
// Apply a force that attempts to reach our target velocity
Vector3 velocity = rigidbody.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
// Jump
if (canJump && Input.GetButton("Jump")) {
rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(new Vector3 (0, -gravity * rigidbody.mass, 0));
grounded = false;
}
void OnCollisionStay () {
grounded = true;
}
float CalculateJumpVerticalSpeed () {
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
}