- Home /
2D "Platformer" Movement Problems
Alright so I have movement working pretty much how I want too on the graphical side. The only problem I am having is getting jumping to work how I want too with the horizontal movement how I want too. Right now I am using a mix of RigidBody2D.AddForce() for jumping, and then transform.Translate() for horizontal movement as seen below. This is a multiplayer game so the Position of the player is being sent over the network, as well as the Velocity of the player.
So if the player is standing still and they jump (Space), they jump up and land perfectly fine, no problems. If the player is moving left or right (A/D, < / >), they move left or right perfectly fine, no problems. The problem is when the player jumps while moving left or right, the velocity of the player seems to drastically lower and they fall in "slow motion", barely lowering themselves. If you move in mid-air and then stop moving, the velocity kicks back in and you fall down like you should.
What I want is to allow the player to move while in mid-air, but to drop at the same time. There should be no "hanging" in mid-air while you are able to move left or right.
PlayerMovement.js
private var moveSpeed: float = 7.5;
private var jumpForce: float = 250.0;
private var facingRight: boolean;
function FixedUpdate() {
if (networkView.isMine) {
//JUMPING SCRIPT
if (Input.GetAxis('Jump') && isGrounded()) { //IF GROUNDED
rigidbody2D.AddForce(Vector2(0, jumpForce)); //JUMP THE CHARACTER USING FORCE
}
}
}
function Update() {
if (networkView.isMine) {
//HORIZONTAL MOVING CODE
if (Input.GetAxis("Horizontal")) {
transform.Translate(Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime, 0.0, 0.0);
if (Input.GetAxis("Horizontal") >= 0.0) { //FACING RIGHT
thisHead.localPosition.x = 0.5;
facingRight = true;
} else { //FACING LEFT
thisHead.localPosition.x = -0.5;
facingRight = false;
}
}
}
function isGrounded() {
var result: boolean = Physics2D.Linecast(transform.position, groundChecker.position, 1 << LayerMask.NameToLayer("Ground"));
if (result) {
//rigidbody2D.velocity.y = 0;
Debug.DrawLine(transform.position, groundChecker.position, Color.green, 0.5f, false);
} else {
Debug.DrawLine(transform.position, groundChecker.position, Color.red, 0.5f, false);
}
return result;
}
function OnSerializeNetworkView(stream: BitStream, info: NetworkMessageInfo) {
if (stream.isWriting){
//Executed on the owner of the networkview; in this case the Server
//The server sends it's position over the network
var pos: Vector3 = transform.position;
var velocity: float = rigidbody2D.velocity[1]; //Only send the Y velocity, Networks can't send Vector2's
stream.Serialize(pos);//Encode and send Position first
stream.Serialize(velocity);//Encode and send Velocity second
} else {
//Executed on the others; in this case the Clients
//The clients receive a position and set the object to it
var posReceive : Vector3 = Vector3.zero;
Debug.Log('Receiving '+posReceive);
stream.Serialize(posReceive);//Decode Position first and save it
transform.position = posReceive;
var velocityReceive: float;
stream.Serialize(velocityReceive);//Decode Velocity second and save it
rigidbody2D.velocity = Vector2(0, velocityReceive);
}
}
Answer by oasisunknown · Jun 09, 2014 at 12:26 AM
If I am understanding right your issue is in how the physics works for unity.
your using a physics based update for the just (AddForce) but your over riding that with a non physics based movment when you move left or right by controlling the transform of the obj directly through transform.translate.
your best option is to do it all through the physics engine so there is no overlap.
let me link you to a video on the unity training archives that should help you clear this up.
http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers
the coding starts at around minute 38
Alright I decided to switch everything over to Physics-based, as I guess the problems I had with it were fixed in Unity 4.5. Before 4.5 there were tons of bugs with physics and 2D.
I also figured out that I only Serialize and Send the velocity of the Y, and just Serialize and Send the X of the transform. If I send both velocities it causes some weird problems on the client & server where they misread eachother.