我希望make状态在跳转时不能使用Space,但是当我make时,冲突只会返回True。我尝试使用RayCast进行测试,即使检测到null仍返回true。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
public float speed;
public float jump_height;
Rigidbody2D PlayerRig;
public GameObject Spawner;
// Use this for initialization
void Start()
{
}
private void Awake()
{
PlayerRig = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float Move = Input.GetAxis("Horizontal") * speed;
gameObject.transform.Translate(new Vector3(Move, 0, 0) * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space) && isTouching() == true)
{
Jump();
}
}
void Jump()
{
PlayerRig.AddForce(Vector2.up * jump_height);
}
bool isTouching()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 0.1f);
if (hit)
{
return true;
}
else
return false;
}
}
由于CollisionEnter / Stay也无法正常工作,仍然只返回true