简体   繁体   中英

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.

人物移动的画面。

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?

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

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