- Home /
Physics collider behaving very odd on iphone
The Problem:
Picture yourself hold the iphone landscape, so that the home button is facing downwards. At either side of the iphone screen are walls (so the player cannot escape the screen). The character has a rigidbody and a box collider attached and the walls have a box collider attached(with the triggers turned off). The user must press the screen to allow the character to fly upwards (think doodle jump). The user will tilt the phone left and right to move the character left and right. When the character collides with either wall, the character will stop untill i tilt the opposite direction.
The Solutions:
1) At first, i was changing the transform of the character with the accelerometer and the touch. This would obviously cause errors with colliding, so i changed the transforms to AddForce. This worked perfectly on the walls when colliding, the character was sliding up and down the walls, BUT, the accelerometer moving was all wrong. The character was too slow at either tilt and taking too much time to gain speed. Not only that i could not tell the forces to stop completely, or reset, to move the other directions (sort of snapping effect).
2) I moved away from AddForce and tried out velocity. This works likes a charm, it has my snapping effect and the character moves at a decent set speed. The problem of the walls has come back though. So a success with a failure.
3) Since i liked the velocity "style of movement", i want to keep it, so my thoughts on correcting this issue with the walls were to create an additional 2 box colliders on the walls, but moving them in by 1 into the scene view. These box colliders are put here so that when the character triggers them (moves into them), the velocity will equal nothing, thus stopping the character stopping on the wall and when the player moves the character in the opposite direction, the velocity is equaled to its default state. This works perfectly!....but only on the left wall. The right and left wall, are mirrored, they are 100% the same. Just when the character collides with the right wall, the problem comes back.
The Code:
The Player:
var jumpo: GameObject;
var TiltSpeed: int = 50;
var tileAngle: Vector3;
static var anint: float = 0.0;
var thetext: GUIText;
var speedtext: GUIText;
function Update()
{
//var tiltAngle = Vector3.zero;
tiltAngle = Vector3.zero;
tiltAngle.x = Input.acceleration.x;
if(tiltAngle.sqrMagnitude > 1)
{
tiltAngle.Normalize();
}
//tiltAngle *= Time.deltaTime;
tiltAngle *= TiltSpeed;
anint = tiltAngle.x;
thetext.text = ""+(tiltAngle.x).ToString("f2");
speedtext.text = ""+(TiltSpeed).ToString("f2");
if(changeaxis.thebool == true)
{
if(tiltAngle.x <= -1)
{
jumpo.rigidbody.velocity.x = 0;
}
else if(tiltAngle.x >= 1)
{
jumpo.rigidbody.velocity.x = anint;
}
}
if(changeaxisright.thebool == true)
{
if(tileAngle.x >= 1)
{
jumpo.rigidbody.velocity.x = 0;
}
else if(tiltAngle.x <= -1)
{
jumpo.rigidbody.velocity.x = anint;
}
}
if(changeaxis.thebool != true && changeaxisright.thebool != true)
{
jumpo.rigidbody.velocity.x = anint;
}
}
The Left Wall:
var jumpo: GameObject;
var wall: Collider;
static var thebool = false;
var thetext: GUIText;
function OnTriggerEnter() { thebool = true;
}
function OnTriggerExit() { thebool = false;
}
function Update() { if(thebool == true) { thetext.text = "true"; } if(thebool == false) { thetext.text = "false";
} }
The Right Wall:
The right wall code is EXACTLY the same as the left wall, just the javascript file is called differently.
Some Ideas:
I do have some ideas, like reverting back to the AddForce method, but, is there a way to completely STOP the character from moving in the current direction to avert in the opposite direction, while its in "mid-air"? OR Attempt to carry on the way i am going. I have been stuck with this problem for a few days now, so any help would be brilliant! Hopefully i made sense in this question. I can explain further if need be.
Your answer
Follow this Question
Related Questions
Why does this MeshCollider think it's half as thick? 2 Answers
Continuously moving rigidbody 2 Answers
Major issues with physics 1 Answer
Stopping my player from moving when hitting a wall. 5 Answers