Hi Guys, watched a lot of tutorials, but they are all “closed” in itself and are about platformers, and I just can’t translate it the following code. Everything is working pretty fine, but I fail to implement that the isGrounded becomes “false” again after the player object hits the ground again.
What the code does is pretty simple, you can drag and drop the mouse and shoot the player away. Then, you can’t do it again as isGrounded goes “true” (I know it should be the other way round but well it works) - so I need it to become “false”. And this is where I fail. I thought about if…player collides with something that is tagged as ground, but eh.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragNShoot : MonoBehaviour
{
public float power = 10f;
public Rigidbody2D rb;
public Vector2 maxPower;
public Vector2 minPower;
TrajectoryLine tl;
Camera cam;
Vector2 force;
Vector3 startPoint;
Vector3 endPoint;
public bool isGrounded;
private void Start()
{
cam = Camera.main;
tl = GetComponent<TrajectoryLine>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
}
if (Input.GetMouseButton(0))
{
Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
tl.RenderLine(startPoint, currentPoint);
}
if (Input.GetMouseButtonUp(0) && isGrounded == false)
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
endPoint.z = 15;
force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
rb.AddForce(force * power, ForceMode2D.Impulse);
tl.EndLine();
isGrounded = true;
}
GO FALSE AGAIN ARGH == FALSE WHYNOT ;-)
}
}
I cannot help you, since your ‘isGrounded’ seemingly is not supposed to contain what it says. Currently you are “isGrounded” whenever you are not isGrounded and release the left mouse button. Based on that, i actually have no idea whatsoever, in what state it should become false. It’s arbitrary.
I imagine you want to be able to shoot the player while he is grounded, and not while in the air? Why would you intentionally just flip the true and false state of that? It makes no sense.
Anyways, while the player is flying… or i guess in your case… “isGrounded”, cast a ray downwards and get the hit location. Calculate the distance between the hit and your player position, and if it’s below some treshhold, you are grounded - or excuse me, not isGrounded (lol).
Or, since you use a Rigidbody, you could check the velocity, and assume you are grounded when the velocity.y is approximately 0, or even if the magnitude of the velocity vector is 0 (or approximately), in case you want it to stand still before launching again.
The “ground” tag with a collision is simplest, but it also means you can collide with the side of it (like a platform) and it will “ground” the player. Raycasting is the most accurate.
Hey Guys, I’ve followed what I have found and for the little game I am trying to make, it seemed like the easiest way. However, my isGrounded (yes, it really is isGrounded now haha) is not working.
The Player object has the script attached
The ground has a layer called groundLayer on it
But it does not check isGrounded, so I cant jumpt at all
Any idea where the mistake is?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragNShoot : MonoBehaviour
{
public float power = 10f;
public Rigidbody2D rb;
public Vector2 maxPower;
public Vector2 minPower;
TrajectoryLine tl;
Camera cam;
Vector2 force;
Vector3 startPoint;
Vector3 endPoint;
public bool isGrounded;
public LayerMask groundLayers;
private void Start()
{
cam = Camera.main;
tl = GetComponent<TrajectoryLine>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
}
if (Input.GetMouseButton(0))
{
Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
tl.RenderLine(startPoint, currentPoint);
}
isGrounded = Physics2D.OverlapArea(new Vector2(transform.position.x - 0.5f, transform.position.y - 0.5f),
new Vector2(transform.position.x + 0.5f, transform.position.y - 0.5f), groundLayers);
if (Input.GetMouseButtonUp(0) && isGrounded)
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
endPoint.z = 15;
force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
rb.AddForce(force * power, ForceMode2D.Impulse);
tl.EndLine();
}
}
}
If you haven’t already, try adding
print("IsGrounded = " + isGrounded);
right after you set isGrounded, then see if you can get it to ever change by fiddling with the parameters in the Inspector.
This ^ ^ ^ is definitely the first place to start. Building upon that, I recommend liberally sprinkling Debug.Log() (or print()) statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.