0
\$\begingroup\$

I have been trying for the past 5 hours to get this to work.

So i have an enemy that has the following script:

        private bool Look(StateController controller)
    {
        Debug.DrawRay(controller.transform.TransformPoint(controller.mCombatant._CombatOffset),  controller.transform.TransformDirection(Vector3.forward) * 800,
            Color.green);
        RaycastHit hit;

        if (Physics.SphereCast(controller.transform.TransformPoint(controller.mCombatant._CombatOffset), 360,
                controller.transform.TransformDirection(Vector3.forward), out hit, 800)
            && hit.collider.CompareTag("Player"))
        {
            controller.mCombatant.Target = hit.transform;
            controller.chaseTarget = hit.transform;
            return true;
        }
        else
        {
            return false;
        }


        return false;
    }

Now both my enemy (the one this script is attached to) and my player are on the default layer

Here are some images that shows the gameview:

enter image description here

As you can see from the above picture The "Ray" should hit the player:

My Player prefab looks like this:

enter image description here

So my question is what have i done wrong?

EDIT

So after alot of time i managed to make the following code work (however its a normal raycast :( )

    RaycastHit hit;

    if (Physics.Raycast(controller.transform.position, controller.transform.forward, out hit) &&
        hit.collider.CompareTag("Player"))
    {
        var g = 0;
    }

This is good but not good enough since i want the vision of my enemy to be more than a straight line :(

\$\endgroup\$
2
  • \$\begingroup\$ I notice you're using a sphere radius of 360. What led you to choose that number? Can you drop a sphere collider with radius 360 into your scene to see how it compares against the scale of the other objects? \$\endgroup\$
    – DMGregory
    Oct 31, 2017 at 19:51
  • \$\begingroup\$ @DMGregory To be honest nothing made me choose that number it is from several hours of attempts \$\endgroup\$ Oct 31, 2017 at 20:00

1 Answer 1

1
\$\begingroup\$

I think a spherecast radius of 360 is much too big.

Quickly eyeballing your other colliders, it looks like your characters are about 0.4 units in radius themselves, and only a few units apart.

That means when the physics sphere starts its cast, it's already encompassing all of the colliders nearby. Since spherecasts and raycasts detect only stuff they hit as they travel, these already-embedded colliders get ignored.

Try reducing your sphere radius to be more on par with the sizes of your characters & scale of their interactions.

\$\endgroup\$
3
  • \$\begingroup\$ what do you suggest i put in instead then il test those values :) \$\endgroup\$ Oct 31, 2017 at 21:07
  • \$\begingroup\$ "Try reducing your sphere radius to be more on par with the sizes of your characters" ie. 0.4? \$\endgroup\$
    – DMGregory
    Oct 31, 2017 at 21:09
  • \$\begingroup\$ if (Physics.SphereCast(controller.eyes.transform.position, 0.4f,controller.eyes.transform.forward, out hit) && hit.collider.CompareTag("Player")) { var g = 0; } Still gives me no results :( \$\endgroup\$ Oct 31, 2017 at 21:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .