**Hey!** I have this script here, for a 2D side scrolling game. It is attached to my player object.
----------
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
//Variables
public float PlayerSpeed = 5f;
public float PlayerRotation = 10f;
public float JumpForce = 800f;
public bool IsRight = false;
public bool CanFlip = true;
//Update Function
void Update () {
//Move
float Move = Input.GetAxis ("Horizontal");
float MoveVertical = Input.GetAxis ("Vertical");
rigidbody2D.velocity = new Vector2 (MoveVertical * PlayerSpeed, rigidbody2D.velocity.x);
rigidbody2D.velocity = new Vector2 (Move * PlayerSpeed, rigidbody2D.velocity.x);
rigidbody2D.AddTorque (Move * PlayerRotation);
rigidbody2D.AddTorque (MoveVertical * PlayerRotation);
//Flip
if (Move > 0 && !IsRight) {
Flip ();
} else if (Move < 0 && IsRight) {
Flip ();
}
//Jump
// if (Input.GetKeyDown (KeyCode.W)) {
// rigidbody2D.AddForce(new Vector2 (0, JumpForce));
// }
}
//Flip Function
void Flip() {
if (CanFlip) {
IsRight = !IsRight;
Vector3 Scale = transform.localScale;
Scale.x *= -1;
transform.localScale = Scale;
}
}
}
----------
It works perfectly! I am able to move my character in all four directions, and a slight torque is applied. What I want to achieve is a way to make the gravity feel a little more floaty, like space. Even when he is not moving I would love to see a slight 'bobbing' around. *Is there a fairly simple way to do this?*
↧