I have a fairly simple movement control, Playership moves left and right based on Input.GetAxis(“Horizontal”)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed = 15f;
private float xMax = -7.5f;
private float xMin = 7.5f;
void Update ()
{
float movement = (speed * Input.GetAxis("Horizontal")) * Time.deltaTime;
transform.Translate(1 * movement, 0, 0);
}
}
I wish to clamp the x movement to xMIn and xMax using Mathf.Clamp. I’ve tried quite a few variations, and the best i get is some odd results. I thought the following might work, but not luck.
void Update ()
{
float movement = (speed * Input.GetAxis("Horizontal")) * Time.deltaTime;
// transform.Translate(1 * movement, 0, 0);
Vector3 pos = transform.position;
pos.x = Mathf.Clamp(transform.position.x, xMin,xMax);
transform.Translate(pos.x * movement, 0, 0);
}
Could use a hand with this. Thanks in advance.
B-