Hi!,
Sorry for the dumb Topic, but didn’t really know how to describe my problem. First of all I must say I have spent last 5 days trying to make this work without luck, so, I have tested everything I have come across before posting. I’m pretty depressed not being able to find a solution for thi “easy” problem
What I’m trying to do is fairly easy, I have a player that has a collider (box collider). In the other side I have another 90 dregrees horizontal collider (a wall). I want that when I get a trigger enter in my player, push it out of the wall he is hitting. More ore less it works, but i can see during litle periods of time how player gets into the wall collider, what creates a vibrating effect on the player character being move it forward and back again to fixed position out of the wall collider.
What I’m doing is move the body in FixedUpdate based on player input and then wait for collisions. When I detect a vertical collision I just put the body back out of the colliding wall. Here is the the code I use:
public class PlayerController : MonoBehaviour
{
// Use this for initialization
void Start()
{
speed = 250;
jumpSpeed = 1400;
gravity = -850;
_weapon = GetComponent(typeof(IWeapon)) as IWeapon;
_mainCamTransform = Camera.mainCamera.transform;
_thisTrans = transform;
}
// Update is called once per frame
void FixedUpdate()
{
CharacterController2D controller = this.GetComponent<CharacterController2D>();
PackedSprite spr = this.GetComponent<PackedSprite>();
Vector3 moveVector = Vector3.zero;
if (Input.GetKey(KeyCode.RightArrow))
{
moveVector = Vector3.right * speed;
if (!facing)
{
gameObject.transform.localScale = (new Vector3(1, 1, 1));
facing = !facing;
}
}
//Move he body
_deltaPos = moveVector * Time.fixedDeltaTime;
rigidbody.MovePosition(rigidbody.position + _deltaPos);
}
void HandleVerticalCollision(Collider other)
{
Debug.Log("Entering Vertical Collision for " + other.name);
//We have a collision, check if it collided and push the collider out
Bounds otherBound = other.bounds;
Rigidbody body = gameObject.rigidbody;
Vector3 newPos = body.position;
Vector3 oldPos = newPos - _deltaPos;
//Check if collision moving in X
Bounds thisBound = collider.bounds;
thisBound.center = oldPos + new Vector3(_deltaPos.x, 0, 0);
if (thisBound.Intersects(otherBound))
{
//Push back the body if we're heading right
if (transform.localScale.x > 0)
{
newPos.x = otherBound.min.x - thisBound.extents.x;
}
else if (transform.localScale.x < 0)
{
//Push back the body if we're heading left
newPos.x = otherBound.max.x + thisBound.extents.x;
}
}
body.position = newPos;
}
void OnTriggerEnter(Collider other)
{
HandleVerticalCollision(other);
}
public float speed;
public float jumpSpeed;
private float gravity;
public Vector3 verticalSpawnPoint;
public Vector3 horizontalSpawnPoint;
IWeapon _weapon;
//Right
bool facing = true;
//can jump
bool jumping = false;
//100 units per second
float curJumpSpeed = 0;
Transform _mainCamTransform = null;
Transform _thisTrans = null;
Vector3 _deltaPos;
}
At first I was thinking about the idea that some rendering was made between the FixedUpdate call and the collision detection, so, this could explain why the player was first rendered inside the wall and later out of it, but it seems by one of the wiki enties (execution order of monobehaviours) that nothing is executed between fixedupdate and collision code, so my idea should work.
I have minimized the code to make easy for everybody to check it. I really need to fix this, so, if anyone could be so kind to have a look at it I would be really gratefull.
Thanks in advance,
HexDump.