I’m extremely new to coding and have been looking through the tutorials but I can seem to find my answer. I’m trying to limit the area my character can move in a box shape but everything I’ve tried so far has not been successful. Here is the code I’m using for character movement, taken from the first game tutorial.
using UnityEngine;
using System.Collections;
public class Player1Movement : MonoBehaviour {
public float speed;
void Start ()
{
gameObject.renderer.material.color = Color.red;
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
}
}
My question is where would I add Mathf.Clamp so that I can limit the movement? Everywhere I’ve tried putting into so far has not yielded the results I want.