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

(UNSOLVED) Mathf.Clamp to screen help

Discussion in '2D' started by malachiddm, Jan 17, 2015.

  1. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    I've been at this a few days. and others have told me to try certain things and nothing has worked so far. This is my first game and what I'm trying to do is use the accelerometer to move my gameobject along the x axis just left and right. I finally figured out how to do that but I can't get Mathf.Clamp to clamp my code to the screen size.
    If anyone can solve this problem it would make my life. I'm new to Unity and coding so that's why its going over my head.




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HatController : MonoBehaviour {
    5.  
    6. public Camera cam;
    7.  
    8. private float maxWidth;
    9. private bool canControl;
    10.  
    11. // Use this for initialization
    12. void Start () {
    13. if (cam == null) {
    14. cam = Camera.main;
    15. }
    16. canControl = false;
    17. Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
    18. Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
    19. maxWidth = targetWidth.x;
    20. }
    21.  
    22. // Update is called once per frame
    23. void Update () {
    24. if (canControl) {
    25. transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
    26. Vector3 rawPosition = Input.acceleration;
    27. Vector3 targetPosition = new Vector3 (rawPosition.x , 0.0f, 0.0f);
    28. float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);
    29. targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
    30.  
    31. }
    32. }
    33. public void ToggleControl (bool toggle) {
    34. canControl = toggle;
    35. }
    36. }
     
  2. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,055
    You dont seem to be writing back the clamped value to the transform, ie transform.position = targetPosition;
     
  3. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    So how would I go about that? in line 28 do I add something?
     
  4. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    I use matfh.clamp in this way
    Code (CSharp):
    1. //Lock the position in the screen by putting a boundaries
    2.         rigidbody2D.position = new Vector2
    3.             (
    4.                 Mathf.Clamp (rigidbody2D.position.x, boundary.xMin, boundary.xMax),  //X
    5.                 Mathf.Clamp (rigidbody2D.position.y, boundary.yMin, boundary.yMax)     //Y
    6.             );
    7.     }
    I making a 2d game but the idea is the same
     
  5. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    So do I delete this line? Either way I will let you know if it works when I get home from work. Thank you!
    1. float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);?
     
  6. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    You should replace the 2d definition with rigidboy and i think that You use Vector3
     
  7. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    So I changed the code around and its giving me this error
    Assets/scripts/HatController.cs(29,34): error CS1729: The type `UnityEngine.Vector2' does not contain a constructor that takes `1' arguments


    this is my new code btw
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HatController : MonoBehaviour {
    5.  
    6.     public Camera cam;
    7.  
    8.     private float maxWidth;
    9.     private bool canControl;
    10.     public float xMin, xMax;
    11.     // Use this for initialization
    12.     void Start () {
    13.         if (cam == null) {
    14.             cam = Camera.main;
    15.         }
    16.         canControl = false;
    17.         Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
    18.         Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
    19.         maxWidth = targetWidth.x;
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         if (canControl) {
    25.             transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
    26.             Vector3 rawPosition = Input.acceleration;
    27.             transform.position = new Vector2(
    28.                 Mathf.Clamp (transform.position.x, xMin, xMax)
    29.                 );
    30.        
    31.     }
    32. }
    33.     public void ToggleControl (bool toggle) {
    34.         canControl = toggle;
    35.     }
    36. }