I have been trying for a few days to create a way to stop my rigibody/sphere collider mesh to not go past a plane collider. The mallet uses a raycast script which says if its hitting a collider then transform position here is the code:
private var ray : Ray;
private var hit : RaycastHit;
function Start () {
// Hide the cursor
rigidbody.Sleep();
Screen.showCursor = true;
}
function Update () {
rigidbody.Sleep();
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
rigidbody.WakeUp();
transform.position.x = hit.point.x;
transform.position.z = hit.point.z;
}
}
I though this would prevent the object from leaving the table but it doesn’t the object can get through the table and then stops i tried making separate colliders which ignore raycast to stop the mallet but it doesn’t work any suggestions?
Hmm, from my newb reading it looks like you’re checking to see if the mouse cursor is colliding with something (anything - you’re not checking what it hits). If that happens, you’re setting the x and z coords to be the position of the object the cursor was over. Is that right?
The current code makes it so the mallet object is at the position of the cursor only when the ray cast hits the tables collider (a plane collider) other wise its not meant to do anything (all other colliders are set to ignore raycast) the mallet goes past the collider of the table im not sure how its doing it if the code only moves the postions depening on collision with tables collider
Is there a collider defined outside the table? You’re not checking that the RayCastHit hits the table (it could be hitting any collider), so if it hits something else the mallet will go to that spot.
i have managed to fix the issue but the mouse can leave the table and then has to return to the table to move the object again. i want the cursor to not be visible so it looks like the plaer is only moving the mallet. is there a way to not use raycast but to move the mallet by only iputting movment on mouse axes so
when mouse moves left mallet moves left untill it hits the stop point. but as soon as the player moves mouse to the right mallet beings to move to the right so there is no lag time for the mouse to get back to the table.
oh and heres the current code:
#pragma strict
private var ray : Ray;
private var hit : RaycastHit;
function Start () {
// Hide the cursor
Screen.showCursor = false;
}
function Update () {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
transform.position.x = hit.point.x;
transform.position.z = hit.point.z;
if(transform.position.z > 188.6688)
transform.position.z = 188.6688;
if(transform.position.z < 174.7648)
transform.position.z = 174.7648;
if(transform.position.x > 176.3533)
transform.position.x = 176.3533;
if(transform.position.x < 145.9523)
transform.position.x = 145.9523;
}
}