Disable input while falling?
I have a game where you get points by each input (mouse left click) you made to stay on a platform. I want to prevent the player from spamming the input when he is already not colliding with the platform.
The platform do fall and regenerate as the player gets forward. Here's how it is:
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
Invoke("Fall", 0.2f);
}
}
void Fall()
{
GetComponent<Rigidbody>().isKinematic = false;
Destroy(gameObject, 1f);
}
So my plan is to do the same exact thing (OnCollisionExit) but for the "Platform" tag. The issue is that I have no clue how to disable the input completely. Can anyone help me with that? Thank you a lot! :)
Answer by Pathojen · Dec 06, 2021 at 11:31 PM
I would set up a boolean and check if boolean is true to perform the action after a mouse press.
On Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootOnClick : MonoBehaviour
{
public static bool onGround;
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.CompareTag("Platform"))
{
onGround = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Platform"))
{
onGround = false;
}
}
}
On Target
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static ShootOnClick;
public class ShotOnClick : MonoBehaviour
{
private void OnMouseDown()
{
if (ShootOnClick.onGround == true)
{
Destroy(this.gameObject);
}
}
}
In case you or another person reading this doesn't notice, not that the target script says using static ShootOnClick
. Without this line in the second script, or setting the bool onGround to public static in the first one, this would not have worked. Now, for movement, you could put transform.Translate in an if statement to check if onGround is true.
I get what you've done and I also had an idea to set up a boolean but I also need to completely disable the input. I implemented this code but the player is still able to get some points by clicking in mid air. Do you know how to do that? These are all the player movement related codes:
void Move()
{
currentSpeed = Mathf.SmoothStep($$anonymous$$Speed, maxSpeed, time / accelerationTime);
transform.position += transform.forward * currentSpeed * Time.deltaTime;
time += Time.deltaTime;
}
void CheckInput()
{
//if first input then ignore
if (firstInput)
{
firstInput = false;
return;
}
if (Input.GetMouseButtonDown(0))
{
ChangeDirection();
}
}
void ChangeDirection()
{
if (movingLeft)
{
movingLeft = false;
transform.rotation = Quaternion.Euler(0, 90, 0);
}
else
{
movingLeft = true;
transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
I am sorry if I am a bit disturbing but I tried a bunch of things and nothing seemed to work :(
Try this. I just edited the code. It might work a little better.
Answer by imbruceter · Dec 07, 2021 at 05:07 AM
Yes, you made it! Sorry for being stupid, this movement blocking was new to me. I also blocked the player from increasing the score while mid air. Thank you a lot for your time and help!
Glad it worked! And believe me when I say that learning something new and screwing up is never stupid, it's just part of the process. Happy coding.
Oh, no! I hope you still here. Sorry for disturbing you again, but there is a new bug, which is caused by this code. Some platforms disappear right after or under me, before or meanwhile they are falling. Sometimes platforms even disappear before me (wasn't letting me insert the pic so I upoaded it:
https://pasteboard.co/sWO9YcuN5ij6.png
Why is that? What to do with that? I tried to delay the fall and the destroying but doesn't seem to work. Also set the Rigidbody.isKinematic to false in ShotOnClick, but still no improvement.
I'm here! And I'm guessing it's because you're destroying anything on click, rather than anything that is not tagged platform. Try placing the destroy function in an if statement.