Hi, I currently have a script set up for a drag and shoot ability, although I only want to be able to drag and shoot whenever the player is standing on a pillar, so far I got it to work so that way the player has to be touching a pillar to be able to drag and shoot, but I can still drag and shoot whenever the player is touching the side of a pillar. I have implemented a RayCast to detect a pillar, but I don’t know how to make it so that way I can drag and shoot only when the player is standing on a pillar.
public class PlayerMov : MonoBehaviour
{
public float power = 10f;
public Rigidbody2D rb;
public BoxCollider2D bc;
private bool isGrounded = false;
private Vector3 horizontalMov;
private float rotationalSpeed = 20f;
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>();
isGrounded = false;
}
private void Update()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position + new Vector3(.49f, 0f, .5f), transform.TransformDirection(Vector2.down), 10f);
Debug.DrawRay(transform.position + new Vector3(.49f, 0f, .5f), transform.TransformDirection(Vector2.down) * 10f, Color.red);
RaycastHit2D hit1 = Physics2D.Raycast(transform.position + new Vector3(-.48f, 0f, -.48f), transform.TransformDirection(Vector2.down), 10f);
Debug.DrawRay(transform.position + new Vector3(-.48f, 0f, -.48f), transform.TransformDirection(Vector2.down) * 10f, Color.red);
if (hit.collider.tag.Equals("Ground"))
{
Debug.Log("Hit: " + hit.collider.name);
}
else if (hit1.collider.tag.Equals("Ground"))
{
Debug.Log("Hit1: " + hit1.collider.name);
}
if (isGrounded == true)
{
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();
}
}
horizontalMov = new Vector3(0f, 0f, -Input.GetAxis("Horizontal"));
transform.Rotate(horizontalMov * rotationalSpeed * Time.deltaTime);
}
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D other)
{
if(other.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}
Here is an image: