敌人传送周围

时间:2015-01-28 01:34:36

标签: c# unity3d

所以我已经发布了很多关于这个项目的问题,但我遇到了另一个问题。我创造了一个像它应该四处游荡的游戏对象,但它传送到新的位置,而不是像它应该那样在那里慢慢滑动。

这是移动代码:

if(directionGenerator == 1){ //up
            enemy.transform.Translate(new Vector2(0, totalWalkDistanceUp) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: up");
        }
        else if(directionGenerator == 2){ //down
            enemy.transform.Translate(new Vector2(0, totalWalkDistanceDown) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: down");
        }
        else if(directionGenerator == 3){ //left
            enemy.transform.Translate (new Vector2(totalWalkDistanceLeft, 0) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: left");
        }
        else if(directionGenerator == 4){ //right
            enemy.transform.Translate(new Vector2(totalWalkDistanceRight,0) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: right");
        }

这是整个代码:

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
    private void Start(){
        findPosition();
    }
    private void walkRepeat(){
        InvokeRepeating("enemyWalk", 3, 5f);
        //enemyWalk();
    }


    //Variables
    public Transform enemy;

    private Vector2 position;

    /* not necessary
    private float maxPosX;
    private float maxPosY;
    private float maxNegX;
    private float maxNegY;
    */

    public float wanderSpeed = 3;

    private float totalWalkDistanceUp;
    private float totalWalkDistanceDown;
    private float totalWalkDistanceLeft;
    private float totalWalkDistanceRight;




    private void findPosition(){
        position.x = enemy.position.x;
        position.y = enemy.position.y;

        /* not neccessary
        maxPosX = position.x + 5;
        maxNegX = position.x - 5;
        maxPosY = position.y + 5;
        maxNegY = position.y -5;
        */

        Debug.Log ("Enemy position: " + position);
        //Debug.Log ("Max positive 'x' position: " + maxPosX + ". Max positive 'y' position: " + maxPosY + ". Max negative 'x' position: " + maxNegX + ". Max negative 'y' position: " + maxNegY);
        Debug.Log ("Finished!");

        walkRepeat();
    }

    private void enemyWalk(){
        //Distance Generator
        float walkDistance = Random.Range (40f, 70f);
        totalWalkDistanceUp = walkDistance + position.y;
        totalWalkDistanceDown = position.y - walkDistance;
        totalWalkDistanceRight = walkDistance + position.x;
        totalWalkDistanceLeft = position.x - walkDistance;
        Debug.Log ("Distance generated up: " + totalWalkDistanceUp + ". Distance generated down: " + totalWalkDistanceDown + ". Distance generated left: " + totalWalkDistanceLeft + ". Distance generated right: " + totalWalkDistanceRight);

        //Direction generator
        int directionGenerator = Random.Range (1, 4);

        if(directionGenerator == 1){ //up
            enemy.transform.Translate(new Vector2(0, totalWalkDistanceUp) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: up");
        }
        else if(directionGenerator == 2){ //down
            enemy.transform.Translate(new Vector2(0, totalWalkDistanceDown) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: down");
        }
        else if(directionGenerator == 3){ //left
            enemy.transform.Translate (new Vector2(totalWalkDistanceLeft, 0) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: left");
        }
        else if(directionGenerator == 4){ //right
            enemy.transform.Translate(new Vector2(totalWalkDistanceRight,0) * wanderSpeed * Time.deltaTime);
            Debug.Log ("Direction generated: right");
        }
    }

}

我不确定为什么它会传送到新位置而不是滑动。我确信这很简单,但感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

InvokeRepeating并不适合这个。这就是为什么它看起来像是传送...... 如果你想更新它的位置,你应该使用Update()或者如果你需要基于心灵的运动,那么你需要使用FixedUpdate() ...你可能可以使用当前的方法并使用{ {1}}在值之间进行插值,但在我看来它会变得非常奇怪......