Search Unity

Clamp vs Accelerometer

Discussion in '2D' started by Fasertio41, Sep 15, 2017.

  1. Fasertio41

    Fasertio41

    Joined:
    Sep 13, 2017
    Posts:
    8
    Hello everyone! I'm trying to create a limit for my player. The player is moved with accelerometer but when i'm trying to use mathf.clamp it's not work =(, this is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Aeroplano : MonoBehaviour {
    6.     // Use this for initialization
    7.     [SerializeField]
    8.     private float LimiteSx;
    9.     [SerializeField]
    10.     private float LimiteDx;
    11.  
    12.     private float Speed = 40f;
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.         transform.Translate(Input.acceleration.x * Time.deltaTime * Speed, 0, 0);
    21.        
    22.        
    23.         /*transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -2.82f, 2.82f), transform.position.z);
    24.        
    25.         Vector3 ClampedPosition = transform.position;
    26.         ClampedPosition.y= Mathf.Clamp(transform.position.x, -2.82f, 2.82f);
    27.         transform.position = ClampedPosition; */
    28.     }
    29. }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    In your commented code you've used "transform.position.x" but I think you meant "transform.position.y".
     
  3. Fasertio41

    Fasertio41

    Joined:
    Sep 13, 2017
    Posts:
    8
    Why?
    My player move only on the x axis so the y is 0... so I modified the code but don't work...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Aeroplano : MonoBehaviour {
    6.     // Use this for initialization
    7.     [SerializeField]
    8.     private float LimiteSx;
    9.     [SerializeField]
    10.     private float LimiteDx;
    11.  
    12.     private float Speed = 40f;
    13.     void Start () {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.      
    20.         transform.Translate(Input.acceleration.x * Time.deltaTime * Speed, 0, 0);
    21.      
    22.      
    23.         //transform.position = new Vector3(Mathf.Clamp(transform.position.x,-2.82f, 2.82f), transform.position.y,  transform.position.z);
    24.      
    25.         Vector3 ClampedPosition = transform.position;
    26.         ClampedPosition.x= Mathf.Clamp(transform.position.x, -2.82f, 2.82f);
    27.         transform.position = ClampedPosition;
    28.     }
    29. }
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Code (CSharp):
    1. ClampedPosition.y = Mathf.Clamp(transform.position.x, -2.82f, 2.82f);
    In this line in your first example you were setting the Y position by clamping the X position.

    You never actually explained how your game works so I had no idea what you were trying to do, I was just pointing out a logical error.

    What is supposed to be happening? Your player only moves on the X axis? Which value are you trying to clamp?
     
  5. Fasertio41

    Fasertio41

    Joined:
    Sep 13, 2017
    Posts:
    8
    yeah sorry :(, so my character is moving on the x axis and i try to clamp two x values for limit the moving of my character
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    What you have there should be properly clamping the X position. Are you seeing it not clamp on the X, or are you seeing movement on the Y?
     
  7. Fasertio41

    Fasertio41

    Joined:
    Sep 13, 2017
    Posts:
    8
    Nope, I'm not seeing movement on the y but on the x axis the character move free =( (the clamp create a range on the x axis where the character move in, so it don't work)
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I dont understand, in your script you aren't applying any offset to the Y, and you are in fact clamping the X. Is this script actually being applied to the object? Try printing out position with Debug.Log to make sure it's actually running that code.