C# 触地即跳
点击左键后,如果与“地面”层碰撞的条件为真,玩家应跳起 问题:当我点击它时,它没有跳转 层次结构(3)C# 触地即跳,c#,unity3d,C#,Unity3d,点击左键后,如果与“地面”层碰撞的条件为真,玩家应跳起 问题:当我点击它时,它没有跳转 层次结构(3) 主摄像机 玩家 地板 检查员信息: 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类玩家控制器:单行为 { 公共浮力=25f; 私有刚体2d刚体; 无效唤醒() { 刚体=GetComponent(); } 无效更新() { if(Input.GetMouseButtonDown(0)){ 跳跃(); } } 无效跳转(){
- 主摄像机
- 玩家
- 地板
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家控制器:单行为
{
公共浮力=25f;
私有刚体2d刚体;
无效唤醒()
{
刚体=GetComponent();
}
无效更新()
{
if(Input.GetMouseButtonDown(0)){
跳跃();
}
}
无效跳转(){
如果(isground()){
刚体.附加力(矢量2.up*跳跃力,力模2D.脉冲);
}
}
公共层屏蔽底层;
bool-IsGrounded()
{
if(Physics2D.Raycast(this.transform.position,Vector2.down,0.2f,groundLayer.value)){
返回true;
}
否则{
返回false;
}
}
}
我看不出你的播放器有多大,但请确保光线投射器实际上可以到达地面,因为它只有0.2米长。可以使用Debug.DrawLine()
在编辑器中查看光线投射
还要确保“跳跃力”值足够高。是的,光线投射未到达地面,因此无法应用条件语句。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float jumpForce = 25f;
private Rigidbody2D rigidBody;
void Awake()
{
rigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetMouseButtonDown(0)) {
Jump();
}
}
void Jump(){
if (IsGrounded()) {
rigidBody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
public LayerMask groundLayer;
bool IsGrounded()
{
if (Physics2D.Raycast(this.transform.position, Vector2.down, 0.2f, groundLayer.value)) {
return true;
}
else {
return false;
}
}
}