- Home /
Faking inertia with projectiles in endless scroller.
I have a kind of endless scroller where the player and the camera are continually moving along the world z-axis. The player throws projectiles where the player clicks the mouse (based on a raycast from the camera). The projectile will move toward the clicked destination just fine, but it will appear faster if it is thrown behind the player character and slower if thrown in front of the player character (because of the constant motion).
To fix this, I added the constant motion of the z-axis, but now the projectile obviously misses the target location. I know there is a simple solution to getting the speed correct and the accuracy right, but I am completely blanking right now. Help?
Thanks.
Would having the projectile attached as a child to the player or camera help? It would allow more of a local speed ins$$anonymous$$d of global.
It effectively does the same thing that my "fix" code did. Now it has the movement of the player or camera as well (which is the scrolling movement).
Im not sure if its relevant in your game but if it's childed, if the player moves backwards then so will the projectiles.
Answer by xt-xylophone · Nov 12, 2013 at 03:59 AM
With the addition of the momentum in the z-axis you have pretty much re created how throwing projectiles works in real life.
So if you want players to still hit their mark you will either need to make them aware of the fact that aiming requires a bit more thinking i.e. aiming straight down to hit something down and right etc.
Or instead of the projectile go towards where they click, make it move to a point in front of the click point. How far in front will be some maths on how far away the target is and how fast the projectile is. This will always hit that position as it moves away from you but it 'may' look weird as the projectile leaves the player.
Or alternatively even make the projectile curve and home in on the point clicked if that satisfies how the game works.
So tldr
Tell the player to account for the inertia
Do some aim correction for them
Change how projectiles work
I guess I'm looking for the "Do some aim correction for them" answer.
I have gone about this two ways now:
1) updating the direction in my update function (works, but seems wasteful)
2) The following code to create a new target point:
distance = Vector3.Distance(target, myTransform.position);
zOffset = (distance / (moveSpeed * Time.fixedDeltaTime)) * (scrollSpeed * Time.fixedDeltaTime); // Take the distance between the origin and the target and divide it by the speed it will go to find how long it will take. Take that time and multiply it by the speed of the scrolling to see how far forward on the z-axis we will go.
target.z -= zOffset;
This code doesn't work and I'm not sure why... It ends up behind the target ins$$anonymous$$d of in front of it.
Im not sure if that will hit 100% of the time but if its just on the wrong side try changing that -= zOffset to +=?