- Home /
Question by
ColdComfort · Feb 28, 2014 at 06:13 PM ·
raycastrotate
Rotate a Raycaster Around an Object
Hi guys,
I'm making a top-down 2D asteroids game, and I'm trying to rotate a raycaster around a central object. The raycaster destroys the object it hits, so I need to have it offset from the center of the player object. (Like a laser, mounted on the front of a ship.) However, I'm having a lot of trouble figuring out how to set up a rotating local offset for it. As it is now, it's offset on the global axis, which is great until you rotate at all.
void Update ()
{
Vector2 emitter = new Vector2();
emitter.Set(transform.position.x, transform.position.y+4);
RaycastHit2D hit = Physics2D.Raycast (emitter, transform.up);
if (Input.GetKey (KeyCode.Space) & hit != null)
{
Destroy (hit.collider.gameObject);
}
}
Comment
Best Answer
Answer by robertbu · Feb 28, 2014 at 06:15 PM
You can do it this way:
Vector2 emitter = transform.position + transform.up * 4.0f;
'transform.up' will be the local up vector of your object translated into world coordinates.
Awesome, works like a charm. Thanks for the explanation too; that makes a lot of sense. Appreciate your taking the time to answer!