- Home /
Wall collision problems!!
I am having some serious problems with the way my character controls. I have a custom code that isn't complex at all, so it's not the character controller preset. Anyways, whenever the player hits a wall, it kinda vibrates against it, trying again and again to go through it. I'd rather the player run into the wall, and just be forced to stop moving.
I tried writing something in the OnCollisionEnter function about when the player hits the wall to reduce the speed to 0, but then the player cant move. I even set it up so that if the character hits it and the speed is dropped to zero, that when the player turns around, it can accelerate in the turned-around direction.
Does anybody have some techniques to make a character whose collision isn't done through ray casting, but rather all just collision boxes, STOP colliding strangely with walls? I'll link you to a video to show what is happening.
http://www.youtube.com/watch?v=pcSJFEiOyYE
Also, here is my code:
private var done = false;
var spawnPoint : Transform;
var speed : int = 5;
var jumpSpeed: float = 8;
var isgrounded : boolean = true;
var isRight : boolean = true;
var LevelComplete : boolean;
var canMove: boolean = true;
function Start () {
}
function Update () {
Movement();
textureChanges();
}
function Movement (){
if(!LevelComplete){
Camera.main.transform.position = new Vector3(transform.position.x, 1, transform.position.z + 30);
if(canMove){
// if(Input.GetAxis("Horizontal")){
// transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
// }
var colliderBox = gameObject.GetComponent(BoxCollider); //controlls collider box size
var AT = gameObject.GetComponent(AnimateTexture); //Store AnimateTexture Script
if(Input.GetKey("a")){ //Player moves left
AT.rowNumber = 1;
transform.Translate(Vector2.right * speed * Time.deltaTime);
isRight = false;
} else if(Input.GetKey("d")){
AT.rowNumber = 1;
transform.Translate(-Vector2.right * speed * Time.deltaTime);
isRight = true;
} else { //Player is not moving
AT.rowNumber = 0; //Change to idle animation!
}
if(isgrounded == true){
if(Input.GetKeyDown("space")){
AT.rowNumber = 3; //Player jumping animation
rigidbody.velocity += Vector3.up * jumpSpeed;
Physics.gravity = Vector3(0, -85, 0);
}
}
if(isgrounded == false){
colliderBox.center = Vector3(0.03, -0.08, 0);
colliderBox.size = Vector3(.5, .7, .5);
}
else if(Input.GetKey("s")){
AT.rowNumber = 4; //Player squats down
colliderBox.center = Vector3(0.03, -0.39, 0);
colliderBox.size = Vector3(0.44, 0.18, 1);
}
else {
colliderBox.center = Vector3(0.03, -0.2965, 0);
colliderBox.size = Vector3(0.44, 0.37, 1);
}
}
}
}
Any suggestions?? This jagged hitting crap is so annoying, and I have no idea why the character is registering as on the ground when it's clearly not (allowing more jumps to happen). sigh.
I AM NOT USING A CHARACTER CONTROLLER!!!!! Also, yes, I have tried changing the physics material on the walls. Nothing works..
I believe the CharacterController script has fields to fix this. Read into the CharacterController again.
I'm not using the character controller though. I specifically noted that this has nothing to do with it. I'll look at it again, but seriously, it's so unrelentingly complicated that I have no idea what I'm looking at.
Answer by Remludar · May 30, 2015 at 11:31 AM
Put the movement code in FixedUpdate()
Thank you so much, I know this topic is old, but this fixed my problem!
Answer by Tekksin · Dec 30, 2013 at 06:29 PM
oh crap, sorry the video was private lol, I changed the setting, you all should be able to see it now. And yes, it's 2D...
Anyways, I'd rather understand exactly what's happening with my character, instead of relying on some taxing preset that barely does what I need it to.
Thanks for the down vote for me being honest, btw. Real mature. edit: I wrote a code that stops the jittering from happening. And it doesn't involve the evil character controller.
Answer by Spinnernicholas · Dec 30, 2013 at 05:28 PM
Use the CharacterController provided by Unity. It has controls to handle collision jitter.
[OLD] In the CharacterController, Min Move Distance and Skin width have to do with collision jitter.
so you're saying reference what that script says about skin width? because I'm not using the character controller.
edit: just searched for the word "skin" in all the javascript files in the character controller assets, and couldn't find anything =_=
edit again: I found this in one of the scripts.. Is it something I should look into? it's admittedly very confusing. I'm not a coding master or anything.
function OnControllerColliderHit (hit : ControllerColliderHit) {
if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) {
if ((hit.point - movement.lastHitPoint).sqr$$anonymous$$agnitude > 0.001 || lastGroundNormal == Vector3.zero)
groundNormal = hit.normal;
else
groundNormal = lastGroundNormal;
movingPlatform.hitPlatform = hit.collider.transform;
movement.hitPoint = hit.point;
movement.frameVelocity = Vector3.zero;
}
}
honestly, I have no idea how to pick that apart and use it. at all. :|||||||||
Lol, sorry, I thought you were using the CharacterController, i didn't really look at your code. You should consider using the CharacterController. It is a script provided by Unity to handle exactly what you are trying to do. It's in the components menu in the menubar. You can either use it as a starting point, or you can create another script to interact with it.
And, it is a great example of really well built code.
I know all about it. There's a reason I'm not using it. I wanted a custom collision. I wrote my own script to have a box collider ins$$anonymous$$d of the annoying pill one. The pill made the character not feel like a traditional 2D one. But this jittery wall crap is really limiting my design set up.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
2D game, going through walls 1 Answer
Passing through walls. 2d toolkit 1 Answer
How to change the color of the ball after hitting square? 5 Answers
Unity2D Collision is bugged 0 Answers