- Home /
Collision with obstacles while constant moving forward
Hello, unityFriends
I'm new to Unity, so I would be thankful if somebody helps me with my issues.
Now I'm working over a 2D "runner" and I have 2 problems:
1) Collision with obstacles while constant moving forward
"Forward" I mean along the X axis) So while navigating the Player using the "A/D" it stops before Cubes with "Box Collider: on"
But I made my Player run constantly by adding this script to the Character Controller
function Update () {
transform.position.x += speed;
}
And after that the Character runs directly through the obstacles without stoping
What am I doing wrong ?
2) Square form of a Character Controller ?
y Player is a simple cube, with a texture on it.
How to change the "Character Controller" to handle an sharp-edged body? Or it allows usage of a sphere, ball or pill figure only? Picture may describe the second issue better:
Thank you!
Answer by ozone · Dec 09, 2013 at 12:14 AM
In the future, please ask questions about different issues separately. I'll be happy to answer these anyway, though.
1) You should use CharacterController.SimpleMove(Vector3.right*speed) instead of changing transform.position directly. The character controller takes collisions into account while direct modifications of transform.position will be carried out regardless of collisions.
2) First of all, make sure your player character has a Rigidbody attached. Then, you could try attaching a box collider to the player character and adding a section to your script that uses the OnCollisionEnter or OnTriggerEnter function (which should still be activated even if your character is passing through the object). In that function, you can add a line to set the player's forward speed to zero when it detects a collision or do whatever else you want it to do when it encounters an object.
Hey, thanks! You helped me to figure out the #1st problem and now it's working properly
But the second did not work. I've attached a box collider in addition to the character controller, changed the boundaries of the body, but still the collision does not respond correctly. I'll search for it or create another/separate question
I thought of something to fix the second problem. Editing my answer to include that as well.
Your answer
Follow this Question
Related Questions
Unity 4.3 CharacterController collision with physics 2D not working 1 Answer
I want my Player to have collision physics 1 Answer
Why Does Object Keep Getting Faster? 1 Answer
How do I get the player and the object they touch both disappear/destroyed? 1 Answer
How to make multiple objects mirror Player movement AND share collisions 1 Answer