- Home /
move sprites in side scrolling 2d game in screen coordinates
I am trying to make the enemies in my side scrolling 2d game move in absolute screen coordinates instead of world coordinates I have their waypoints stored in screen coordinates and use the following line of code to move them:
// Move the enemy toward its waypoint
transform.position = Vector3.MoveTowards
(transform.position,
mainCamera.camera.ViewportToWorldPoint(nextWaypoint),
currentSpeed * Time.deltaTime);
The problem I am having is that, since the actual movement happens in world space, the enemies are fighting the screen scrolling so their speeds are messed up and their target position moves away from the screen location. I want them to move as if they are on a static plane and not a scrolling map (They are aerial) I have googled up a storm but have not found a way.
My problem is probably mindset, I am used to XNA where I would create worldToScreen() and ScreenToWorld() methods and then use them interchangeably depending on the context of the problem being solved.
Thanks for any help
Mike
I'm pretty good at figure out these kinds of problems. I read your question when it first came in the list and again just now, and I'm having difficulty understanding your problem. Can you post a video of the 'speeds are messed up'?
I want them to move as if they are on a static plane and not a scrolling map
As long as the speed is high enough to overcome the scrolling and if the object is at the waypoint, then this should be happening.
I thought about this issue last night and I think I have a solution. It is a side scroller ($$anonymous$$oon Patrol clone as a learning project) and the areal enemies are my issue. I started with a waypoint system in world coordinates but they would get left behind if the player sped up.
I changed the coordinate system to use ViewPortToWorld() and that helped but they still fight the scroll speed of the game because the target location moves after the first frame.
I am going to try recalculating the world coordinate they are headed to each frame and then compensate for the variable scroll speed of the player by adding the players speed to their speed each frame.
Your answer
Follow this Question
Related Questions
Poor quality sprite 2D game 3 Answers
How to Instatiate an empty spriteObject and be able to draw a image/sprite like you would in paint? 0 Answers
How to animate a 2d character 1 Answer
Changing a Sprite when Collision occurs 2 Answers
How do i change the sprite on a sprite renderer without changing the size? 1 Answer