统一;随机物体运动 [英] Unity; Random Object Movement

查看:107
本文介绍了统一;随机物体运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个机器人格斗游戏,我希望敌人随机移动,然后有时向敌人行进.我希望运动进入的代码.

I am making a robot battling game where I want the enemy to randomly move and then sometimes travel towards the enemy. Code that I want the movement to be in.

else if (avoid == false)
        {
            transform.LookAt(target);
            transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed);
            currentLerpTime = 0;
        }

此代码仅使AI向玩家移动,但我也希望它偶尔向随机方向移动,然后偶尔改变方向.我该怎么办?

This code just makes the AI move towards the player but I also want it occasionally to move in a random direction and then change direction occasionally. How would I do this?

推荐答案

要使其沿随机方向移动,您需要编辑的变量是矢量.

To make it move in a random direction, the variable you need to edit is the vector.

else if (avoid == false)
        {
            transform.LookAt(target);
            transform.Translate(Vector3.forward*Time.deltaTime*Speed);
                                 // ^^^^^^
            currentLerpTime = 0;
        }

虽然这里的问题是它在移动时会继续观察目标(我假设这是每帧都被调用).就生成三个随机数而言,您可以仅使用C#自己完成此操作,也可以阅读此书

The problem here though is while it moves it will continue to keep looking at the target (i am assuming this is getting called each frame). In terms of generating three random numbers, you can do this by yourself with just C# or go read this

https://docs.unity3d.com/ScriptReference/Random.html

该链接确实可以为您提供帮助.

That link should really help you out.

希望这会有所帮助.

这篇关于统一;随机物体运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆