I have used the if else statements below to limit by player movement though it seems to jump to the limit when it get near 6 or -6 which is distracting. ( I have commented out that code below)
But I thought I would try the same think using the MathF.Clamp. But the code I have written for movement only between 6 and -6 makes two players appear that are shaking at both 6 and -6?
Thank you,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move1 : MonoBehaviour
{
public int speed = 1;
// Start is called before the first frame update
void Start()
{
//transform.position = new Vector3(0, 4, 0);
}
// Update is called once per frame
void Update()
{
float moving = (speed * Time.deltaTime);
transform.Translate(moving * Input.GetAxis("Horizontal"),
moving * Input.GetAxis("Vertical"), 0);
restrict();
}
void restrict()
{
transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, 0,-6),0);
/*if (transform.position.y>= 5)
{
transform.position = new Vector3(transform.position.x, 5, 0);
}
else if (transform.position.y<-6)
{
// transform.position = new Vector3(transform.position.x, -6, 0);
*/
}
}