kylorius beno
kylorius beno

Reputation: 191

My characters in my 2D game do not collide even when i create 2D collider box

i want to make the characters in my game collide with objects around them and stop moving. the thing is the characters just spawn and keep walking down the map until they're out of the Canvas view.

image of the characters moving.

my characters have Constantmove script that i assigned to them which is this one:

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

public class CharacterConstantMove : MonoBehaviour
{

    GameManager Game_Manager;

    void Start()
    {

        GameObject gameController = GameObject.FindGameObjectWithTag("GameController");
        Game_Manager = gameController.GetComponent<GameManager>();

    }


    void Update()
    {

        transform.Translate(Game_Manager.moveVector * Game_Manager.moveSpeed * Time.deltaTime);
        
    }
}

What do i do to make them stop around the desk and just stay idle when they collide there?

Upvotes: 0

Views: 52

Answers (1)

Filipeak
Filipeak

Reputation: 56

You are moving them with transform.Translate() function, which does not include physics. If you want them to collide, you would have to add Rigidbody2D component and move them using velocity property: https://docs.unity3d.com/ScriptReference/Rigidbody2D.html

Upvotes: 1

Related Questions