简体   繁体   English

unity 2D 游戏中的角色碰撞问题

[英]Character Collision problem in unity 2D game

I'm making a 2D game and trying to make a characters move automatically when they spawn from the door to the desk or close to the sofa then stop there.我正在制作一个 2D 游戏,并试图让角色在从门到桌子或靠近沙发时自动移动,然后停在那里。 the problem is that they don't collide with anything in the background like sofa or table or desk and they keep going trying to force move and going out of the canvas.问题是它们不会与背景中的任何东西(如沙发、桌子或桌子)发生碰撞,并且它们会继续试图强制移动并离开 canvas。 this is a GIF for the game play and script that i'm using on the characters:这是我在角色上使用的游戏和脚本的 GIF:

What am I supposed to do?我应该做些什么?

错误的碰撞

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);
        
    }
}

GameManager.Cs游戏管理器.Cs

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

public class GameManager : MonoBehaviour
{

    public float moveSpeed = 1.0f;
    public Vector2 moveVector;



}

That's what happens when you translate the body in every frame.当您在每一帧中平移身体时,就会发生这种情况。 From an answer to this similar problem has been addressed, Transform is not a physics-based component.从已经解决了这个类似问题的答案来看, Transform不是基于物理的组件。 It's the position, rotation, and scale of the object.它是 position,旋转和缩放 object。 It doesn't interact with physics system on it's own.它本身不与物理系统交互。 Rigidbody is a physics-based component that performs different kinds of physics interactions, like collisions, forces, and more. Rigidbody是一个基于物理的组件,它执行不同类型的物理交互,如碰撞、力等。

Add a rigidbody component to your gameobject and set it's velocity, it then interacts with the game physics为你的游戏对象添加一个刚体组件并设置它的速度,然后它与游戏物理交互

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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