raycast in direction of movement key down

Hey all,

I’m working on the fundamentals of a Third Person Cover Shooter similar to GoW. Currently my script shoots two raycasts 5m in front of the character (one for low cover, the other for high cover) in order to detect the height of the cover in front. This works like a charm.

However, moving forward i would like to rewrite this essentially so the raycasts shoot 5m out in the direction of the movement keys. For example, if the player is holding “W” and “D”, the raycast would point in a 45 degree angle to the right. Therefore players could have greater control over where they would like to snap to cover.

Any insight would be greatly appreciated!

Perhaps using transform.forward instead of transform.TransformDirection(Vector3.forward) would help.

Update:

So I was able to work out what I was trying to achieve by writing a more complex version of my CoverDetect Script.


Here’s what I did:

void Update () {

        var forward = transform.forward;
        var backward = transform.forward * -1;
        var right = transform.right;
        var left = transform.right * -1;

        var forwardleft = transform.right * -1 + transform.forward;
        var backright = transform.forward * -1 + transform.right;
        var backleft = transform.right * -1 + transform.forward * -1;
        var forwardright = transform.right + transform.forward;


        var x = Input.GetAxisRaw("Horizontal");
        var y = Input.GetAxisRaw("Vertical");

Then in order to get the raycast pointing in the direction the player was pressing input I wrote the following:
_
if (x < 0 && y > 0)
{
Debug.DrawRay(HighCoverOrigin.transform.position, forwardleft * 5, Color.green);
Debug.DrawRay(LowCoverOrigin.transform.position, forwardleft * 5, Color.green);
}

        if (x < 0 && y == 0)
        {
            Debug.DrawRay(HighCoverOrigin.transform.position, left * 5, Color.green);
            Debug.DrawRay(LowCoverOrigin.transform.position, left * 5, Color.green);
        }


        if (x > 0 && y > 0)
        {
            Debug.DrawRay(HighCoverOrigin.transform.position, forwardright * 5, Color.green);
            Debug.DrawRay(LowCoverOrigin.transform.position, forwardright * 5, Color.green);
        }

        else if (x > 0 && y < 0)
        {
            Debug.DrawRay(HighCoverOrigin.transform.position, backright * 5, Color.green);
            Debug.DrawRay(LowCoverOrigin.transform.position, backright * 5, Color.green);
        }


        else if (x == 0 && y < 0)
        {
            Debug.DrawRay(HighCoverOrigin.transform.position, backward * 5, Color.green);
            Debug.DrawRay(LowCoverOrigin.transform.position, backward * 5, Color.green);
        }
        else if (x == 0 && y > 0)
        {
            Debug.DrawRay(HighCoverOrigin.transform.position, forward * 5, Color.green);
            Debug.DrawRay(LowCoverOrigin.transform.position, forward * 5, Color.green);
        }

        else   if (x < 0 && y < 0)
        {
            Debug.DrawRay(HighCoverOrigin.transform.position, backleft * 5, Color.green);
            Debug.DrawRay(LowCoverOrigin.transform.position, backleft * 5, Color.green);
        }

        else if (x > 0 && y == 0)
        {
            Debug.DrawRay(HighCoverOrigin.transform.position, right * 5, Color.green);
            Debug.DrawRay(LowCoverOrigin.transform.position, right * 5, Color.green);
        }

_
Your code seems to be a better version of the monstrosity i developed but hey it’s all apart of the learning process i suppose!

Feel free to comment on my code and things I could do to improve, I wont get offended.

Thank you @LCStark