- Home /
How to Prevent a Script From Overriding Colliders?
Ok so basically I have a gameobject that follows my cursor along with some stationary ground that when released from the cursor it will land on and obey physics. Everything works fine besides when I am holding it and my cursor passes through a piece of ground. This causes it to travel into the ground when I want it to obey the colliders that are on all of the objects and just follow it as best as it can.
This is my code:
using UnityEngine;
using System.Collections;
public class CursorFollow : MonoBehaviour {
public GameObject Cam;
public int cursorSizeX;
public int cursorSizeY;
public Texture2D yourCursor;
public Texture2D altCursor;
public bool follow = false;
public bool changeMouse = false;
// Use this for initialization
void Start () {
Screen.showCursor = false;
}
// Update is called once per frame
void Update () {
//transform.position = Cam.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
follow = Input.GetMouseButton (0);
if (follow && changeMouse) {
transform.position = Cam.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
rigidbody.velocity = new Vector3(0, 0, 0);
}
}
void OnMouseEnter () {
changeMouse = true;
}
void OnMouseExit() {
if (!follow) {
changeMouse = false;
}
}
void OnGUI() {
//Screen.showCursor = !changeMouse;
if (changeMouse) {
//EditorGUIUtility.AddCursorRect(new Rect(10, 10, 100, 100), MouseCursor.Link);
//Cursor.SetCursor(MouseCursor.Link, Vector2.zero, CursorMode.ForceSoftware);
GUI.DrawTexture (new Rect (Event.current.mousePosition.x - cursorSizeX / 2, Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), altCursor);
} else {
GUI.DrawTexture (new Rect (Event.current.mousePosition.x - cursorSizeX / 2, Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), yourCursor);
}
}
}
If anyone could help it would be greatly appreciated! :)
Please take the time to format (all) your code properly.
I think transform.position is the problem, it ignores all colliders, collisions, etc.
Sorry about the format, had to do it really quickly and honestly don't know how.
But anyway do you know how to make something work in the same way without transform.position?
Look at $$anonymous$$oveTowards, Translate (might have same issue w/ colliders, can't recall)
You need to take a look standard DragRigidbody.js script. You can get it by:
Assets > Import Package > Scripts
It only needs to go on one object. I would create a sample scene with a plane and some boxes. Attach the script to the camera or an empty game object, then play in the scene. This script likely isn't a whole solution, but it will give you some ideas on how you can drag an object in the scene and have it obey the colliders.
I tried that but it still isn't following the colliders, do I have to tag anything in the scene or set layers?
Answer by wibble82 · Mar 14, 2014 at 08:17 AM
Hi there
So the problem you're gonna hit here is that in its basic form, you are simply overiding the physics every frame when holding the object. So it doesn't matter whether the physics engine wants to resolve the collision - you're are overriding any attempts to do so!
This actually turns out to be a not-entirely-trivial issue in any game. Typically you have 2 options:
try to predict if putting the object where you want it to be would cause an intersection, and if so either refuse to move it or even try and guess a better place.
work with the physics engine constraints system to move your object
Generally, the 2nd option works better because you want something sensible to happen when dragging the object against something - for example pushing your object onto the floor then moving your mouse should drag the object along the floor.
So how to do this? Well whether its unity development or low level c++ on a console, I typically:
create an invisible kinetic rigid body that 'follows' the mouse cursor perfectly. This needs no collision whatsoever.
create a fixed joint between your invisible body and the body you're moving that tries to get them line them up in the position/orientation that matches how you grabbed it.
the joint can have 'maximum forces' it is allowed to apply, and breaking point. You can even play with things like spring joints for better effect
By using this 'ghost body' plus constraint approach you're effectively asking the physics to do the complicated maths for you! This means it'll not only give you good results, but because you're properly working with the physics, stuff like velocity and interaction with other dynamic objects will work!
Shout if you need a help with this as actual code, although there's plenty of examples online for 'creating joints at run time'.
Note: Your 'moving of the rigid body' should be done in FixedUpdate so it runs at the same speed as the physics!
Hi wibble,
Thanks for that! That is a much better way than what I had previously! If you don't $$anonymous$$d though could you help me out on enabling and disabling the spring joint? Because I only want the cube to follow my cursor when I am clicking.
Cheers! :D
Your answer
