Hello guys, I’m trying to make a Rail Space Shooter (like Star Fox, for example), and have been with an issue for a few days.
I’m trying to clamp the player to the camera, so it cannot move arround beyond the camera.
Searching over, I made this two methods in a script assigned to the player GameObject:
void Update(){
processInput();
float h = Input.GetAxis("Mouse X");
float v = Input.GetAxis("Mouse Y");
MoveArround(h, v, 18);
}
void MoveArround(float x, float y, float speed)
{
transform.localPosition += new Vector3(x, y, 0) * speed * Time.deltaTime;
ClampPosition();
}
void ClampPosition()
{
Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
pos.x = Mathf.Clamp01(pos.x);
pos.y = Mathf.Clamp01(pos.y);
transform.position = Camera.main.ViewportToWorldPoint(pos);
}
The issue is that if I don’t call the method ClampPosition(), as spected, the player can move beyond the camera. If I call it, the player stays still, and don’t move at all.
A weird thing, is that if I put a debugger log on both methods, de Console show the text on the MoveArround method, the console is showing the message on the log constantly, but the debugger log on the ClampPosition method is only shown once.
Hope someone can help me.
Thank you all!