My Realm Rush World

1 Like

Hey dude cool scene!
I guess you want that your enemy move smoothly to add some flavor to your game?
Try this method(EnemyMove script)

    int i = 0;
    IEnumerator FollowPath(List<Waypoint> path)
    {
        yield return null;
        for ( ; i < path.Count; i++)
        {
            Vector3 randomValue = new Vector3(1,0,1) * Random.Range(-0.5f, 0.5f) + path[i].transform.position;
            transform.LookAt(randomValue);
            while (Vector3.Distance(transform.position, randomValue) >= 0.35f)
            {
                transform.position = Vector3.MoveTowards(transform.position, randomValue, Time.deltaTime* 3f);
                yield return null;
            }
        }
    }

your enemy will not only move smooth, but do some small turns which looks more natural than just go straight forward.

3 Likes

Woah that’s cool! Thanks so much!

@111100

I used your code in my project and it works great! I made a small change where the transform.position is being calculated and created a Serialized Field so that I can alter how fast the enemy moves.

[SerializeField] float enemySpeed = 7f;

	// smooth FollowPath Function
	IEnumerator FollowPath(List<Waypoint> path)
	{
		yield return null;
		for (int i = 0; i < path.Count; i++)
		{
			Vector3 randomValue = new Vector3(1, 0, 1) * UnityEngine.Random.Range(-0.5f, 0.5f) + path[i].transform.position;
			transform.LookAt(randomValue);
			while (Vector3.Distance(transform.position, randomValue) >= 0.35f)
			{
				transform.position = Vector3.MoveTowards(transform.position, randomValue, Time.deltaTime * enemySpeed);
				yield return null;
			}
		}
	}
1 Like

Thats great! Thanks for feedback!

thx i used it in my game and it works wonderfull

heloo sir… I’ve just tried your code, and it worked really good… so, can you explain what’s actually going on in those code? so I’m not just copy paste it… or anyone here that understand can explain? thanks

  int i = 0; // Variable that store current path node

    IEnumerator FollowPath(List<Waypoint> path)
    {
        yield return null; // Delay for 1 frame

// iteration for all path nodes
        for ( ; i < path.Count; i++)
        {
// small random value for moving(optional) part new Vector3(1,0,1) * Random.Range(-0.5f, 0.5f) 
//can be removed
            Vector3 randomValue = new Vector3(1,0,1) * Random.Range(-0.5f, 0.5f) + path[i].transform.position;
// this rotates transform.forward to direction in 3d space. If you use 2d space use transform.up = direction(Vec2)
            transform.LookAt(randomValue);

//while distance between my current position to destination position is greater than small number move me towards destination every frame and wait 1 frame

            while (Vector3.Distance(transform.position, randomValue) >= 0.35f)
            {
                transform.position = Vector3.MoveTowards(transform.position, randomValue, Time.deltaTime* 3f);
                yield return null;
            }
        }
    }

When youmove object transform.position = some position - you basically teleport it instantly.If you want smooth move over time, you have to split the movement into many small teleports, and after each small teleport wait 1 frame.

For example you stand on (0, 0) and you want to go to (3, 0) over 1 sec.
Your destination is 3,0, your start position is 0,0. Direction = destination - start position
Direction over 1 frame = (3,0) / amount of frames in 1 second
which is same as Direction(3,0) * time.deltaTime

To move over 1 sec you add to your position Direction over 1 frame every frame for 1 sec of time =)

after read your explanation, I got it… thank you so much sir… :grinning:

1 Like

Privacy & Terms