C# 在与冰霜子弹碰撞时减速
我是Unity and C#的新手。我几周前开始玩这个2D游戏 所以基本上我想做的是当玩家被“冰霜子弹”击中时,它会造成伤害并随着时间的推移减慢玩家的速度。在我的剧本中,玩家被冰霜子弹击中,造成伤害,速度是恒定的,而不是随着时间的推移。 我希望这样,当玩家被冰霜子弹击中时,玩家应该减速2秒,然后他的移动速度恢复正常 这就是我的代码的样子:C# 在与冰霜子弹碰撞时减速,c#,unity3d,collision,2d-games,slowdown,C#,Unity3d,Collision,2d Games,Slowdown,我是Unity and C#的新手。我几周前开始玩这个2D游戏 所以基本上我想做的是当玩家被“冰霜子弹”击中时,它会造成伤害并随着时间的推移减慢玩家的速度。在我的剧本中,玩家被冰霜子弹击中,造成伤害,速度是恒定的,而不是随着时间的推移。 我希望这样,当玩家被冰霜子弹击中时,玩家应该减速2秒,然后他的移动速度恢复正常 这就是我的代码的样子: public class FrostHit : MonoBehaviour { float maxS = 40f; float slowSpeed = 20
public class FrostHit : MonoBehaviour
{
float maxS = 40f;
float slowSpeed = 20f;
public Rigidbody2D rb;
int damage = -5;
int timeOfReducedSpeed = 2;
bool isHit = false;
void Start()
{
rb.velocity = transform.right * slowSpeed;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player_Njinja")
{
HealthNjinja healthNjinja = other.gameObject.GetComponent<HealthNjinja>();
healthNjinja.ModifyHealth(damage);
PlayerMovementNjinja reduceMoveSpeed = other.gameObject.GetComponent<PlayerMovementNjinja>();
reduceMoveSpeed.runSpeed = slowSpeed;
Destroy(gameObject);
}
PlayerMovementKnight maxSpeed = other.gameObject.GetComponent<PlayerMovementKnight>();
maxSpeed.runSpeed = maxS;
HealthKnight healthKnight = other.gameObject.GetComponent<HealthKnight>();
if (other.gameObject.tag == "Player_Knight")
{
isHit = true;
healthKnight.ModifyHealth(damage);
StartCoroutine(speedTime());
Destroy(gameObject);
}
IEnumerator speedTime()
{
while (isHit == true)
{
slowPlayer();
yield return new WaitForSeconds(timeOfReducedSpeed);
revertSpeed();
}
}
void slowPlayer()
{
maxSpeed.runSpeed = slowSpeed;
}
void revertSpeed()
{
maxSpeed.runSpeed = maxS;
}
}
public class FrostHit:monobhavior
{
浮点最大值=40f;
浮子慢速=20f;
公共刚体2d rb;
整数伤害=-5;
降低速度的int时间=2;
bool-isHit=false;
void Start()
{
rb.velocity=transform.right*慢速度;
}
无效OnTiggerEnter2D(碰撞的R2D其他)
{
if(other.gameObject.tag==“Player\u Njinja”)
{
HealthNjinja HealthNjinja=other.gameObject.GetComponent();
healthNjinja.修改健康(损害);
PlayerMovementNjinja reduceMoveSpeed=other.gameObject.GetComponent();
reduceMoveSpeed.runSpeed=slowSpeed;
摧毁(游戏对象);
}
PlayerMovementKnight maxSpeed=other.gameObject.GetComponent();
maxSpeed.runSpeed=maxS;
HealthKnight HealthKnight=other.gameObject.GetComponent();
如果(other.gameObject.tag==“玩家”
{
isHit=真;
healthKnight.ModifyHealth(伤害);
开始例行程序(speedTime());
摧毁(游戏对象);
}
IEnumerator speedTime()
{
while(isHit==true)
{
slowPlayer();
产生返回新WaitForSeconds(速度降低的时间);
回复速度();
}
}
void slowPlayer()
{
maxSpeed.runSpeed=slowSpeed;
}
无效速度()
{
maxSpeed.runSpeed=maxS;
}
}
}
还有我的PlayerMovement代码:
public class PlayerMovementKnight : MonoBehaviour
{
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
public WeaponKnight Bullet;
public bool grounded;
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
animator.SetBool("IsJumping", true);
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
}
else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
if (grounded && GetComponent<Bullet>().knockBack == false)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
}
}
public void OnLanding()
{
animator.SetBool("IsJumping", false);
}
public void OnCrouching(bool isCrouching)
{
animator.SetBool("IsCrouching", isCrouching);
}
void FixedUpdate()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
公共类PlayerMovementKnight:单一行为
{
公共字符控制器2D控制器;
公共动画师;
公共浮子运行速度=40f;
浮动水平移动=0f;
布尔跳跃=假;
布尔·克劳奇=假;
公共武器;
公共场所;
无效更新()
{
水平移动=输入。GetAxisRaw(“水平”)*运行速度;
动画师.SetFloat(“速度”,Mathf.Abs(水平移动));
if(Input.GetButtonDown(“跳转”))
{
跳跃=真;
SetBool(“IsJumping”,true);
}
if(Input.GetButtonDown(“蹲伏”))
{
克劳奇=真;
}
else if(Input.GetButtonUp(“cruch”))
{
克劳奇=假;
}
if(固定&&GetComponent().knockBack==false)
{
GetComponent().velocity=新向量2(0,0);
}
}
公共无效联机()
{
动画师.SetBool(“IsJumping”,false);
}
公共空间置换(bool Iscouching)
{
SetBool(“Iscoruching”,Iscoruching);
}
void FixedUpdate()
{
控制器。移动(水平移动*时间。固定时间,蹲下,跳跃);
跳跃=假;
}
}问题似乎是,在调用协同程序之前设置为
true
的bool isHit
永远不会重置为false
因此,循环:
while (isHit == true)
{
slowPlayer();
yield return new WaitForSeconds(timeOfReducedSpeed);
revertSpeed();
}
实际上是一个无限循环。
您可能希望在某个地方将bool重置为false
编辑:
public class FrostHit : MonoBehaviour
{
float maxS = 40f;
float slowSpeed = 20f;
public Rigidbody2D rb;
int damage = -5;
int timeOfReducedSpeed = 2;
void Start()
{
rb.velocity = transform.right * slowSpeed;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player_Njinja")
{
HealthNjinja healthNjinja = other.gameObject.GetComponent<HealthNjinja>();
healthNjinja.ModifyHealth(damage);
PlayerMovementNjinja mSpeedNinja = other.gameObject.GetComponent<PlayerMovementNjinja>();
StartCoroutine(speedTimeNinja());
}
else if (other.gameObject.tag == "Player_Knight")
{
HealthKnight healthKnight = other.gameObject.GetComponent<HealthKnight>();
healthKnight.ModifyHealth(damage);
PlayerMovementKnight mSpeedKnight = other.gameObject.GetComponent<PlayerMovementKnight>();
StartCoroutine(speedTimeKnight());
}
// Destroy Bullet
Destroy(gameObject);
}
IEnumerator speedTimeKnight()
{
mSpeedKnight.runSpeed = slowSpeed;
yield return new WaitForSeconds(timeOfReducedSpeed);
mSpeedKnight.runSpeed = maxS;
}
IEnumerator speedTimeNinja()
{
mSpeedNinja.runSpeed = slowSpeed;
yield return new WaitForSeconds(timeOfReducedSpeed);
mSpeedNinja.runSpeed = maxS;
}
}
在评论之后,我现在看到您的脚本已附加到bullet游戏对象。在启动协同程序并减慢玩家速度后,您销毁(游戏对象)代码>
在那之后,脚本也被破坏了,协同进程因此停止了……并且没有任何东西可以让玩家的速度恢复到正常水平
您应该将协同程序移动到玩家的脚本中,并在需要时启动它。这样,在销毁子弹后,它仍然存在。您的boolisHit
永远不会设置为false。导致玩家被减速,然后在被击中一次后被永久重置
首先不需要创建isHit
。为什么在输入IEnumerator
时,仅当Player\u Knight
被击中时,才需要检查玩家是否被击中
代码示例:
public class FrostHit : MonoBehaviour
{
float maxS = 40f;
float slowSpeed = 20f;
public Rigidbody2D rb;
int damage = -5;
int timeOfReducedSpeed = 2;
void Start()
{
rb.velocity = transform.right * slowSpeed;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player_Njinja")
{
HealthNjinja healthNjinja = other.gameObject.GetComponent<HealthNjinja>();
healthNjinja.ModifyHealth(damage);
PlayerMovementNjinja mSpeedNinja = other.gameObject.GetComponent<PlayerMovementNjinja>();
StartCoroutine(speedTimeNinja());
}
else if (other.gameObject.tag == "Player_Knight")
{
HealthKnight healthKnight = other.gameObject.GetComponent<HealthKnight>();
healthKnight.ModifyHealth(damage);
PlayerMovementKnight mSpeedKnight = other.gameObject.GetComponent<PlayerMovementKnight>();
StartCoroutine(speedTimeKnight());
}
// Destroy Bullet
Destroy(gameObject);
}
IEnumerator speedTimeKnight()
{
mSpeedKnight.runSpeed = slowSpeed;
yield return new WaitForSeconds(timeOfReducedSpeed);
mSpeedKnight.runSpeed = maxS;
}
IEnumerator speedTimeNinja()
{
mSpeedNinja.runSpeed = slowSpeed;
yield return new WaitForSeconds(timeOfReducedSpeed);
mSpeedNinja.runSpeed = maxS;
}
}
public class FrostHit:monobhavior
{
浮点最大值=40f;
浮子慢速=20f;
公共刚体2d rb;
整数伤害=-5;
降低速度的int时间=2;
void Start()
{
rb.velocity=transform.right*慢速度;
}
无效OnTiggerEnter2D(碰撞的R2D其他)
{
if(other.gameObject.tag==“Player\u Njinja”)
{
HealthNjinja HealthNjinja=other.gameObject.GetComponent();
healthNjinja.修改健康(损害);
PlayerMovementNjinja mspeednija=other.gameObject.GetComponent();
Start例程(speedTimeNinja());
}
else if(other.gameObject.tag==“玩家/骑士”)
{
HealthKnight HealthKnight=other.gameObject.GetComponent();
healthKnight.ModifyHealth(伤害);
PlayerMovementKnight mSpeedKnight=other.gameObject.GetComponent();
Start例程(speedTimeKnight());
}
//销毁子弹
摧毁(游戏对象);
}
IEnumerator speedTimeKnight()
{
mSpeedKnight.runSpeed=slowSpeed;
产生返回新WaitForSeconds(速度降低的时间);
mSpeedKnight.runSpeed=maxS;
}
IEnumerator speedTimeNinja()
{
mspeednija.runSpeed=slowSpeed;
产生返回新WaitForSeconds(速度降低的时间);
mspeednija.runSpeed=maxS;
}
}
我改变的事情和原因:
public class FrostHit : MonoBehaviour
{
float maxS = 40f;
float slowSpeed = 20f;
public Rigidbody2D rb;
int damage = -5;
int timeOfReducedSpeed = 2;
void Start()
{
rb.velocity = transform.right * slowSpeed;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player_Njinja")
{
HealthNjinja healthNjinja = other.gameObject.GetComponent<HealthNjinja>();
healthNjinja.ModifyHealth(damage);
PlayerMovementNjinja mSpeedNinja = other.gameObject.GetComponent<PlayerMovementNjinja>();
StartCoroutine(speedTimeNinja());
}
else if (other.gameObject.tag == "Player_Knight")
{
HealthKnight healthKnight = other.gameObject.GetComponent<HealthKnight>();
healthKnight.ModifyHealth(damage);
PlayerMovementKnight mSpeedKnight = other.gameObject.GetComponent<PlayerMovementKnight>();
StartCoroutine(speedTimeKnight());
}
// Destroy Bullet
Destroy(gameObject);
}
IEnumerator speedTimeKnight()
{
mSpeedKnight.runSpeed = slowSpeed;
yield return new WaitForSeconds(timeOfReducedSpeed);
mSpeedKnight.runSpeed = maxS;
}
IEnumerator speedTimeNinja()
{
mSpeedNinja.runSpeed = slowSpeed;
yield return new WaitForSeconds(timeOfReducedSpeed);
mSpeedNinja.runSpeed = maxS;
}
}
删除了bool和while循环,没有解决任何实际问题
内部分离的IEnumerator和2个函数OnTriggerEnter2D()
添加<代码>销毁(游戏对象)代码>到最后,因为子弹总是会被摧毁
移除