how to make space gravity in unity code example
Example 1: how to make space gravity in unity
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;
}
}
}
Example 2: how to make space gravity in unity
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; } } }