public class Selection_Control : MonoBehaviour {
// Variable to hold a selected gameobject
public GameObject activeObject;
// Update is called once per frame
void Update () {
// Delcare a ray from the camera to the mouse input
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Get the mouse position
Vector3 mousePos = Input.mousePosition;
// Right click to select a game object.
if (Input.GetMouseButtonUp ( 1 ) )
{
// Ignores gameobjects that are tagged as "Static"
if(Physics.Raycast(ray, out hit, 100) && !hit.collider.gameObject.CompareTag("Static"))
{
// Draw the ray
Debug.DrawLine(ray.origin, hit.point, Color.green);
// Assign the hit.collide.gameobject as out acitve object
activeObject = hit.collider.gameObject;
Debug.Log( "Selected " + activeObject );
}
else
{
Debug.Log("This is a static object");
}
}
//Check to see if the active object still under the mouse
if(Physics.Raycast(ray, out hit, 100) && hit.collider.gameObject == activeObject)
{
// Draw the ray
Debug.DrawLine(ray.origin, hit.point, Color.green);
// Check to see if the left mouse button is held down.
if ( Input.GetMouseButton ( 0 ) )
{
// Update the active objects position with that of the mouse's position
activeObject.transform.position = Camera.main.ScreenToWorldPoint( new Vector3( mousePos.x, mousePos.y, 5.6f ) );
}
}
}
}
I was messing around and made this script to allow selection and movement of objects via the mouse. It works fine with objects that don’t have a rigid-body component but those that do have one behave strangely.
When I move the object around with a rigid-body after releasing the object it will sometimes fall through the terrain for no reason. I have checked and both the terrain and the object have colliders on them.
If it touches another object it will shoot off in a random direction and rotation, sometimes it will do this even when it doesn’t hit anything else.
What am I doing wrong here ?
Thanks