- Home /
Colliders not working
im currently working my way through the recently released "Classic Game Design" and for some reason the colliders just aren't working. This is the 2nd game in the book and they didn't work for the first one either (which was pong). The paddle just ends up clipping through the boundaries when you move it towards them. All of the objects have a box collider so i have no idea what could be causing this and whether or not this was an intentional decision from the author.
screen:
Answer by MrProfessorTroll · Sep 02, 2013 at 05:00 AM
Updated answer :)
public class PlayerScript : MonoBehaviour
{
void Start ()
{
Screen.lockCursor = true;
}
void Update ()
{
if(Input.GetKey("left"))
{
transform.Translate(-20 * Time.deltaTime, 0, 0);
}
if(Input.GetKey("right"))
{
transform.Translate(20 * Time.deltaTime, 0, 0);
}
float h = 30.0f * Time.deltaTime * Input.GetAxis ("Mouse X");
transform.Translate (h, 0, 0);
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -5f, 5f), transform.position.y, transform.position.z);
}
void OnTriggerEnter (Collider other)
{
BallScript.yspeed = -BallScript.yspeed;
if(other.collider.gameObject.transform.position.x > gameObject.transform.position.x)
{
BallScript.xspeed = Mathf.Abs(BallScript.xspeed);
}
else
{
BallScript.xspeed = - Mathf.Abs(BallScript.xspeed);
}
BallScript.collflag = true;
}
}
yes but the ball isn't the problem. Everything works like its supposed to. The only problem is that when i move the paddle left or right or it travels straight through the field's borders.
Ok, then do you know about this: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html
It clamps the position of anything. Do you need an example script?
i don't think its time based but i should be more specific. when i press left or right the paddle translates perfectly along the X axis but when it hits a border it goes straight through ins$$anonymous$$d of stopping.
Yes, that is what you need. In the example they use time. That is what they are limiting. Here, try this:
transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, -5f, 5f), transform.position.y, transform.position.z);
You can change '5' and '-5' to different values. Also, this is in C#. Do you need a Javascript version? Also, please trust me on this. Just plug in the code and keep moving until it stops.
And, put this on your paddle script AFTER you finish moving it.
Updated the answer :), you might have to change the '5' and '-5' to get the right number. An easy way is to play the game, move your paddle to a point where you want it to stop, record the X position, then replace it for '5' and '-5'