- Home /
simple clamp camera translate on Y axis only
I have a script for my camera on my player so that the camera can move up and down on the y axis. I don't want the camera to move left and right because my player already rotates left and right on the x axis and the camera follows because it is the players child. Now the problem is that the camera rotates too far up or too far down which is why I need a clamp or bonds for it. I have looked all over Google and other sites but I can not find anything the fits what I'm looking for. All I need is a clamp on the y axis when input axis horizontal is pushed but I also want the camera to follow the player when it moves up and down. Thanks for your help, here's my script.
private var player : Transform;
private var speed : float = 10;
function Update() {
player = GameObject.Find("Gabriel Health Bar Red").transform;
transform.LookAt(player);
var translation = Input.GetAxis ("Mouse Y") * speed;
translation *= Time.deltaTime;
transform.Translate (0, translation, 0);
}
Answer by Jagster · Mar 11, 2014 at 10:54 PM
not sure if this is 100% what you wanted but the code will lock the camera movement along the axis you chose, just put it in a script and attach that to the camera
// Update is called once per frame
void Update () {
transform.position = new Vector2 (0, transform.position.y);
}
That's close but not quite. Let me reword it so it makes more since. On the script "mouse orbit", I want that same clamp to be exactly like that but for the y axis only. The script I showed you is just me trying to make a script similar to mouse orbit but, without the ability to move the camera left and right. I tried to edit the script "mouse orbit" to fit what I'm looking for but with my limited amount of experience I could not figure it out. SO what you could do is take the "mouse orbit" script and take out the input axis "$$anonymous$$ouse X." I would do this myself but, like I said I don't know how too and make it still work right. Thanks!
Answer by iuhfewkjbwekj · Mar 19, 2014 at 12:51 AM
Never mind I figured it out but thanks! private var player : Transform; private var speed : float = 10;
function Update() {
player = GameObject.Find("Gabriel Health Bar Red").transform;
transform.LookAt(player);
var translation = Input.GetAxis ("Mouse Y") * speed;
translation *= Time.deltaTime;
transform.Translate (0, translation, 0, Space.World);
if (Vector3.Distance(transform.position, player.position) > 8) {
transform.position += transform.TransformDirection (Vector3.fwd);
}
if (Vector3.Distance(transform.position, player.position) < 6) {
transform.position -= transform.TransformDirection (Vector3.fwd);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Clamp Mouse Zoom using Scroll Wheel (SOLVED) 2 Answers
How do I use mathf.clamp to make boundaries for a camera 0 Answers
Bizzare problem with limiting camera veritcal rotation 2 Answers
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer