简体   繁体   English

如何更改玩家位置 ontriggerenter unity 2d C#

[英]how to change player location ontriggerenter unity 2d c#

I'm trying to make a platformer with two characters and I want it so when they touch it goes to the next level.我正在尝试制作一个带有两个角色的平台游戏,我想要它,所以当他们触摸它时,它会进入下一个级别。 however, it instantly teleports me to the end of the game help!!但是,它立即将我传送到游戏结束帮助! I'm new to coding so don't judge lol.我是编码新手,所以不要判断大声笑。

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

public class Mrglobtransform : MonoBehaviour
{
    private int level;
    public GameObject player;
    private bool update;
    private void Start()
    {
        level = 1;
    }
     IEnumerator waitforknees()
    {
        
        yield return new WaitForSeconds(0.4f);
        update = false;
    }
    void knees()
    {
        if (update == true)
        {
            if (level == 2)
            {
             player.transform.position = new Vector3(-42.75f, -31.97f, 0);
             GameObject.Find("Main Camera").GetComponent<cameramove>().player = 2;
             
            }
            if (level == 3)
            {
             player.transform.position = new Vector3(-7.43f, -196.2f, 0);
             GameObject.Find("Main Camera").GetComponent<cameramove>().player = 2;
             
            }
            StartCoroutine(waitforknees());
            return;
        }
    }
   
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.layer == 8)
        {
            Debug.Log("sd hfjsgbchgbvds hgvn");
            knees();
            if (level == 1)
            {
                level = 2;    
            }
            if (level == 2)
            {
                level = 3;
            }
            update = true;
        }
    }
    
}

ignore the names I'm uncreative.忽略我没有创意的名字。 im new to coding there's probably an easy fix that I'm missing.我是编码新手,我可能缺少一个简单的修复方法。

So some issues here.所以这里有些问题。

First of all首先

if (level == 1)
{
    level = 2;    
}
if (level == 2)
{
    level = 3;
}

this means if your level at the top is 1 or 2 it will always be 3 at the end because both cases will be executed sequencially.这意味着如果您在顶部的level12 ,那么最后总是3 ,因为这两种情况都将按顺序执行。

You rather want to do eg你宁愿做例如

level++;
// top it to 3
level = Mathf.Min(level, 3);

Then you are setting update = true after calling knees .. so most probably nothing will ever happen at all.然后你调用knees设置update = true .. 所以很可能什么都不会发生。

Why not simply do为什么不简单地做

private void OnTriggerEnter2D(Collider2D collision)
{
    if(collision.gameObject.layer == 8)
    {
        Debug.Log("sd hfjsgbchgbvds hgvn");

        level++;
        // top it to 3
        level = Mathf.Min(level, 3);

        knees();
    }
}

void knees()
{
    switch(level)
    {
        case 2:
            player.transform.position = new Vector3(-42.75f, -31.97f, 0);
            GameObject.Find("Main Camera").GetComponent<cameramove>().player = 2;
            break;
        case 3:
            player.transform.position = new Vector3(-7.43f, -196.2f, 0);
            GameObject.Find("Main Camera").GetComponent<cameramove>().player = 2;
            break;
    }
}

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

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