When I add Mathf.Clamp to the player object, and then press play to test, the player object will move a load of units to the right on the X Axis.
When I play the timeline, the object doesn’t move to the right on the x AXIS.
Here is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
[SerializeField] float controlSpeed = 10f;
[SerializeField] float xRange = 5f;
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 newYPos = transform.localPosition.y + yOffset;
transform.localPosition = new Vector3 (clampedXPos, newYPos, transform.localPosition.z);
}
}