How to change the position of a Raycast (on Center of the Player 2D)?
public class PullAndPush : MonoBehaviour {
public float distance = 1f;
public LayerMask boxMask;
GameObject box;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Physics2D.queriesStartInColliders = false;
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, distance, boxMask);
if(hit.collider != null && hit.collider.gameObject.tag == "Grab" && Input.GetKey(KeyCode.X))
{
box = hit.collider.gameObject;
box.GetComponent<FixedJoint2D>().enabled = true;
box.GetComponent<FixedJoint2D>().connectedBody = this.GetComponent<Rigidbody2D>();
}else if (Input.GetKeyUp(KeyCode.X))
{
box.GetComponent<FixedJoint2D>().enabled = false;
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position,(Vector2)transform.position + Vector2.right * transform.localScale.x * distance);
}
}
///// @Lockstep , @Nymisu, @crazyknight , @clunk47 , @urooba , @Menyus777 , @NoBrainer-David, @MaxGuernseyIII ,
Comment
Your answer
Follow this Question
Related Questions
Is there any way how to do raycasts like this? 0 Answers
Strange Raycast problem 0 Answers
2D Raycast reflection only changes hit.normal 0 Answers
One 2D Raycast Not Stopping Even Though Others Do? 0 Answers
Drawline for a longer distance 1 Answer