0
\$\begingroup\$

I am making a game in which I created a character (main player) and another character(snail). Now the snail will move on the ground, when there is no ground detected it will change its direction. I attached transform empty game objects on either side (left and right) to detect collisions with the main player. When it changes the direction the transform empty game objects are also changing their direction. To get rid from that I swapped them in my code, when I swapped them, they are moving far away from the snail why it happened? (But they are changing their direction).

The code of snail is :

The game seen here .

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnailScript : MonoBehaviour
{
    private Rigidbody2D myBody;
    private Animator anim;
    public float speed = 0.2f;
    public Transform groundCheck,up_position,left_position,right_position;
    private bool moveLeft, canMove, stun;
    public LayerMask playerLayer;
    private Vector3 leftCollisionPosition, rightCollisionPostion;

    void Start()
    {
        moveLeft = true;
        canMove = true;       
    }

    void Awake()
    {
        myBody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        leftCollisionPosition = left_position.position;
        rightCollisionPostion = right_position.position;
    }
  
    void Update()
    {
        playerMove();
        collision();
    }
    
    void playerMove()
    {
        if (canMove)
        {
            changeDirection();
            if (moveLeft)
            {
                myBody.velocity = new Vector2(-speed, myBody.velocity.y);
                
            }
            else
            {
                myBody.velocity = new Vector2(speed, myBody.velocity.y);
               
            }
        }
    }

    void changeDirection()
    {
        if (!Physics2D.Raycast(groundCheck.position, Vector2.down, 0.1f))
        {
            moveLeft = !moveLeft;
            Vector3 tempScale = transform.localScale;
            if (moveLeft)
            {
                left_position.position = leftCollisionPosition;
                right_position.position = rightCollisionPostion;
                tempScale.x = Mathf.Abs(tempScale.x);
                

            }
            else
            {
                left_position.position = rightCollisionPostion;
                right_position.position = leftCollisionPosition;
                tempScale.x = -Mathf.Abs(tempScale.x);
                
            }
            transform.localScale = tempScale;
        }

    }

    void LeftRightCollisions()
    {
        RaycastHit2D leftHit = Physics2D.Raycast(left_position.position, Vector2.left, 0.1f, playerLayer);
        RaycastHit2D righHit = Physics2D.Raycast(right_position.position, Vector2.right, 0.1f, playerLayer);
    }

    void collision()
    {
        Collider2D upHit = Physics2D.OverlapCircle(up_position.position, 0.2f, playerLayer);
        if (upHit != null)
        {
            if(upHit.gameObject.tag == "player")
            {
                if (!stun)
                {
                    upHit.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(upHit.gameObject.GetComponent<Rigidbody2D>().velocity.x, 7f);
                    canMove = false;
                    myBody.velocity = new Vector2(0, 0);
                    anim.Play("stunAnimation");
                    stun = true;

                }
            }
        }

        RaycastHit2D leftHit = Physics2D.Raycast(left_position.position, Vector2.left, 0.1f, playerLayer);
        RaycastHit2D righHit = Physics2D.Raycast(right_position.position, Vector2.right, 0.1f, playerLayer);

        if (stun)
        {
            if (leftHit)
            {
                myBody.velocity = new Vector2(20f, myBody.velocity.y);
            }
            if (righHit)
            {
                myBody.velocity = new Vector2(-20f, myBody.velocity.y);
            }
        }
    }
}//class
\$\endgroup\$
1
  • \$\begingroup\$ Remember to proofread your post when editing. If the code formatting is looking incorrect, you're not done yet. Visit the help center if you need support getting the markdown formatting right. \$\endgroup\$
    – DMGregory
    Sep 24 at 20:07

1 Answer 1

0
\$\begingroup\$

It looks like you meant to use localPosition (the offset of the child object relative to its parent) rather than position (the object's absolute location in the world). Try rewriting your script like so...

void Awake()
{
    myBody = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    leftCollisionPosition = left_position.localPosition;
    rightCollisionPostion = right_position.localPosition;
}

...

// Use an initial uppercase letter for method names.
void ChangeDirection()
{
    if (!Physics2D.Raycast(groundCheck.position, Vector2.down, 0.1f))
    {
        moveLeft = !moveLeft;
        Vector3 tempScale = transform.localScale;
        if (moveLeft)
        {
            left_position.localPosition = leftCollisionPosition;
            right_position.localPosition = rightCollisionPostion;
            tempScale.x = Mathf.Abs(tempScale.x);
        }
        else
        {
            left_position.localPosition = rightCollisionPostion;
            right_position.localPosition = leftCollisionPosition;
            tempScale.x = -Mathf.Abs(tempScale.x);                
        }
        transform.localScale = tempScale;
    }
}
\$\endgroup\$

You must log in to answer this question.

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