- Home /
Collision not working.
Hi, I've spent about 3 hours trying every single possible way of doing this but nothing seems to work. I have an object with a mesh collider and rigidbody. Iam moving this object on Z with 2 touch controls (android platform) Script for moving:
var leftcontrol : GUITexture;
var rightcontrol : GUITexture;
function awake(){
leftcontrol = GameObject.Find("GUIleft").guiTexture;
leftcontrol = GameObject.Find("GUIright").guiTexture;
}
function Start () {
}
function Update () {
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Stationary && leftcontrol.HitTest (touch.position)){
transform.position.z -= 10;
}}
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Stationary && rightcontrol.HitTest (touch.position)){
transform.position.z += 10;
}
}
}
and then there are 2 objects with box colliders that are supposed to block the object from moving too far but it just isnt working. The Collision isnt working by itself so I tried adding this script but it isnt working either.
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "lWall"){
transform.position.z += 10;
}
if(theCollision.gameObject.name == "rWall"){
transform.position.z -= 10;
}
}
Any suggestions?
I believe the way it's executing, if you're still holding the button it will keep adding 10 to the z axis position. In your collisionEnter/Exit functions have a bool set to true and false, and in the Update if that bool is false as well as the input add 10 to the position.
Oh yeah and attach a collider set as a trigger to your character and attach the collision enter/exit checking script to it. The script is essentially checking if it collided with itself, if you attach it to the wall. Also I would just tag each wall as "Wall" in the inspector and check the tag. You'll have a much shorter script.
If you're not sure if OnCollision is working you can place a Debug command to log the name of collided objects. (Don't leave it in release build!): Debug.Log(theCollision.gameObject.name);
I think if your setting position directly on the object it can pass through physics...you can try moving the object via addforce: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
Answer by aldonaletto · Jan 01, 2013 at 05:15 AM
Moving the object by setting its transform.position (or using Translate) fools the collision system. You should move a rigidbody by adding forces or setting its velocity, but it would be easier to use a CharacterController instead. Unfortunately, the CharacterController has a capsule collider always aligned to the Y axis - you can't change it to a box collider, nor tilt it, but can control its height and radius, and even transform it in a sphere. If a capsule or sphere works for you, the code could be as simple as this one:
var leftcontrol : GUITexture;
var rightcontrol : GUITexture;
private var controller : CharacterController;
function Start(){ // find the controls at Start, not Awake!
leftcontrol = GameObject.Find("GUIleft").guiTexture;
rightcontrol = GameObject.Find("GUIright").guiTexture;
controller = GetComponent(CharacterController);
}
function Update () {
for (var touch : Touch in Input.touches){
if (touch.phase == TouchPhase.Stationary){
if (leftcontrol.HitTest (touch.position)){
controller.Move(Vector3(0, 0, -10));
}
else
if (rightcontrol.HitTest (touch.position)){
controller.Move(Vector3(0, 0, 10));
}
}
}
}
NOTE: CharacterController and Rigidbody are mutually exclusive - delete the Rigidbody after adding the CharacterController.
Thank you, that pretty much solved my problem. One thing though, you are missing a ")" at line 15 and 19. Thank you
Your answer
Follow this Question
Related Questions
BCE0019: 'collision' is not a member of 'Object'. 1 Answer
How to change speed transition between materials 2 Answers
Coins work in engine but not on phone 0 Answers
Collide detection with tag [JS] 0 Answers
Car dynamics? 0 Answers