- Home /
Character controller problem
First I tried to use Character controller but then I ran into problem with Character Collider which couldn’t be rotated or freely modified.
Then I used Transform.Translate tocontroll my 2D object but now it is going thru walls. I tried Rigid bodies, box colliders, mesh with convex but still nothing. Part of my script:
var moveSpeed : double = 1.0;
// Amount to move
var amountToMovex =Input.GetAxis("Horizontal") * moveSpeed;
var amountToMovey =Input.GetAxis("Vertical") * moveSpeed;
//Move the player
currentShip.Translate(Vector3.right * amountToMovex);
currentShip.Translate(Vector3.up * amountToMovey);
I want to be able move my character but when it hits the wall I want it to stop. Character controller would do that but Character controller has only capsule collider and my object is rectangle shape. I couldn't rotate that capsule collider. Half of my object goes through the wall before it hits the vertical capsule collider.
Answer by Paulius-Liekis · Nov 28, 2012 at 02:35 PM
Try putting forces on RigidBody instead of moving it with Transform.
I tried this but it doesn't work.
Does AddForce always have to be in FixedUpdate or can it be in Update
var amountTo$$anonymous$$ovex =Input.GetAxis("Horizontal") * moveSpeed;
var amountTo$$anonymous$$ovey =Input.GetAxis("Vertical") * moveSpeed;
//$$anonymous$$ove the player
currentShip.Rigidbody.AddForce(Vector3.right * amountTo$$anonymous$$ovex);
currentShip.Rigidbody.AddForce(Vector3.up * amountTo$$anonymous$$ovey);
There are a number of possible reasons this isn't working for you. Some things to look for, make sure you have a big enough collider on your rigidbody, as well as making sure that you have static colliders applied to what ever makes up your wall that you are trying to collide with.
You misunderstood Paulius. If you are using forces, you can't use a CharacterController too. And the player has to have a Rigidbody attached to it, you can't just call the Rigidbody class on the ship if it doesn't have a rigidbody.
The character controller works great but I can't modify hard coded capsule collider that is attached to it.
I tried to search but there's not perfect answer to my problem. I will have to work on sollution now.
Thank you for help everybody.
Answer by kag359six · Nov 28, 2012 at 11:39 PM
You can use a character controller. it handles the collisions for you. just create:
var controller = GetComponent("CharacterController");
var moveX = Input.GetAxis("Horizontal") * speed;
var direction = new Vector3(moveX, moveY, 0);
controller.Move(direction * Time.deltaTime);
Go to this link here for a full example of using the controller.
I see that I can't have Character controller atached to other object.
I had my Transform.Translate script attached to empty object which had DontDestroyOnLoad and I was able to swap between controlling different objects.
Now I will have to come up with different sollution for that.