C# 如何使精灵在与统一体中的对象碰撞时停止
我正在做一个游戏,其中一个物体主要是飞行,我想得到一些帮助,因为我有一个前进的速度,当精灵落在地上它龋齿继续前进,但其余的代码仍然是好的C# 如何使精灵在与统一体中的对象碰撞时停止,c#,unity3d,2d,C#,Unity3d,2d,我正在做一个游戏,其中一个物体主要是飞行,我想得到一些帮助,因为我有一个前进的速度,当精灵落在地上它龋齿继续前进,但其余的代码仍然是好的 using UnityEngine; using System.Collections; public class BirdMovment : MonoBehaviour { Vector3 Velocity = Vector3.zero; public Vector3 gravity; public Vector3 flapVel
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);
}
}
使用UnityEngine;
使用系统集合;
公共类鸟类迁徙:单一行为{
矢量3速度=矢量3.0;
公共矢量3引力;
公共向量3 flapVelocity;
公共浮点数maxSpeed=5f;
公共浮球前进速度=1f;
bool-didFlap=false;
动画师;
//用于初始化
无效开始(){
animator=GetComponent();
}
无效更新(){
if(Input.GetKeyDown(KeyCode.Space)| Input.GetMouseButtonDown(0))
{
didFlap=真;
}
}
//物理更新在这里
无效固定更新(){
速度x=前进速度;
速度+=重力*时间增量;
if(didFlap){
设置触发器(“DoFlap”);
didFlap=false;
if(速度y<0)
速度y=0;
速度+=松弛度;
}
速度=矢量3.0毫米振幅(速度,最大速度);
transform.position+=速度*时间增量;
浮动角度=0;
if(速度y<0)
{
角度=Mathf.Lerp(0,-45,-速度.y/最大速度);
}
transform.rotation=四元数.Euler(0,0,角度);
}
}
你能告诉我应该把代码放在哪里,以便在我的物体撞击地面时使前进速度停止吗?有两种快速方法可以做到这一点,我可以从头顶上想到 第一种方法是向对象添加多个状态,以更改其在更新循环中的操作。例如,如果对象要触地,您会将状态设置为“空闲”(或您想称之为“空闲”)状态。更新循环将根据状态调用特定函数来更新对象速度。在状态为空闲的情况下,更新功能可以简单地将对象速度设置为零
另一种方法是,当你与地面碰撞时,简单地将前进速度设置为零。这不一定是最好的方法,但仍然是一种选择。因此,基本上,我认为最简单的方法是添加一个Rigidbody2D(我在这里假设您正在制作一个2D游戏),让物理引擎为我们做大部分工作。你还需要添加某种2D对撞机,以防你的鸟一开始没有。我已将您的脚本修改为使用物理引擎,而不是直接操纵变换位置,请尝试并告诉我它是否有效:)
使用UnityEngine;
使用系统集合;
//添加此选项以确保您不会忘记向游戏对象添加刚体和碰撞器
[RequiredComponent(typeof(rigiidbody2d)、typeof(CircleCollider2D))]
公共类鸟类迁徙:单一行为
{
//我把它改成了Vector2,这样使用2D物理会更舒服
公共向量2 flapVelocity;
公共浮点数maxSpeed=5f;
公共浮球前进速度=1f;
bool-didFlap=false;
动画师;
//我在这里假设你的游戏是2D的,使用2D物理。
//如果不是-只需切换到常规刚体,如果需要的话,只需进行一些修改
刚体;
//在这里,我们将存储通过拍打获得的速度
//为了简单起见,它与总速度和前进速度分开计算
Vector2用户速度=Vector2.0;
//用于初始化
无效开始(){
animator=GetComponent();
肉豆蔻体=刚体2d;
}
无效更新(){
if(Input.GetKeyDown(KeyCode.Space)| Input.GetMouseButtonDown(0))
{
didFlap=真;
}
}
//物理更新在这里
无效固定更新(){
//我们不需要应用重力,就像刚体为我们所做的那样(它以更现实的形式这样做,可以简单地实现)
//速度+=重力*时间增量;
//这是必要的,因为您不想重置襟翼上的鸟类速度,但同时希望保留由于重力而产生的真实下落
userVelocity.y=myRigidbody.velocity.y;
if(didFlap){
设置触发器(“DoFlap”);
didFlap=false;
//在这里,我们使用用户速度,而不是直接应用于刚体的速度来简化一切
if(userVelocity.y<0)
userVelocity.y=0;
userVelocity+=flapVelocity;
}
userVelocity=Vector2.ClampMagnitude(userVelocity,maxSpeed);
//如果我们避开物理引擎直接改变变换,一切都会停止,所以我们不想这样做:)
//transform.position+=速度*时间增量;
//相反,我们正在设置刚体的速度,所以物理引擎将使我们的一切工作
myRigidbody.velocity=用户速度;
//给我们的速度增加一个前进速度
myRigidbody.velocity=新矢量2(前进速度,myRigidbody.velocity.y);
//这部分基本上是完整的,只是我们现在用刚体的速度来计算
浮动角度=0;
if(体速度y<0)
{
角度=Mathf.Lerp(0,-45,-myRigidbody.velocity.y/maxSpeed);
}
transform.rotation=四元数.Euler(0,0,角度);
}
}
你能告诉我如何编写代码以及将代码放在哪里吗?我是一个游戏初学者。这是一个很好的解释,说明了碰撞遮罩是如何在unity中工作的。它应该给你一个基本的例子,说明如何在统一中建立碰撞。另一个链接是回调函数f
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);
}
}