# C# How do I restrict camera movement in the x-axsis Unity?

## Recommended Posts

Posted (edited)

Hello,

I am trying to create a smooth camera movement script. I have been able to get the smooth movement that I wanted but the gameobject that the camera follows moves forwards as well as bounces up and down. I only want the camera to follow the gameobject when it is moving forwards and not when the gameobject is bouncing. I am looking for a way to have it where I can restrict the movent of the camera along the x-axis. Here is the script that is on the camera

public Transform target;

public float smoothSpeed = 0.125f;

public Vector3 offset;

private void FixedUpdate()
{
Vector3 desiredPosition = target.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;

}

If anybody has any suggestions that would be great!

Thanks!

Edited by Cuber01

##### Share on other sites

You mentioned restricting motion along the x axis. I'm not sure exactly what you mean by that (unless the x axis is up in your environment), but what I gather from the rest of your post is that you want the camera to follow the target around, but not 'bounce up and down' with the target, as it were.

If that's the case, and if aside from bouncing the target always has the same vertical position (i.e. elevation), you could just set the vertical component of desiredPosition as appropriate before applying the lerp, or alternatively set the vertical component of the camera transform position after applying the lerp.

If that's not clear, here are a few questions that might help clarify things:

- What axis is up in the environment?

- If it's not 'x', what do you mean by restricting motion along the x axis?

- Aside from bouncing, does the target always have the same vertical position? Or can it move vertically independently of the bouncing?

##### Share on other sites

You say you just want the x? Well, target.position has all 3 components (x, y, and z).  So if you just want the x of the target.position, take just the x.

private void FixedUpdate()
{
Vector3 desiredPosition = offset;
desiredPosition.x = desiredPosition.x + target.position.x
Vctor3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
}

- Eck

##### Share on other sites

4
1 hour ago, Zakwayda said:

You mentioned restricting motion along the x axis. I'm not sure exactly what you mean by that (unless the x axis is up in your environment), but what I gather from the rest of your post is that you want the camera to follow the target around, but not 'bounce up and down' with the target, as it were.

If that's the case, and if aside from bouncing the target always has the same vertical position (i.e. elevation), you could just set the vertical component of desiredPosition as appropriate before applying the lerp, or alternatively set the vertical component of the camera transform position after applying the lerp.

If that's not clear, here are a few questions that might help clarify things:

- What axis is up in the environment?

- If it's not 'x', what do you mean by restricting motion along the x axis?

- Aside from bouncing, does the target always have the same vertical position? Or can it move vertically independently of the bouncing?

Sorry for the confusion I meant to put the y-axis as the one that is up. The ball bounces up and down along the y-axis. Yes, currently the camera is following the ball up and down as it is bouncing. The bouncing ball always has the same height there is no way for the ball to gain elevation independently. Are you able to show an example of how I would set the vertical component of desiredPosition. I understand what you are saying but I unsure on how to go about implementing that.

Thanks

20 minutes ago, Eck said:

You say you just want the x? Well, target.position has all 3 components (x, y, and z).  So if you just want the x of the target.position, take just the x.


private void FixedUpdate()
{
Vector3 desiredPosition = offset;
desiredPosition.x = desiredPosition.x + target.position.x
Vctor3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
}

- Eck

I tried what was suggested here but it results in the camera not following the ball at all. It no longer follows the bouncing that is in the y-axis but it also doesn't follow the ball in the x-axis and the z-axis. Is there something else that I can put so that it will follow the ball in the z-axis without following the bouncing that the ball does?

Thanks!

##### Share on other sites

If it isn't following the ball at all, my guess is target may not be set. If you want to also follow in the z direction, add in the z components the same way we did for the x components. The second line of the function where we set desiredPosition.x = desiredPosition.x + target.position.x is where we copy over the x component. Try to figure out what the line is for the z component on your own.

If you can't figure that out on your own, you may want to take a step back from game development and focus your learning back on just programming. This isn't a slam on your abilities. We were all new at one point.

- Eck

##### Share on other sites
14 minutes ago, Eck said:

If it isn't following the ball at all, my guess is target may not be set. If you want to also follow in the z direction, add in the z components the same way we did for the x components. The second line of the function where we set desiredPosition.x = desiredPosition.x + target.position.x is where we copy over the x component. Try to figure out what the line is for the z component on your own.

If you can't figure that out on your own, you may want to take a step back from game development and focus your learning back on just programming. This isn't a slam on your abilities. We were all new at one point.

- Eck

Ahh, I see now. I was able to figure it out. Thanks for the help

## Create an account

Register a new account

• 18
• 26
• 11
• 21
• 16