Hello, im in the process of creating a 2D game based on bird-like flight. Basically you can fly up, down, left and right in a 2D platform type game using the WASD keys. Anyway, when flying up and down, the object often drops by itself, twists and bounces around. Is there anyway to fix this issue? I only have a basic understanding of javascript so sorry if i don’t understand.
Code used:
#pragma strict
// The speed of the keyboard controls. A higher value will
// cause the object to move more rapidly.
var keyboardSpeed = 20.0;
// FixedUpdate is a built-in unity function that is called every fixed framerate frame.
// According to the docs, FixedUpdate should be used instead of Update when dealing with a
// Rigidbody.
// See http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.FixedUpdate.html
// for more information.
function FixedUpdate () {
// This is where we move the object.
// Get input from the keyboard, with automatic smoothing (GetAxis instead of GetAxisRaw).
// We always want the movement to be framerate independent, so we multiply by Time.deltaTime.
var keyboardX = Input.GetAxis("Horizontal") * keyboardSpeed * Time.deltaTime;
var keyboardY = Input.GetAxis("Vertical") * keyboardSpeed * Time.deltaTime;
// Calculate the new position based on the above input.
// If you want to limit the movement, you can use Mathf.Clamp
// to limit the allowed range of newPos.x or newPos.y.
var newPos = rigidbody.position + Vector3(keyboardX, keyboardY, 0.0);
// Move the object.
rigidbody.MovePosition(newPos);
}
// Require a Rigidbody component to be attached to the same GameObject.
@script RequireComponent(Rigidbody)
Thanks
-Caleb