- Home /
Clamping after forcing aspect ratio
I'm using the following script to force aspect ratio of 16:9: https://forum.unity.com/threads/force-camera-aspect-ratio-16-9-in-viewport.385541/
It works perfectly. However now my game objects are allowed to move off the edges. The camera of the game is fixed, and never needs to move or scale.
I have changed my code to WorldToViewportPoint for this but half my of character still goes off the edge.
void Update()
{
Vector3 pos = cam.WorldToViewportPoint(transform.position);
pos.x = Mathf.Clamp01(pos.x);
transform.position = Camera.main.ViewportToWorldPoint(pos);
}
private void FixedUpdate()
{
Vector3 pos = cam.ScreenToWorldPoint(Input.mousePosition);
if (pos.x - transform.position.x > 0.5f)
{
transform.eulerAngles = new Vector2(0, 180);
animateOgre(true);
Vector2 move = new Vector2(1, 0);
rb.MovePosition((Vector2)transform.position + (move * speed * Time.deltaTime));
}
else if (pos.x - transform.position.x < -0.5f)
{
transform.eulerAngles = new Vector2(0, 0);
animateOgre(true);
Vector2 move = new Vector2(-1, 0);
rb.MovePosition((Vector2)transform.position + (move * speed * Time.deltaTime));
}
else
{
animateOgre(false);
}
}
My questions are:
How do I allow the whole character to stay within the Viewport?
How can I clamp within the fixed update method rather than making a seperate update method.
Is there anyway to prevent character from moving when the mouse is aligned up with the position of the character. Right now I'm using a hacky solution to find if the x difference is > 0.5. (If i don't do this the character just moves back and forth when mouse stops)
Your answer
Follow this Question
Related Questions
Screen stretch 0 Answers
Changing screen resolution is cutting the very edge of my screen off 1 Answer
How can I support FHD+ Resolution in Android game? 0 Answers
Standalone fullscreen-fixed size 0 Answers
Fixed to the screen GUI ? 1 Answer