- Home /
CharacterController going trought walls.
First of all, I need to say that I´m new to Unity. I created a "map" in blender and imported it to Unity as a .blend file, after it a CharacterController was created, w/ a camera and "walking and looking" scripts. Everythings works fine and smoothly but my character keep going trough the walls of the map. I´ve tried using RigidBody, every kind of "---"Collider but the CharacterController keep going trough. Sorry for asking something that must be very basic for all of you, but I couldn´t find any solution on the WEB. Please help =)
Is there a collider on the wall and if is make sure it's not a trigger?
Also how are you moving your character controller? With the standard charactermotor or a custom script?
Well if you're using the Character Controller you should make sure you use charactercontroller.move to move the CC http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.$$anonymous$$ove.html . Also make sure the wall has a collider.
The wall has: -$$anonymous$$esh Collider -Rigid Body
I´m moving my CharacterController with a custom script =)
In that custom script: Do you move the player using something like:
transform.position +=.......;
or do you use something like:
charactercontroller.$$anonymous$$ove(...);
?
Only the last option will let the player react to collision so making him not go through walls.
Oh thanks a lot! I´ll try to use "charactercontroller.$$anonymous$$ove();" The custom script that I was using: #pragma strict
function Start () {
}
function Update () { if (Input.Get$$anonymous$$ey("w")){ transform.Translate(0,0,1);} if (Input.Get$$anonymous$$ey("s")){ transform.Translate(0,0,-1);} if (Input.Get$$anonymous$$ey("a")){ transform.Translate(-1,0,0);} if (Input.Get$$anonymous$$ey("d")){ transform.Translate(1,0,0);} }
Answer by ExTheSea · May 21, 2013 at 05:40 PM
Ok so after looking at your script i can asure you that your problems come from transform.Translate. When you use transform.Translate or something like transform.position .... your transform will ignore collisions.
The basic rule for movement with collision-recognition is: If it's a CharacterController use CharactorController.Move or something equal and if it's a rigidbody use rigidbody.AddForce or something equal.
I'm gonna convert my comment to an answer because i'm certain that it will work and if you still have a problem with your script then please just go ahead and ask.
I had a little time left so i converted your movement script to use CharacterController.Move();
var GameobjectwithCharacterController : Gameobject;
var controller : CharacterController;
function Start () {
controller= GameobjectwithCharacterController.transform.GetComponent(CharacterController)
}
function Update () {
if (Input.GetKey("w")){ controller.Move(Vector3.forward);}
if (Input.GetKey("s")){ controller.Move(-Vector3.forward);}
if (Input.GetKey("a")){ controller.Move(-Vector3.right);}
if (Input.GetKey("d")){ controller.Move(Vector3.right);}
}
Warning: Code is untested.
Also you should change the variable name for the gameobject or use somthing like transform, transform.root or transform.parent.
PS: When it comes to CharacterController-Movement I would always recommend the FPSWalkerEnhanced: http://wiki.unity3d.com/index.php?title=FPSWalkerEnhanced
It worked man ^^ Your script helped me a lot! And the best of all: you explained everything. Now I understand, very interesting =) Thank you very much indeed.
I'm glad it worked. Could you now please accept the answer using the tick below the thumbs up/down buttons to mark the question as solved.
Good luck with your project :)
Answer by GamezAtWork · May 20, 2013 at 05:01 AM
Is the normals in blender pointed in the right direction? I had that problem once until I inverted some of the normals...
If not, perhaps you could create "invisible" wall colliders?
Was it really the thing with the normals that helped you when you had the problem because the normals don't have anything to do with the character controller. I would guess you did something else too.
Well, the normals dictate whether there is collision, from the last I recall.
Went to do a doublecheck as well, created a vertical plane and walked my charactercontroller through both sides. You can't go through from the visible side, but you can walk through the invisible side.
Of course, we have to check on his side first.
So you're talking about a mesh collider right? In that case maybe it's dependent on normals but i would recommend not to use mesh collider if possible. For example if your' making a wall i would just attach a box collider to it. $$anonymous$$akes the collision faster.
Your answer
Follow this Question
Related Questions
I need Ready Characters. Help me !!! 1 Answer
First Person Character Controls, How? 1 Answer
Passing through walls. 2d toolkit 1 Answer
Cloud recognition in Vuforia 0 Answers
General Unity 2D Character/Environment Control Questions 2 Answers