- Home /
how can i restrict Ball movement in -ve X axis ......?
hi,
I'm trying a ball game. Here i have some problem i want to restrict my ball movement on -ve x axis. and also i tried Mathf.clamp. but did't work for me.
transform.position = new Vector3(Mathf.Clamp(transform.position.x,5,500),transform.position.y,transform.position.z)
i need update my MinX value in with screen size. Please suggest me
Answer by ironblock · Mar 16, 2015 at 10:48 AM
the clamp does need to be in the update. if it is, i dont know whats wrong. but his usualy works for me.
using UnityEngine;
using System.Collections;
public class limitPlayer : MonoBehaviour {
public float minimumX = -10f;
public float maximumX =10f;
public float minimumY = -9.8f;
public float maximumY =9.8f;
public float minimumZ = -20f;
public float maximumZ =20f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position = new Vector3 (Mathf.Clamp (transform.position.x, minimumX, maximumX), Mathf.Clamp (transform.position.y, minimumY, maximumY), Mathf.Clamp (transform.position.z, minimumZ, maximumZ));
}
}
what do you need ? you can change the float values. so it will fit in your game.i usualy use a sepperate script to clamp the player so it can be used with differend controllers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613190200im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Game crashes as soon as splashscreen is displayed in android 3 Answers
Ball Falling Game. 2 Answers
Creating Simple Peg Solitaire Game in unity. 0 Answers
How do I fix these two errors in my Number Wizard Game? 2 Answers
Custom Cursor for my Game 1 Answer