Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Game Boundaries Script

Discussion in 'Scripting' started by Hannah_emmm, Dec 19, 2020.

  1. Hannah_emmm

    Hannah_emmm

    Joined:
    Dec 17, 2020
    Posts:
    6
    Hi everyone,
    I'm trying to add boundaries to my screen so my character doesn't fall off the edge of the game.
    This is the code I'm trying to get to work and the error I'm receiving is "no overload for method clamp takes two arguments". I added in floats as it wouldn't let me put the positions in otherwise, but not sure how to get it to work? I'm very new to coding so please be nice :p

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Boundaries : MonoBehaviour
    6. {
    7.     public float gameBoundaryX = -0.41f;
    8.     public float gameBoundaryY = 4.12f;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         transform.position = new Vector3(Mathf.Clamp(transform.position.x, gameBoundaryX),
    19.             Mathf.Clamp(transform.position.y, gameBoundaryY), transform.position.z);
    20.     }
    21. }
     
  2. neco777

    neco777

    Joined:
    Aug 30, 2014
    Posts:
    6
    Clamp takes three arguments. You probably meant something like this
    Code (CSharp):
    1.         transform.position = new Vector3(Mathf.Clamp(transform.position.x, 0, gameBoundaryX),
    2.             Mathf.Clamp(transform.position.y, 0, gameBoundaryY), transform.position.z);
    3.