我正在制作一个游戏,其中一个物体主要是飞行,我想要一些帮助,因为我有一个向前的速度,当精灵落到地面时它会继续car然而其余的代码仍然很好
using UnityEngine;
using System.Collections;
public class BirdMovment : MonoBehaviour {
Vector3 Velocity = Vector3.zero;
public Vector3 gravity;
public Vector3 flapVelocity;
public float maxSpeed = 5f;
public float forwardSpeed = 1f;
bool didFlap = false;
Animator animator;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
void Update (){
if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0))
{
didFlap = true;
}
}
// Physics update goes here
void FixedUpdate () {
Velocity.x = forwardSpeed;
Velocity += gravity * Time.deltaTime;
if (didFlap) {
animator.SetTrigger("DoFlap");
didFlap = false;
if(Velocity.y < 0)
Velocity.y = 0;
Velocity += flapVelocity;
}
Velocity = Vector3.ClampMagnitude (Velocity, maxSpeed);
transform.position += Velocity * Time.deltaTime;
float angle = 0;
if(Velocity.y < 0)
{
angle = Mathf.Lerp(0 , -45 ,-Velocity.y / maxSpeed);
}
transform.rotation = Quaternion.Euler(0,0,angle);
}
}
你能告诉我应该放置代码的位置,以便在我的物体撞击地面时使前进速度停止
答案 0 :(得分:0)
有两种快速的方法可以让我想到这一点。
第一种方法是向对象添加多个状态以更改其在更新循环期间的操作。例如,如果对象要击中地面,您可以将状态设置为“空闲”(或者您想要调用的任何内容)。根据状态,更新循环将调用特定函数来更新对象速度。在状态为空闲的情况下,更新功能可以简单地将对象速度设置为零。
另一种方法是在碰撞地面时简单地将前进速度设置为零。这不一定是最好的方式,但也是一种选择。
答案 1 :(得分:0)
所以,基本上,我认为最简单的方法是添加一个Rigidbody2D(我假设您在制作一个2D游戏)并让物理引擎为我们完成大部分工作。你还需要添加某种2D对撞机,以防你的鸟在第一时间没有。我已经修改了你的脚本以使用物理引擎,而不是直接操作变换位置,尝试并告诉我它是否有效:)
using UnityEngine;
using System.Collections;
// Added this to make sure that you'll not forget to add a rigidbody and a collider to your game object
[RequireComponent(typeof(Rigidbody2D), typeof(CircleCollider2D))]
public class BirdMovment : MonoBehaviour
{
// I've changed it to Vector2, so it would be more comfortable to use with 2D physics
public Vector2 flapVelocity;
public float maxSpeed = 5f;
public float forwardSpeed = 1f;
bool didFlap = false;
Animator animator;
// I'm assuming here that your game is 2D and used 2D physics.
// If not - just switch to the regular Rigidbody, only a few modifications will be needed, if at all
Rigidbody2D myRigidbody;
// Here we will store the velocity achieved by flapping
// It's calculating separately from the overall velocity and forwardSpeed for simplicity
Vector2 userVelocity = Vector2.zero;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
myRigidbody = rigidbody2D;
}
void Update (){
if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0))
{
didFlap = true;
}
}
// Physics update goes here
void FixedUpdate () {
// We do not need to apply gravity, as the rigidbody does it for us (and it's doing so in more realistic form that can simply be achieved)
//Velocity += gravity * Time.deltaTime;
// This is needed because you wan't to reset birds velocity on a flap, but at the same time want to retain realistic falling due to the gravity
userVelocity.y = myRigidbody.velocity.y;
if (didFlap) {
animator.SetTrigger("DoFlap");
didFlap = false;
// Here we use user velocity instead of directly applying to rigidbody's velocity to simplify everything a little bit
if (userVelocity.y < 0)
userVelocity.y = 0;
userVelocity += flapVelocity;
}
userVelocity = Vector2.ClampMagnitude(userVelocity, maxSpeed);
// If we will directly change our transform avoiding the physics engine, everything will brake, so we don't want to do that :)
//transform.position += Velocity * Time.deltaTime;
// Instead we are setting the velocity of our rigidbody, so the physics engine will make all work for us
myRigidbody.velocity = userVelocity;
// Adding a forwardSpeed to our velocity
myRigidbody.velocity = new Vector2(forwardSpeed, myRigidbody.velocity.y);
// This part is mostly intact, only we're now using our rigidbody's velocity for calculations
float angle = 0;
if(myRigidbody.velocity.y < 0)
{
angle = Mathf.Lerp(0, -45, -myRigidbody.velocity.y / maxSpeed);
}
transform.rotation = Quaternion.Euler(0,0,angle);
}
}