Created
May 27, 2021 11:44
-
-
Save TheCuttlefish/360a3df9ddc7f4f3222256330565afb5 to your computer and use it in GitHub Desktop.
Orbital Movement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour | |
{ | |
Vector3 planetDir; | |
float pull = 1; // away or towards planet / 1 or -1 | |
Rigidbody2D rb; | |
float movement;// horizontal input | |
void Start() | |
{ | |
rb = GetComponent<Rigidbody2D>(); | |
} | |
void Update() | |
{ | |
planetDir = Vector3.Normalize(transform.position); // direction | |
Debug.DrawLine(transform.position, Vector3.zero, Color.red); // debug line | |
transform.up = planetDir;//always look at the planet | |
if (Input.GetKeyDown(KeyCode.Space)) pull = -pull;//swtich pull - positive to negative | |
movement = Input.GetAxis("Horizontal");//left right input | |
} | |
private void FixedUpdate() | |
{ | |
rb.AddForce(planetDir * pull * 200);//planet pull | |
rb.AddForce(transform.right * movement * 300);//movement dir | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment