Hi, I am making a Tower Defence game and have tried to make a slow tower. It slows the enemy, but the enemy backs, and then continues with a lower speed. It does this because I’m using Lerp. But I don’t know how to fix it. Here is my code:
Yes, but that’s not the problem. When the turret hits the enemy, the speed goes down with 0.5. Because of this gameObject.transform.position doesnt work, so the enemy teleports backwards, and then continues at 0.5 of the speed
I’ve never made a tower defence game before, but i would use the Navmeshagent and waypoints, and just half the speed on a bullet collision. This also allows you to bake the map and set the movable zones for the enemies
edit: i’m also pretty sure navmeshagents aren’t suitable for 2D games, as it’s not supported as of my knowledge,
I would use vector3.distance and vector3.movetowards to get player movement, and set up 2d box colliders so they cant go into unwanted zones
Im using the vector3.moveTowards. But I can’t use the deltaTime. When i use this
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, endPosition, speed * Time.deltaTime);
The enemy starts on the right side (should be left), and out of bounds.
its possibly getting spawned using the prefab’s transform data, depending on the Instantiate method your using. The Instantiate() version can be buggy sometimes in that the transform isn’t cached properly. it’ll render in the scene where you specify it, but in physics memory it’ll think its at it’s prefab’s location. I’ve had my fair share of this issue back in 5.1.2 (when I tried to do raycasts from its transform position) and I’m not sure if it was ever fixed. nonetheless switching back to the normal Instantiate (and moving the prefab out of the way) avoided this issue.
normally in your code you were lerping from waypoint to waypoint. now you need to moveTowards to next waypoint from your current position, so you need to make sure you’re at the right position when you start.
in that script’s Start method just make sure that the transform.position = waypoints [0].transform.position;