unity3d c# unity2d:如何移动一个对象并在它碰撞时改变方向?

lmyy7pcs  于 2022-12-13  发布在  C#
关注(0)|答案(2)|浏览(175)

物体在移动,但它没有改变方向,我不知道为什么。

这是名为'zackenblock'的对象的代码:

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

public class zackenblock_move : MonoBehaviour
{
    public static bool col = false;
    public float speed = 10f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    void Update()
    {
        if (zackenblock_col.collosin != col)
        {
            col = zackenblock_col.collosin;
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (col == false){
            transform.Translate(Vector3.right * Time.deltaTime * speed);
        }
        else
        {
            transform.Translate(Vector3.left * Time.deltaTime * speed);
        }

        /*if (Input.GetKey(KeyCode.M))
        {
            col = true;
        }*/
    }
    void OnCollisionEnter(Collision collision){
        //Check for a match with the specified name on any GameObject that collides with your GameObject
        if (collision.gameObject.name == "Level1_part2")
        {
            //If the GameObject's name matches the one you suggest, output this message in the console
            Debug.Log("Do something here");
            col = true;
        }

        else if (collision.gameObject.name == "Level")
        {
            //If the GameObject's name matches the one you suggest, output this message in the console
            Debug.Log("Do something here");
            col = false;
        }
    }
    /*IEnumerator waiter_not_that_waiter_just_waiter(){
        yield return new waitforseconds(3f);
        //my code here after 3 seconds
        transform.Translate(Vector3.right * Time.deltaTime);
        yield return new waitforseconds(3f);
        transform.Translate(Vector3.left * Time.deltaTime);
    }*/

}

这就是Tilemap中的代码

using System.Security.Cryptography;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class zackenblock_col : MonoBehaviour
{
    public static bool collosin;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.M))
        {
            collosin = true;
        }
    }
    void OnCollisionEnter(Collision collision){
        //Check for a match with the specified name on any GameObject that collides with your GameObject
        if (collision.gameObject.name == "zackenblock")
        {
            //If the GameObject's name matches the one you suggest, output this message in the console
            Debug.Log("Do something here");
            collosin = true;
        }

        /*else if (collision.gameObject.name == "Level")
        {
            //If the GameObject's name matches the one you suggest, output this message in the console
            Debug.Log("Do something here");
            collosin = false;
        }*/
    }
}

以下是一些截图:
Unity和对象的屏幕截图:

Unity和切片图的屏幕截图:

Unity和Tilemap2的屏幕截图:

物体应该向右移动,当它碰撞时,然后向左移动。
它只不停地向右移动。

bjp0bcyl

bjp0bcyl1#

您的代码的主要问题是拼写错误。将“collosin”更改为“collision”。并更改:

void OnCollisionEnter(Collision collision)

进入

void OnCollisionEnter2d(Collision2d collision)

除此之外,你的代码看起来还不错。

nlejzf6q

nlejzf6q2#

使用OnCollisionEnter 2D,因为您正在制作2D游戏

private void OnCollisionEnter2D(Collision2D col)
        {
           
        }

如果碰撞倾向于粘滞,还可以检查OnCollisionStay 2D/exit。

相关问题