- Home /
Box collider doesn't work with player object
So I have this code for the Player's movement but the object which has a Box collider doesn't interact with walls. When it hits a wall that I've made which also has a box collider it passes the wall but I want it to stop when it hits a wall. The ball that I made and all the other objects work fine with my walls but the player object doesnt. Help please! The game is in 2D by the way. Here is the code:
private var ray : Ray;
private var rayCastHit : RaycastHit;
function Update()
{
if(Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, rayCastHit))
{
transform.position.y = rayCastHit.point.y;
}
}
}
This script doesn't look like it's related to anything you're asking about... 2D and 3D colliders do not interact with each other. Use colliders in the Physics 2D section only if you want them to collider with eachother. Or only in the Physics section.
colliders also wont work unless one of the objects has a rigidbody
Yeah i understood my mistake, forgive me Iam still a beginner. :P
Could anyone help me to change this code to work with Physics 2D because all of my game works with Physics 2D and this code doesn't.
private var ray : Ray;
private var rayCastHit : RaycastHit;
function Update()
{
if(Input.Get$$anonymous$$ouseButton(0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, rayCastHit))
{
transform.position.y = rayCastHit.point.y;
}
}
}
Please don't post further questions as answers! :)
This isn't the place to ask multiple questions in one, or to ask for code to be changed, but for answers. The issue there is that you're trying to use the 3D raycast system with the Physics 2D system. Use Physics2D.Raycast.
Here's a list of the Physics2D functions available, Linecast is nice as well. http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.html