首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字符移动代码不工作- Unity3D单元脚本

字符移动代码不工作- Unity3D单元脚本
EN

Stack Overflow用户
提问于 2015-03-01 09:25:47
回答 1查看 747关注 0票数 0

我有一个脚本,移动字符的方向取决于它与什么碰撞。下面是我的代码:

代码语言:javascript
复制
var speed : float = 1;
private var leftBool : boolean;
private var rightBool : boolean;
private var upBool : boolean;
private var downBool : boolean;

function Update () {
    if(leftBool == true) {
        gameObject.GetComponent(Rigidbody2D).velocity.x =         gameObject.GetComponent(Rigidbody2D).velocity.x - speed * Time.deltaTime;
        rightBool = false;
        upBool = false;
        downBool = false;
    }
    if(upBool == true) {
        gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y + speed * Time.deltaTime;
        rightBool = false;
        leftBool = false;
        downBool = false;
    }
    if(rightBool == true) {
        gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x + speed * Time.deltaTime;
        rightBool = false;
        leftBool = false;
        downBool = false;
    }
    if(downBool == true) {
        gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y - speed * Time.deltaTime;
        rightBool = false;
        leftBool = false;
        upBool = false;
    }   
}

function Start () {
    leftBool = true;
}


function OnTriggerEnter2D (other : Collider2D) {
    if(other.GetComponent(Arrows).rotationNumber == 1) {
        leftBool = true;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 2) {
        upBool = true;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 3) {
        rightBool = true;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 4) {
        downBool = true;
        Debug.Log("Entered");
    }
}

这一切为什么要发生?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-03-01 15:21:28

起初,当假设与rotationNumber==1发生冲突时,leftBool仍然为真。根据声明和Update()中的代码,其他代码为false。现在,当您与rotationNumber==2发生冲突时,upBool将返回true。但如前所述,在Update()中,leftBool仍然为true,并且它位于代码执行顺序的顶部,upBool将立即返回false。所以,你永远不会得到if(upBool == true){}个东西(顺便说一句,你不必使用==操作符来表示布尔,if(upBool)表示true,if(!upBool)表示false)。希望它能澄清你的困惑。现在,有很多方法可以实现你想要的。但是,我修改了你的代码,这样你就能明白发生了什么-

代码语言:javascript
复制
var speed : float = 1;
private var leftBool : boolean;
private var rightBool : boolean;
private var upBool : boolean;
private var downBool : boolean;

function Start () {
    leftBool = true;
}

function Update () {
    if(leftBool) {
        gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x - speed * Time.deltaTime; 
    }
    if(upBool) {
        gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y + speed * Time.deltaTime;   
    }
    if(rightBool) {
        gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x + speed * Time.deltaTime;
    }
    if(downBool) {
        gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y - speed * Time.deltaTime;
    }   
}

function OnTriggerEnter2D (other : Collider2D) {
    if(other.GetComponent(Arrows).rotationNumber == 1) {
        leftBool = true;
        rightBool = false;
        upBool = false;
        downBool = false;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 2) {
        upBool = true;
        rightBool = false;
        leftBool = false;
        downBool = false;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 3) {
        rightBool = true;
        upBool = false;
        leftBool = false;
        downBool = false;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 4) {
        downBool = true;
        rightBool = false;
        leftBool = false;
        upBool = false;
        Debug.Log("Entered");
    }
}

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28789044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档