1
對不起,如果標題混亂。基本上,我想要做的是確保玩家接地(這樣他們可以再次跳躍),問題是在我的遊戲中會出現玩家可以着陸的凸形。Unity2D儘量以凸起形狀跳躍
我現在這樣做的方式是使用光線投射,但由於單個光線投射只能來自中心的某個位置,所以我這樣做是爲了讓光線投射沿着球員的底部進行(取而代之)如果一半以上的身體離開平臺,玩家將無法跳躍)。
然而沿着底部的光線投射使得它如此,如果我試圖在像斜坡這樣的形狀上跳躍時,它會使我變得難以置信的高,這是我不想要的。
任何人都可以幫我解決這個問題嗎?
代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour {
public Rigidbody2D rBody;
[Range(1, 10)]
public int Speed;
private float lastDistance;
public bool isGrounded = true;
private LayerMask environment;
void Start() {
environment = LayerMask.GetMask("Environment");
}
void FixedUpdate() {
// Check if the user is attempting to jump
if (Input.GetKey(KeyCode.A)) {
rBody.velocity = new Vector2(-Speed, rBody.velocity.y);
}
if (Input.GetKey(KeyCode.D)) {
rBody.velocity = new Vector2(Speed, rBody.velocity.y);
}
if (Input.GetKey(KeyCode.Space)) {
RaycastHit2D hit2D = Physics2D.Raycast(rBody.position+new Vector2(0,-1.5f), Vector2.right,1f,environment);
if (hit2D) {
if (hit2D.distance < lastDistance) {
lastDistance = hit2D.distance;
}
else {
lastDistance = 100f;
rBody.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
}
}
}
}
}