简体   繁体   中英

How to make an object change direction?

I'm trying to make a fish change direction when it touches certain triggers. I put a trigger at the right of the scene, and I want the fish to move left when it touches it, and to move right when it touches the left trigger. But, I cannot put two "OnTriggerEnter2D" functions in one class. How can I do please ?

Here is the code:

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

public class PoissonMouvementScript : MonoBehaviour
{
    public Collider2D colliderleft;
    public Collider2D colliderright;
    public Rigidbody2D rb2;
    public bool fishdirection = false;
    public float sidewaysforce = 1;

    private void OnTriggerEnter2D(Collider2D colliderleft)
    {
        fishdirection = true;
        Debug.Log("Fish is going right");
    }

    private void OnTriggerEnter2D(Collider2D colliderright)
    {
        fishdirection = false;
        Debug.Log("Fish is going left");
    }

    void FixedUpdate()
    {
        if(fishdirection == false)
        {
            rb2.AddForce(new Vector2(sidewaysforce * Time.deltaTime, 0));
        }
        else if (fishdirection == true)
        {
            rb2.AddForce(new Vector2(-sidewaysforce * Time.deltaTime, 0));
        }
    }

Thanks for your help guys !

You have several options.

One is to have a script in the left and right collider object that has OnCollisionEnter and checks for collision with player and adds velocity to it.

Another option is to tag the two edge objects with tags like "LeftEdge" and "RightEdge". Then in the script you got there in OnCollisionEnter you put collider.CompareTag("LeftEdge") and do your thing and then the same with RightEdge.

There are other options as well, but they would overcomplicate what seems like a simple project.

Ok, thanks to Anton, the script now works. So basically, if you want to make a moving entity from point A to point B, and from point B to point A, you have to place 2 colliders (one at the left and one at the right), and tag them with a tag like "LeftEdge" or "RightEdge", then add this script to the entity:

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

public class BoulbeFishDirection : MonoBehaviour
{
    public Rigidbody2D rb2;
    public bool fishdirection = false;
    public float sidewaysforce = 1;

    private void OnTriggerEnter2D(Collider2D collider)
    {
        if(collider.CompareTag("LeftEdge"))
        {
            fishdirection = true;
            Debug.Log("Fish is going right");
        }

        else if(collider.CompareTag("RightEdge"))
        {
            fishdirection = false;
            Debug.Log("Fish is going left");
        }
    }

    void FixedUpdate()
    {
        if (fishdirection == true)
        {
            rb2.AddForce(new Vector2(sidewaysforce * Time.deltaTime, 0));
        }
        else if (fishdirection == false)
        {
            rb2.AddForce(new Vector2(-sidewaysforce * Time.deltaTime, 0));
        }
    }
}

Have a nice day and have fun coding !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM