- Home /
Making My Character Stop When It Hits A Wall
So, guys. I just started a project this morning. On all my little fun experiments, I've used a presupplied character controller. This time, I made my own, and here's how I did it: I made a simple movement script, attached it to a box, and then parented it to the main camera. Simple, right? Just in case it's necessary, here's the script I wrote:
> public var speed = 5;
function Start () {
}
function Update () {
var newPosition : Vector3 = transform.position;
if (Input.GetKey (KeyCode.W)){
newPosition.z += speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S)){
newPosition.z -= speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A)){
newPosition.x -= speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D)){
newPosition.x += speed * Time.deltaTime;
}
transform.position = newPosition;
}
I then made boxes and a wall. Their positions and rotations are locked on all axis, and the character's position is locked on the y axis. The character's rotations are locked on all axis as well.
So, I made them all rigidbodies to attempt to make it so that that the character would stop when it hit something. It worked, to the extent that the character could not pass through boxes. However, it gained permanent momentum in the opposite direction that it encountered the rigidbody at. When it came to a wall, the character was flung through it and gained tons of permanent momentum.
I'm not sure where to go from here. I did a bit of research, and saw somewhere to check the "Is Kinematic" button. However, all this did was make the character pass straight through the other boxes.
If you want to actually see this happen, go here:
http://xoraterra.blogspot.com/2012/06/welcome-to-blog.html
Thanks for any help, guys!
Answer by torrente · Jun 27, 2012 at 02:37 AM
It looks like you have three systems fighting there... you are moving the object with values that manipulate the positioning of transform and not the rigid body. It works, but think of it as the rigid simply going where the transform goes. The second thing that might be out of order is using Update instead of FixedUpdate. The third is that the rigid body is doing its own thing once there is a collision. Prior to the collision, it is not doing anything. Maybe try something like the code you have, but use rigidbody.AddForce(vector3) in the FixedUpdate() to move it around. It is cool to see that you are really digging into the code and getting a solid understanding of Unity :)
Answer by SarperS · Jun 27, 2012 at 07:01 AM
You can either switch your rigidbody to Kinematic, so it doesn't get affected by forces or use Rigidbody class's MovePosition and MoveRotation methods in FixedUpdate.
Your answer
Follow this Question
Related Questions
Car Hit Wall And Continue 2 Answers
Disable car bounce Wall (Car Hit Wall) 0 Answers
rigidbody Hitting wall question ? 0 Answers
View entire object when on intersection of wall 3 Answers
i can see throught the walls!? -1 Answers