So? My ships position just changed and is now not centered in the camera

So appearently after typing the clamped code my ships position from the camera completely changes when i hit play. im not sure if it has anything to do with that my ships starting position is -72. its not really something ican change but im guessing thats the reason and it has to range between -62 and -82 but im not too sure i thought doing 10 would work but it would just snap it to 10 instead of clamping it

MY CODE;

[SerializeField] float xRange = 5f;

[Tooltip("In ms per second")][SerializeField] float xSpeed = 4f;


void Start()
{
    
}


void Update()
{
    float xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
    float xOffset = xSpeed * xThrow * Time.deltaTime;

    float rawXPos = transform.localPosition.x + xOffset;
    float xClamped = Mathf.Clamp(rawXPos, -xRange, xRange);

    transform.localPosition = new Vector3(xClamped, transform.localPosition.y, transform.localPosition.z);

    
}

}

Edit: alright so i found the root of the problem but i dont understand why. So when i put in the xClamped variable into the new Vector3 thats when my ships entire x positio changes from its path but i dont understand why i just know thats why.

Solution! for me.
So it seems like since my ship didnt start at 0 in the world like bens i can’t exaclty use one number the way he showed in the video. im sure its possible for me some other way but i just ended up hard coding the numbers into the clamp instead of making it -xrang and xrange. Hopefully not making a variable wont ruin anything in future videos.

my new code!

{
[Tooltip(“In ms per second”)][SerializeField] float xSpeed = 4f;

void Start()
{
    
}


void Update()
{
    float xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
    float xOffset = xSpeed * xThrow * Time.deltaTime;

    float rawXPos = transform.localPosition.x + xOffset;
    float ClampedXPos = Mathf.Clamp(rawXPos, -82f, -62f);

    transform.localPosition = new Vector3(ClampedXPos, transform.localPosition.y, transform.localPosition.z);

    
}

}

Not Solution as i thought would happend later on doing this type of solution would only mess things up later in the videos because now im having doing the pitch, yaw , roll since my clamp isnt like in the video and the numbers are hard coded im most likely going to have to make a variable like in Bens video in order to move on instead od doing Mathf.ClampXpos = (rawXPos, -82f, -62f);

Twas a problem with my prefabs got it figured out. i had ship prefab inside of a player prefab lol

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms