- Home /
game object ignores collider
var hit : RaycastHit; var raypoint : Vector3; var getcoor = false;
function Update(){ if(Input.GetButtonDown("Fire1")) getcoor = true; if(Input.GetButtonUp("Fire1")) getcoor = false;
if (getcoor)
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition), hit))
{
raypoint = hit.point;
}
}
transform.position = Vector3.Lerp(transform.position, hit.point, Time.deltaTime);
} I am using this code to move my game object around on a plane. The code let me move my game object wherever I click. But the problem is, it (the game object) ignores anything that i put on the plane (e.g. going through colliders and etc...). Does anyone knows how to make the physics work? p.s: FYI, FixedUpdate() won't work too.
Answer by aldonaletto · Jul 22, 2011 at 06:11 AM
If your object is not a Character Controller, just add a rigidbody and it will collide with other objects (I've just tested the script in my project). The default rigidbody (with Use Gravity checked and Is Kinematic unchecked) is OK.
EDITED: The Character Controller must be moved with Move or SimpleMove in order to detect collisions. I modified your original script to use a character controller, and cut out some redundancies (one Raycast instead of two, GetButton instead of GetButtonDown/GetButtonUp, etc.). Now it detects collisions, and also has a fake gravity (a constant down velocity instead of a real acceleration). The variable speed adjusts the moving speed (the actual velocity will be also proportional to the distance):
var hit : RaycastHit; var raypoint : Vector3; var speed = 10; // move speed var gravity = 10; // fake gravity
private var character: CharacterController;
function Start(){ character = GetComponent(CharacterController); raypoint = transform.position; }
function Update(){ if(Input.GetButton("Fire1")){ var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast(ray, hit)) { raypoint = hit.point; } } var dir = raypoint - transform.position; dir.y = -gravity; // applies a fake gravity character.Move(dir Time.deltaTime speed); }
it is a character controller, and i tried to add a rigidbody but it still go through anything else on the place, and moreover, when i move too much, the game object will go through the plana and fall down...
I edited my answer to use a Character Controller, and added a fake gravity.
Your answer
Follow this Question
Related Questions
Ignore Collider 0 Answers
Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers
Multiple Colliders Trigger problem 2 Answers
Change a GameObject's collider 1 Answer
Get collider from raycast 4 Answers