hi!
i was trying yo use the mathf. clamp to restrict the movement, but instead of restrict the position according to the local position, it radically move it to another place of the map ( with the coordinates of the range). i have checked the timeline and the code, and the problem is in the matf.clamp function and i still dont know exactly why it doesnt work
thanks in advance
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[SerializeField] float speedControl;
[SerializeField] float xRange = 5f;
[SerializeField] float yRange = 3.5f;
// Update is called once per frame
void Update()
{
//PlayerControls//
float horizontalMovement = Input.GetAxis("Horizontal");
float verticalMovement = Input.GetAxis("Vertical");
//Movement//
float xOffset= horizontalMovement * Time.deltaTime * speedControl;
float xPos = transform.localPosition.x + xOffset;
float clampedX = Mathf.Clamp(xPos, -xRange,xRange);
float yOffset = verticalMovement * Time.deltaTime * speedControl;
float yPos = transform.localPosition.y + yOffset;
float clampedY = Mathf.Clamp(yPos, -yRange,yRange);
transform.localPosition = new Vector3( clampedX, clampedY,transform.localPosition.z);
}
}