- Home /
What is causing this to happen?
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
Answer by alpaca of zion · Sep 22, 2012 at 08:06 PM
Gravity happens on the y axis. You aren't constraining that axis so objects can move below the terrain.
When you move a game object by specifying an absolute position like this, the physics engine has to move things out of the way to make space for your object. If they are overlapping much this can create a very strong push away. Also, I think a velocity is calculated from the movement.
Turning on and off Is Kinematic when you start and stop moving it may do the trick.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Can't get a laser working properly. 2 Answers
CharacterController had no enabled property 1 Answer
Creating a collider in a gameObject using only code 1 Answer