Drag and Shoot

Hi, can someone help me with a script. Below is the code that I have for my player. It is a simple drag and shoot script, but I also wanted to make it so that way I can only drag and shoot whenever the player is standing on a pillar. Currently I can drag and shoot him wherever he is. I am pretty new to C# so any help would be wonderful.

public class PlayerMov : MonoBehaviour
{
    public float power = 10f;
    public Rigidbody2D rb;

    public Vector2 minPower;
    public Vector2 maxPower;

    LineTraj tl;

    Camera cam;
    Vector2 force;
    Vector3 startPoint;
    Vector3 endPoint;

    private void Start ()
    {
        cam = Camera.main;
        tl = GetComponent<LineTraj>();
    }

    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);
            currentPoint.z = 15;
            tl.RenderLine(startPoint, currentPoint);
        }

        if (Input.GetMouseButtonUp(0))
        {
            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();
        }
    }
}

Here is an image of the scene:

Thanks

You could use a number of ways to determine this:

  • detect player grounded

  • detect player within trigger area on top of pillar

  • detect location relative to pillar

Check out some Youtube tutorials for whatever game you’ve seen that does what you’re thinking of doing, see how they do it. It’s NEVER just code: colliders and / or trigger areas will need to be set up and connected properly.

But once they are, it would just be some type of inhibiting boolean in the script above that checks to ensure the condition is met before allowing your drag-and-shoot.

Alright Thanks!