Unity3d Unity2D未正确检查状况
我在这么短的时间里问了这么多关于团结的问题,我真的很感谢所有回答的人。我不知道为什么我总是面对这样的问题,我在网上看到的任何答案都不能帮我解决。好吧,这是我的问题:Unity3d Unity2D未正确检查状况,unity3d,2d,Unity3d,2d,我在这么短的时间里问了这么多关于团结的问题,我真的很感谢所有回答的人。我不知道为什么我总是面对这样的问题,我在网上看到的任何答案都不能帮我解决。好吧,这是我的问题: if (Input.GetKey(KeyCode.Q) && (rightHit.collider != null || leftHit.collider != null) && groundHit.collider == null) 如果我按下任何墙上的Q按钮,更新函数中的这行代码会检查每一帧。如
if (Input.GetKey(KeyCode.Q) && (rightHit.collider != null || leftHit.collider != null) && groundHit.collider == null)
如果我按下任何墙上的Q按钮,更新函数中的这行代码会检查每一帧。如果条件满足,则
void Grapple()
{
if (wallGrapple)
{
x = 0f;
rb.gravityScale = 0;
rb.velocity = new Vector2(speed * x, speed * y);
if (Input.GetKeyDown(KeyCode.Space)) { jumpRequest = true; }
}
}
此函数被调用,允许玩家根据垂直轴输入(输入y变量)在墙上粘贴和滑动。现在的问题是,如果我只在墙上按一次Q键,那么它会留在墙上,即使它应该被按住以便调用Grapple()[我使用了Input.GetKey而不是Input.GetKeyDown],而且我也可以在墙上滑动(就像有一些看不见的墙),即使在if行中,正在检查检测墙的条件。
我已经做了大约30-45分钟,我无法破解它。我想实现的另一个解决方法是使用协同程序,但我不知道如何使用,因为我还没有找到任何关于它如何工作以及应该如何使用的基本解释。这是完整的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms;
public class Move : MonoBehaviour
{
[SerializeField] private LayerMask groundMask;
[SerializeField] private LayerMask Wall;
[Header("Horizontal")]
public float x;
float dirX;
public float speed;
float initialSpeed;
[SerializeField] [Range(0, 1)] float LerpConstant;
public float dashForce = 100f;
public bool canDash;
public bool dashRequest;
[Header("Vertical")]
public float y;
public bool canJump;
public bool jumpRequest;
public float initialJumpForce;
public float jumpForce;
public bool canWallJump;
public float modifiedJumpForce;
public float sideJumpForce;
public bool wallGrapple;
[Header("Other")]
public Rigidbody2D rb;
public float gScale = 10f;
BoxCollider2D boxcollider;
RaycastHit2D groundHit;
RaycastHit2D leftHit;
RaycastHit2D rightHit;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
boxcollider = gameObject.GetComponent<BoxCollider2D>();
jumpForce = initialJumpForce;
initialSpeed = speed;
rb.gravityScale = gScale;
}
// Update is called once per frame
void Update()
{
//Instant 1, 0, -1 inputs
x = Input.GetAxisRaw("Horizontal");
y = Input.GetAxisRaw("Vertical");
if(x < 0)
{ dirX = -1; }
else if(x > 0)
{ dirX = 1; }
if (Input.GetKeyDown(KeyCode.Space) && (canJump || canWallJump)) //for jumps
{ jumpRequest = true; }
if (Input.GetKey(KeyCode.Q) && (rightHit.collider != null || leftHit.collider != null) && groundHit.collider == null) //for wall grappling
{ wallGrapple = true; }
if (Input.GetKeyDown(KeyCode.LeftShift) && (canDash)) //for dashing
{ dashRequest = true; }
Detection();
JumpDash();
Grapple();
}
void FixedUpdate()
{
Movement();
}
void Detection()
{
groundHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.down, 0.1f, groundMask);
if (groundHit.collider != null) //If player is on ground
{
canJump = true;
canWallJump = false;
canDash = false;
jumpForce = initialJumpForce;
sideJumpForce = 0;
LerpConstant = 0.25f;
}
else if (groundHit.collider == null)//not on ground
{
LerpConstant = 0.12f;
canJump = false;
}
//Wall detection
//Left wall
leftHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.left, 0.1f, Wall);
if (leftHit.collider != null && groundHit.collider == null) //if player on left wall and not on ground
{
canWallJump = true;
jumpForce = modifiedJumpForce;
sideJumpForce = jumpForce;
}
//Right wall
rightHit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.right, 0.1f, Wall);
if (rightHit.collider != null && groundHit.collider == null) //if player on right wall and not on ground
{
canWallJump = true;
jumpForce = modifiedJumpForce;
sideJumpForce = -jumpForce; // negative of jump force to jump in left direction
}
if (rightHit.collider == null && leftHit.collider == null) //if both walls are not detected
{
canWallJump = false;
}
}
void Movement()
{
Vector2 move = new Vector2(x * speed, rb.velocity.y);
rb.velocity = Vector2.Lerp(rb.velocity, move, LerpConstant);
}
void JumpDash()
{
//Jump
if (jumpRequest)
{
wallGrapple = false;
rb.gravityScale = gScale;
rb.AddForce(new Vector2(sideJumpForce * 2.1f, jumpForce), ForceMode2D.Impulse);
speed = initialSpeed;
jumpRequest = false; //setting jumpRequest to false after jumping to prevent unlimited jumps
canDash = true;
}
/*if (dashRequest)
{
rb.velocity = Vector2.zero;
rb.AddForce((dashForce, 0f), ForceMode2D.Impulse);
}*/ //remember to put this code in later
}
void Grapple()
{
if (wallGrapple)
{
x = 0f;
rb.gravityScale = 0;
rb.velocity = new Vector2(speed * x, speed * y);
if (Input.GetKeyDown(KeyCode.Space)) { jumpRequest = true; }
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SocialPlatform;
公共阶级运动:单一行为
{
[序列化字段]专用层掩码接地掩码;
[序列化字段]私有层掩码墙;
[标题(“水平”)]
公共浮动x;
浮动dirX;
公众浮标速度;
浮动初始速度;
[SerializeField][Range(0,1)]浮动LerpConstant;
公共浮力=100f;
公共布尔坎达什;
公共布尔请求;
[标题(“垂直”)]
公众浮躁;
公共场所可以跳跃;
公共布尔请求;
公众力量;
公共安全部队;
公共场所;
公众浮力;
公众力量;
公共墙格斗;
[标题(“其他”)]
公共刚体2d rb;
公共浮标gScale=10f;
BoxCollider2D boxcollider;
雷卡斯特2D地面打击;
雷卡斯特2D左击;
雷卡斯特2D右击;
//在第一帧更新之前调用Start
void Start()
{
rb=gameObject.GetComponent();
boxcollider=gameObject.GetComponent();
跳跃力=初始跳跃力;
初始速度=速度;
rb.gravityScale=gScale;
}
//每帧调用一次更新
无效更新()
{
//瞬间1,0,-1输入
x=Input.GetAxisRaw(“水平”);
y=输入。GetAxisRaw(“垂直”);
if(x<0)
{dirX=-1;}
如果(x>0),则为else
{dirX=1;}
if(Input.GetKeyDown(KeyCode.Space)和&(canJump | | canWallJump))//用于跳转
{jumpRequest=true;}
如果(Input.GetKey(KeyCode.Q)&&&(righhit.collider!=null | | leftHit.collider!=null)&&groundHit.collider==null)//用于墙壁抓斗
{wallGrapple=true;}
if(Input.GetKeyDown(KeyCode.LeftShift)&&(canDash))//用于破折号
{dashRequest=true;}
检测();
JumpDash();
抓斗();
}
void FixedUpdate()
{
运动();
}
空隙检测()
{
groundHit=Physics2D.BoxCast(boxcollider.bounds.center,boxcollider.bounds.size,0f,矢量2.down,0.1f,groundMask);
if(groundHit.collider!=null)//如果玩家在地面上
{
canJump=true;
canWallJump=错误;
坎达斯=假;
跳跃力=初始跳跃力;
侧向力=0;
LerpConstant=0.25f;
}
否则如果(groundHit.collider==null)//不在地面上
{
LerpConstant=0.12f;
canJump=false;
}
//壁检测
//左墙
leftHit=Physics2D.BoxCast(boxcollider.bounds.center,boxcollider.bounds.size,0f,向量2.left,0.1f,墙);
if(leftHit.collider!=null&&groundHit.collider==null)//如果玩家在左墙上而不是地面上
{
canWallJump=true;
跳跃力=修改后的跳跃力;
侧向跳跃力=跳跃力;
}
//右墙
rightHit=Physics2D.BoxCast(boxcollider.bounds.center,boxcollider.bounds.size,0f,矢量2.right,0.1f,墙);
if(righhit.collider!=null&&groundHit.collider==null)//如果玩家在右墙上而不是地面上
{
canWallJump=true;
跳跃力=修改后的跳跃力;
sideJumpForce=-jumpForce;//向左跳跃的跳跃力的负值
}
if(righhit.collider==null&&leftHit.collider==null)//如果未检测到两个墙
{
canWallJump=错误;
}
}
空位移动()
{
向量2移动=新向量2(x*速度,rb.速度.y);
rb.velocity=Vector2.Lerp(rb.velocity,move,LerpConstant);
}
void JumpDash()
{
//跳跃
如果(跳转请求)
{
墙格斗=假;
rb.gravityScale=gScale;
rb.附加力(新矢量2(侧跳力*2.1f,跳力),力模式2D.脉冲);
速度=初始速度;
jumpRequest=false;//在跳转后将jumpRequest设置为false以防止无限跳转
坎达斯=真;
}
/*如果(请求)
{
rb.速度=矢量2.0;
rb.附加力((冲击力,0f),力模式2D.脉冲);
}*///记得以后把这段代码放进去
}
空穴抓斗
{
如果(墙抓斗)
{
x=0f;
rb.重力标度=0;
rb.velocity=新矢量2(速度*x,速度*y);
if(Input.GetKeyDown(KeyCode.Space)){jumpRequest=true;}
}
}
}
p.S.I.w
void Start()
{
StartCoroutine("coroutineFunc"); // calling for example
}
IEnumerator coroutineFunc()
{
// before waiting... if u wanna try something
yield return new WaitForSeconds(1); // waiting..
// after waiting something what do you want
}