随机移动尝试

时间:2015-03-04 13:17:34

标签: random unity3d artificial-intelligence unityscript

当玩家不在其范围内时,我试图让我的熊随机行走。我希望它在y轴上随机选择它的旋转然后向前移动它,并在一段时间后再次随机切换旋转。

事情是,我真的不知道如何给他随机轮换。 那我该怎么做呢?

我终于设法做到了,这是工作代码:

var timeToChangeDirection : float = 60;
private var angle : Vector3;

var damping : float = 6.0;

var controller : CharacterController;

function Update()
{
    distance = Vector3.Distance(target.position, transform.position);

    if(distance > aggroRange)
    {
        moveSpeed = 1;
        timeToChangeDirection -= 0.1;

        if (timeToChangeDirection <= 0) 
        {
            ChangeDirection();
        }

        controller.Move(transform.forward * Time.deltaTime);
        animation["Run"].speed = 0.25;
        animation.Play("Run");
    }

function ChangeDirection()
{
    angle = Vector3(transform.rotation.x, Random.Range(-359, 359), transform.rotation.y);
    transform.Rotate(angle);
    timeToChangeDirection = 60;
}

1 个答案:

答案 0 :(得分:0)

好。毕竟,我设法做到了。 如果有人感兴趣的话,请输入以下代码:

var timeToChangeDirection : float = 60;
private var angle : Vector3;

var damping : float = 6.0;

var controller : CharacterController;

function Update()
{
    distance = Vector3.Distance(target.position, transform.position);

    if(distance > aggroRange)
    {
        moveSpeed = 1;
        timeToChangeDirection -= 0.1;

        if (timeToChangeDirection <= 0) 
        {
            ChangeDirection();
        }

        controller.Move(transform.forward * Time.deltaTime);
        animation["Run"].speed = 0.25;
        animation.Play("Run");
    }

function ChangeDirection()
{
    angle = Vector3(transform.rotation.x, Random.Range(-359, 359), transform.rotation.y);
    transform.Rotate(angle);
    timeToChangeDirection = 60;
}