Unity; Random Object Movement [closed]

会有一股神秘感。 提交于 2020-01-04 06:09:11

问题


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;
        }

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?


回答1:


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;
        }

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.

Hope this helps.



来源:https://stackoverflow.com/questions/38879854/unity-random-object-movement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!