- Home /
How can I limit my 2D player movement along the Y-axis?
Hello all, I would like to limit my player movement along the Y-axis at certain Y coordinates. I've tried Mathfclamp but cant seem to get it to work properly with my current code. Here is my player movement code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class MoveScript : MonoBehaviour {
 float directionY;
 public float moveSpeed = 5f;
 Rigidbody2D rb;
 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody2D> ();
 }
 // Update is called once per frame
 void Update () {
     directionY = CrossPlatformInputManager.GetAxis ("Verticle");
 }
 void FixedUpdate()
 {
     rb.velocity = new Vector2 (rb.velocity.x, directionY * moveSpeed);
 }
}
Answer by sean244 · Apr 13, 2018 at 07:31 PM
You misspelled 'Vertical". Try this
 void FixedUpdate()
 {
     directionY = CrossPlatformInputManager.GetAxis("Vertical");
     transform.position = new Vector3(transform.position.x, Mathf.Clamp( transform.position.y, -10f, 10f), transform.position.z);
     rb.velocity = new Vector2(rb.velocity.x, directionY * moveSpeed);
 }
Hey thanks man, that's what I was looking for, I was using mathf.clamp wrong!
Your answer
 
 
             Follow this Question
Related Questions
How can I deal with multiple if-statements? 2 Answers
Unity2D moving on the y-axis 0 Answers
W key to move doesnt work,Movement Script doesnt do anything when i press w 0 Answers
Input.GetAxisRaw doesnt read Keyboardinput 1 Answer
Is there a way to make movement feel movement not feel slippery? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                