Clamping not working

Ok so, I’ve redone my scene a bit. Took out some trees, made the animation track straigher, and changed the ball (enemy) movement to align better.

When I constraint the camera it’s off… My ship looks a little titled to begin with (probably can be fixed with a small rotation), and my position numbers do not align in the viewport to match with the 7 and 10 X.Y range. I’ve tried to modify it to work, but it still is not matching correctly (see video). Also, I still do not understand why the ship will sometimes jump offscreen when I start the game, and I have to re-click the play button again to actually make it start where it is supposed to (every time I go into play mode this happens).

I’ve tried contraining the ship at 2 and 3 and it still does not match. I’ve moved my camera back to have more room, and it still does not work as well…

Also, I do not know why my ship is not scaled like his. If I make it 111 on xyz it is huge, so it is at a .2,.4,.2 right now (don’t know if that is having an effect on the constraint or not…)…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControls : MonoBehaviour
{
[SerializeField] float controlSpeed = 10f;
[SerializeField] float xRange = 10f;
[SerializeField] float yRange = 7f;

// Update is called once per frame
void Update()
{
    ProcessTranslation();
    ProcessRotation();

}

void ProcessRotation()
{
    float pitch = 0f;
    float yaw = 0f;
    float roll = 0f;
    transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}

 void ProcessTranslation()
{
    float xThrow = Input.GetAxis("Horizontal");
    float yThrow = Input.GetAxis("Vertical");

    float xoffset = xThrow * Time.deltaTime * controlSpeed;
    float rawXPos = transform.localPosition.x + xoffset;
    float clampedXpos = Mathf.Clamp(rawXPos, -xRange, xRange);

    float yoffset = yThrow * Time.deltaTime * controlSpeed;
    float rawYPos = transform.localPosition.y + yoffset;
    float clampedYpos = Mathf.Clamp(rawYPos, -yRange, yRange);

    transform.localPosition = new Vector3
        (clampedXpos, clampedYpos, transform.localPosition.z);
}

}

Hi Daisy,

Move the ship to the edges like you did in the video. Then write down the values in your Transform. Use the values for your xRange and YRange. Since each game is unique, it might be that Rick’s value do not work for you.

Did this help?


See also:

It does, but they are usually two different numbers. They’re not even in the viewport… Right will be -1.5 and Left will be 3, same with up and down… Is there something I can do with the viewport to fix this?

I think, changes in value of xRange and yRange will work fine.

How do I list that though? I thought it could only take 1 number in the slot. Can I write a range like xRange: -1.5 - 3 and yRange: 2-4?

What’s the position of the camera in the Inspector? Ideally, it should be set to (0, 0, z). Otherwise, there will be a relative offset to the root game object. And that offset must be applied to the player because the viewport of the camera represents the area in which the ship is supposed to be moving.

Yes you can, you have to declare 2 more variables. Clamp actually returns the given value if it is within minimum range and maximum range like Mathf.Clamp(float value, float min, float max), but it is not the best way in this case. I think there is offset in camera’s or player ship’s position in inspector or timeline.

I’m still learning code lol, do you have an example of what that would look like?

I think it kinda works now that I moved my main camera FOV to 70… but it still a tiny bit rough…

I’m still learning code lol, do you have an example of what that would look like?

You have an example in your code. :wink:

See here:

[SerializeField] float xRange = 10f;
[SerializeField] float yRange = 7f;

Declare two more variables like you did in this and in previous projects. Writing code is not that difficult. We basically do the same over and over again, just with different names.

In your code, you use the xRange variable like this:

float clampedXpos = Mathf.Clamp(rawXPos, -xRange, xRange);

As you can see, you used the value of the variable twice. Use the new variable, too, to define individual min and max range values.


Did you check/fix the position of the camera as suggested in my previous answer?

Privacy & Terms