- Home /
Mathf.Clamp Jumping
I have this script below, I attached it to the gameobject called Paddle, so I am trying to set it so the paddle wont go outside the table, but when I do what I am doing, using Mathf.Clamp();, I get this when I run the game, it is switching from the maxX position to the minX position, I don't know why this is happening, maybe someone can help me with this? Or maybe tell me how to do this instead.
Thanks in advance.
using UnityEngine;
using System.Collections;
public class PaddleController : MonoBehaviour {
public float Distance = 10;
public float maxPosZ = -3.20087f;
public float minPosZ = -5.764705f;
public float maxPosX = -0.224046f;
public float minPosX = -2.940397f;
// Update is called once per frame
void Update () {
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 pos = r.GetPoint(Distance);
Vector3 tPos = transform.position;
pos.x = Mathf.Clamp(tPos.x, minPosX, maxPosX);
pos.y = 2.3f;
transform.position = pos;
//pos.x = Mathf.Clamp(transform.position.x, minPosX, maxPosX);
}
}
Use the following to check what you are feeding to $$anonymous$$athF.Clamp
debug.log(tPox.x);
I have a suspicion the clamp is fine and your ray is messing up.
I'd say it's the Ray rather than the clamp too.
If it's 2D top down all the time I might use Camera.screenToWorldPoint ins$$anonymous$$d of a ray
Your answer
Follow this Question
Related Questions
How to make object move smoothly. 3 Answers
Restricting rotation on an axis 0 Answers
Mathf.Clamp having weird effects on player object with rigidbody 1 Answer
Use yAxis as a Vector3(0,0,yAxis) paramenter? 3 Answers
Relative Rotation 1 Answer