Unity; Random Object Movement [closed]

2019-07-13 15:54发布

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条回答
戒情不戒烟
2楼-- · 2019-07-13 16:28

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.

查看更多
登录 后发表回答