Hi, I have tried searching online for this but can’t find any solutions. After implementing the code and once I hit play my ship disappears. When go back to the code and comment out the clamping code and then hit play my ship stays but I am unable to move it (obviously). Does anyone know why my ship is disappearing once I hit play?
I even tried using the newer input method but that gave me the same issue.
Here is my code (and thank you in advance for any help):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
[SerializeField] float controlSpeed = 10f;
[SerializeField] float xRange = 5f;
[SerializeField] float yRange = 3.5f;
// Update is called once per frame
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);
}
}