Hello I am trying to make my character move in a specified area and no further so that they would stay in the view of the camera. The things I think to know about the game I’m trying to make, so that it might help us better solve this, is that the playercharacter is childed to an empty game object that moves forward and the playercharacter is free to move anywhere under that forward-moving-empty-game object-parent.
The specific codes I am trying to use to limit the player from just moving out of sight is this:
private float minX = -15.0f;
private float maxX = 15.0f;
private float minZ = 0.0f;
private float maxZ = 20.0f;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), 0,
Mathf.Clamp(transform.position.z, minZ, maxZ));
And when I try implementing this code into my playerMovement Script the playercharacter cannot move further than the input float variables as expected, but unexpectedly will be stopped by an invisible force that I cannot imagine why. The player can still move left or right but cannot move forward and is pushed back to what appears to be the value where I set the player could move furthest back. If someone could give me advice/help here that would be much appreciated. Here is the code:
public class PlayerMovement : MonoBehaviour
{
public float movementSpeed = 10.0f;
public float angleChangeSpeed = 100.0f;
public bool isDead = false;
**private float minX = -15.0f;
private float maxX = 15.0f;
private float minZ = 0.0f;
private float maxZ = 20.0f;**
// Update is called once per frame
void Update ()
{
if(!isDead)
{
**transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), 0,
Mathf.Clamp(transform.position.z, minZ, maxZ));**
// new code
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
Vector3 direction = new Vector3 (horizontal, 0, vertical);
Vector3 finalDirection = new Vector3 (horizontal, 0, 1.0f);
transform.position += direction * movementSpeed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (finalDirection), Mathf.Deg2Rad*angleChangeSpeed);
}
}
void SetDead(bool dead)
{
Debug.Log ("SetDead was reached");
isDead = dead;
}
}