- Home /
Linecast, rotate end point relative to objects rotation
Hi,
I am using linecast to check if my enemy is grounded (2d) and it works when enemy doesnt rotate:
When enemy rotates I would like Linecast to use same rotation, but I cannot seem to find solution, thats what i am trying to achieve, relative rotation:
Here`s my code:
Vector3 groundedStart = transform.position + new Vector3(0.5f * direction, 0, 0);
Vector3 groundedEnd = transform.position + transform.rotation * new Vector3(0.5F * direction, -0.2F, 0);
Debug.DrawLine(blockedStart, blockedEnd);
Debug.DrawLine(groundedStart, groundedEnd);
bool isGrounded = Physics2D.Linecast(groundedStart, groundedEnd);
Would be grateful for some tips how to achieve it, thank you.
Answer by merkaba48 · Jul 04, 2017 at 06:55 PM
You can use "transform.right", "transform.down", etc to get the local cardinal directions of the object. Then it's simply,
Vector3 groundedStart = transform.position + transform.right * 0.5f;
Vector3 groundedEnd = transform.position + transform.right * 0.5f + transform.down * 0.2f;
NB. I'm not sure what role direction
plays in your code, so you may need to modify my answer to suit your needs. But the good thing to know is that Vector3.down is down in world space, and transform.down is down in local space.
Also I'm not sure why your rotation * vector3 solution didn't work, but I've not much experience multiplying quaternions with vectors.
Works well, thank you, "direction" was either -1 or 1 (left or right movement of the enemy) so I am only multiplying transform.right by direction and it all works, thank you.