Hello, at a loss here, hoping for some help.
Argon Assault - Mathf.Clamp() video
I am following along with Rick but running into a problem when I try to implement the Mathf.Clamp() portion. When I do this and Play, my ship teleports to the origin of the scene (plus or minus the xRange & yRange values). It doesn’t seem to be using the ship’s local position to make the change, it teleports to origin and starts the run from there.
I have a feeling it has something to do with the prefab but not sure. I looked into resetting the prefab’s origin but in the prefab itself its set to (0,0,0). I also tried using a totally different prefab and it still teleports to the origin first.
While troubleshooting I threw out all my code and tried using Rick’s but it still does this. Just for reference here’s Rick’s code that’s also not working for me:
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;
void Update()
{
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);
}
}