- Home /
Limiting player movement (Partially done)
I am currently using this code to restrict player movement along the horizontal axis, to ensure that the player stays within the camera view. Right now it works for the horizontal axis, but not the vertical axis, for some reason.
In other words, topB and downB values do not seem to be read correctly.
void Update () {
float distance = (transform.position.z - cam.transform.position.z);
float leftB = cam.ViewportToWorldPoint(new Vector3(0, 0, distance)).x;
float rightB = cam.ViewportToWorldPoint(new Vector3(1, 0, distance)).x;
float topB = cam.ViewportToWorldPoint(new Vector3(0, 1, distance)).z;
float downB = cam.ViewportToWorldPoint(new Vector3(0, 0, distance)).z;
Vector3 pos = transform.position;
pos.x = Mathf.Clamp(transform.position.x, leftB, rightB);
pos.z = Mathf.Clamp(transform.position.z, downB, topB); //Not working well:
// topB is roughly the center of the screen while downB is well beyond the
// bottom of the screen.
transform.position = pos;
}
Video describing problem: http://youtu.be/wfGoLZ2GV84
Why can't I make the camera just follow the player?
I have two players on screen and a camera which increases the ortographicSize as they get farther away from each other. At a certain point it stops stretching and needs to start restricting player movement instead.
Notes
he game is top down at an angle. I have been looking for help on camera.ViewportToWorldPoint, but I think I am not getting the dist (distance) right, since the camera is rotated at an angle, not straight down on the players (the cubes in the pictures below).
Have you tried to use the real distance between the object and the camera?
float distance = Vector3.Distance(transform.position, cam.transform.position);
@Luiz It almost works :) It is a bit glitchy though because it also takes the X into consideration though. Any way I can remove that? I have tried distance -= (transform.position.x - cam.transform.position.x); With no success.
Nope, removing the X from the equation still isn't perfect.
humm, you could try something like:
float distance = Vector3.Dot(cam.transform.forward, transform.position - cam.transform.position);
@Luiz Works! Can you put it as answer and explain in more detail maybe? :)
Answer by luizgpa · Feb 16, 2012 at 01:35 AM
If I understood your logic correctly you need the distance from the camera to a plane, parallel to the camera's projection plane, wich contains the player. One way to get this value is projecting a vector from the camera to the player into the camera's forward direction. This is accomplished by:
float distance = Vector3.Dot(cam.transform.forward, transform.position - cam.transform.position);
Your answer
Follow this Question
Related Questions
Mathf.pingpong 4 Answers
regular expressions 1 Answer
Video Scrubber Bar & Rewind using MovieTexture? 0 Answers
Limiting the rotation of x-axis 1 Answer
A node in a childnode? 1 Answer