0
\$\begingroup\$

The idea here is that the raycast shoots downwards from an empty game object and when it detects that there is no ground below it the character should bounce back and walk the other way. When I get to the edge of my platform the character just gets stuck in place and keeps rotating on the Y axis.

public class npcAI : MonoBehaviour
{
public float speed;
public float distance;
private bool movingRight = true;
public Transform groundDetection; //raycast goes downward from this point

private Vector2 movementDirection;

void Start()
{
    //Initialise direction
    movementDirection = Vector2.right;
}

void Update()
{
    transform.Translate(movementDirection * speed * Time.deltaTime);
    RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
    if (groundInfo.collider == false)
    {
        if (movingRight)
        {
            movingRight = false;
            movementDirection = Vector2.left;
      transform.localScale -= new Vector3(0.30F, 0, 0);

        }
        else
        {
            movingRight = true;
            movementDirection = Vector2.right;
      transform.localScale += new Vector3(0.30F, 0, 0);
        }

    }
}

The code above was edited to reflect the answer below. If you find yourself attempting to do the same thing and need to flip your sprite when you reach the edge change 0.30F to whatever double the X value of your sprite is in order to flip it. My sprites scale was (0.15, 0.15, 0) so I used 0.30F to flip my sprite. Be mindful of when you should use -= and when you should use +=.

\$\endgroup\$
  • 1
    \$\begingroup\$ Your raycast should not be perpendicular to the ground but rather oblique. to avoid being stuck at that frame where the raycast will always return false. \$\endgroup\$ – Omar Guendeli Nov 7 '18 at 9:59
1
\$\begingroup\$

It looks as though your if block is wrong:

if (groundInfo.collider == false) 
{
    if (movingRight == true)
    {
        transform.eulerAngles = new Vector3(0, -180, 0);
    }
    movingRight = false;
}
else
{
    transform.eulerAngles = new Vector3(0, 0, 0);
    movingRight = true;
}

Try this instead:

if (groundInfo.collider == false)
{
    if (movingRight)
    {
        transform.eulerAngles = new Vector3(0, -180, 0);
        movingRight = false;
    }
    else
    {
        transform.eulerAngles = new Vector3(0, 0, 0);
        movingRight = true;
    }
}

Personally I would have done something like this:

public class npcAI : MonoBehaviour
{
    public float speed;
    public float distance;
    private bool movingRight = true;
    public Transform groundDetection; //raycast goes downward from this point

    private Vector2 movementDirection;

    void Start()
    {
        //Initialise direction
        movementDirection = Vector2.right;
    }

    void Update()
    {
        transform.Translate(movementDirection * speed * Time.deltaTime);
        RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
        if (groundInfo.collider == false)
        {
            if (movingRight)
            {
                movingRight = false;
                movementDirection = Vector2.left;

            }
            else
            {
                movingRight = true;
                movementDirection = Vector2.right;
            }

        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ You should format the original code that you corrected to make it easier to see where the issue is. It took me several passes to realize flag was out of scope from the check. \$\endgroup\$ – Stephan Nov 7 '18 at 15:24
  • \$\begingroup\$ This was really helpful and I added a couple lines in order to flip the sprite itself. \$\endgroup\$ – Mark Gregg Nov 7 '18 at 22:24
  • 1
    \$\begingroup\$ I think I'm going to add it here in case anyone comes across this question later and needs help. \$\endgroup\$ – Mark Gregg Nov 7 '18 at 22:32
  • 1
    \$\begingroup\$ @Stephan I have updated the formatting as recommended. \$\endgroup\$ – King-Xero Nov 9 '18 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.