Hey guys/gals.
Been working my way through the program but I hit a wall. My ship is going absolutely nuts once I introduce the Mathf.Clamp function into the code. I recorded a video so you can see what I mean here:
The ship just keeps spinning around on the y axis as it goes around the circuit whenever i introduce the Mathf.Clamp function.
Here’s my code, do you see anything off?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class ShipController : MonoBehaviour
{
[Tooltip(“in ms^-1”)][SerializeField] float xSpeed = 1f;
[Tooltip(“in m”)] [SerializeField] float xRange = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
float xOffset = xThrow * xSpeed * Time.deltaTime;
float rawNewXPos = transform.localPosition.x + xOffset;
float clampXpos = Mathf.Clamp(rawNewXPos, -xRange, xRange);
transform.localPosition = new Vector3(rawNewXPos, transform.localPosition.y, transform.localPosition.z);
}
}
Thank you for your time. Your help with save the screen of a 27" iMac from complete destruction.