Я работаю над мини-2D-игрой (Unity), в которой есть объекты, падающие с неба, в какой-то момент мне нужно, чтобы мой игрок встал на них, поскольку они продолжают падать на землю с постоянной скоростью, и таким образом, они не могут быть RigidBody.
Проблема в том, что мой игрок продолжает подпрыгивать на них, когда они падают, никогда не приземляясь.
Примечание: и игрок, и объект у меня есть физический материал с нулевой упругостью, и обратите внимание, что я по-прежнему хочу, чтобы у моего игрока была гравитация и все, что есть у RigidBody.
Я не смог найти решение в обсуждениях Unity, я надеюсь, что, возможно, я смогу как-нибудь найти решение здесь.
Заранее спасибо!
Вот скрипты, которые я прикрепил к плееру и к объекту:
Чтобы мой плеер:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class PlayerScript : MonoBehaviour
{
GameObject Rany;
[SerializeField] FloatingGroundSpawnerBehaviour _floatingGroundSpawnerBehaviour;
[SerializeField] Rigidbody2D rb;
[SerializeField] float maxVel = 15;
[SerializeField] float walk;
[SerializeField] float jump;
bool isGrounded = false;
bool isAlive = true;
float horInput = 0;
public LogicScript logic;
// Start is called before the first frame update
void Start()
{
if (rb == null) {
rb = gameObject.GetComponent();
}
logic = GameObject.FindGameObjectWithTag("Logic").GetComponent();
}
// Update is called once per frame
void Update()
{
if (isAlive) { move(); }
}
void move()
{
horInput = Input.GetAxisRaw("Horizontal");
if (horInput != 0)
{
LimitedAddForce(horInput *= walk);
// rb.AddForce(new Vector2(horInput *= walk, 0f), ForceMode2D.Force);
}
if (isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space) /*|| Input.GetKeyUp("w") || Input.GetKeyUp("j") || Input.GetKeyUp("k") || Input.GetKeyUp("l")*/)
rb.AddForce(new Vector2(0f, jump), ForceMode2D.Force);
}
}
void LimitedAddForce( float force)
{
float currentVel = rb.velocity.x;
if (currentVel + force = -maxVel) rb.AddForce(new Vector2(force, 0f), ForceMode2D.Force);
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "FloatingGround")
{
isGrounded = true;
}
else if(other.gameObject.tag == "DeathGround")
{
isAlive = false;
logic.gameOver();
Debug.Log("GAMEEEEEE OVERRRRRR!!!");
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.tag == "FloatingGround")
{
isGrounded = false;
}
}
}
К моему объекту:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundMoveBehaviour : MonoBehaviour
{
[SerializeField] public float dropVel;
[SerializeField] public float deadZone;
// Update is called once per frame
void Update()
{
transform.position -= new Vector3(0f, dropVel * Time.deltaTime, 0f);
if(transform.position.y < deadZone)
{
Destroy(gameObject);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... in-unity2d