问题描述
我试图让角色在空中时停止跳跃,但问题是它在与墙壁碰撞时可以跳跃.
I'm trying to make the character stop jumping when in the air, but the problem is that it can jump when colliding with walls.
所以我试图让角色在下面的脚本中再次跳跃之前经过一定的时间,但由于某种原因它不起作用.
So i tried to make a certain amount of time pass by before the character could jump again in the script below, but for some reason it doesn't work.
如果有人有任何更有效的方法来做到这一点,我肯定会很感激.
If anyone has any more efficient ways of doing this i would definitely appreciate it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float UpSpeed;
public Rigidbody rb;
public float horizonSpeed;
public bool isGrounded;
public float gravInc = 50;
public int counter;
public float DownSpeed = 50;
public int jumpCount = 0;
public float secCount = 0;
public bool validUp = true;
public float sec;
public void OnCollisionStay(Collision collision)
{
isGrounded = true;
}
public void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
public void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (secCount == (sec))
{
validUp = true;
}
sec = Time.time;
if (Input.GetKeyDown ("up") && isGrounded == (true) && validUp == (true))
{
rb.AddForce(transform.up * UpSpeed);
secCount = 0;
validUp = false;
secCount = sec + 3;
}
else if(Input.GetKeyDown ("right"))
{
horizonSpeed = 200;
rb.AddForce(transform.right * horizonSpeed);
}
else if (Input.GetKeyDown ("left"))
{
horizonSpeed = -200;
rb.AddForce(transform.right * horizonSpeed);
}
}
}
推荐答案
你必须制作一个不同的碰撞器,放在你的播放器的脚上(也更小).让它成为触发器.
You have to make a different collider that sits on the feet of your player (also smaller). Make it a trigger.
并使用 OnTriggerStay 将其 isGrounded 变量设置为 true,并使用 OnTriggerExit 将其设置为 false.
And use OnTriggerStay to set its isGrounded variable to true, and OnTriggerExit to set it to false.
void OnTriggerStay(Collider other){
isGrounded = true;
}
void OnTriggerExit(Collider other){
isGrounded = false;
}
这篇关于如何阻止角色双跳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!