Move player on planets in unity 2d
C#
/*Hey, I found this script on reddit after hours of searching,
It allows you to move left and right (A & D) on any sized 2D planet,
There aren't any jump mechanics tho.*/
//Gravity Attractor, scrpit attached to the planet:
public float gravity;
public void Attract(Transform body)
{
Vector2 gravityUp = (body.position - transform.position).normalized;
Vector2 bodyUp = body.up;
Rigidbody2D rb2d;
rb2d = body.GetComponent<Rigidbody2D>();
rb2d.AddForce (gravityUp * gravity);
Quaternion targetRotation = Quaternion.FromToRotation (bodyUp, gravityUp) * body.rotation;
body.rotation = Quaternion.Slerp (body.rotation, targetRotation, 50 * Time.deltaTime);
}
//Gravity Body, script attached to the player:
public GravityAttractor attractor;
private Transform thisTransform;
private Rigidbody2D rb2d;
void Start ()
{
rb2d = GetComponent<Rigidbody2D> ();
thisTransform = transform;
}
void Update ()
{
attractor.Attract (thisTransform);
}
//Player Controller, script attached to the player:
PlayerController public float moveSpeed = 15;
private Rigidbody2D rb2d;
private Vector2 moveDir;
void Start()
{
rb2d = GetComponent<Rigidbody2D> ();
}
void Update ()
{
moveDir = new Vector2 (Input.GetAxisRaw ("Horizontal"), 0).normalized;
}
void FixedUpdate ()
{
Vector2 globalmovedir = (transform.TransformDirection(moveDir));
rb2d.position += globalmovedir * moveSpeed * Time.fixedDeltaTime
}
Also in C#:
- Title
- insert variables into string c#
- Category
- C#
- Title
- asp.net textarea disable resize
- Category
- C#
- Title
- set decimal point c#
- Category
- C#
- Title
- get hash c#
- Category
- C#
- Title
- c# ref
- Category
- C#
- Title
- c# loop
- Category
- C#
- Title
- what is c# used for
- Category
- C#
- Title
- how to cast list to observablecollection c#
- Category
- C#
- Title
- rotate player unity
- Category
- C#
- Title
- c# md5 int
- Category
- C#
- Title
- c# null conditional operator if statement
- Category
- C#
- Title
- try catch c#
- Category
- C#
- Title
- how to get the last element in an array in c#
- Category
- C#
- Title
- unity custom editor save changes
- Category
- C#
- Title
- how to access the dictionary from another script in unity
- Category
- C#
- Title
- chase object unity
- Category
- C#
- Title
- while loop in c#
- Category
- C#
- Title
- c# string to float
- Category
- C#
- Title
- how to use more than one condition in ef join
- Category
- C#
- Title
- asp net mvc 5 return view from another controller
- Category
- C#
- Title
- how to pause physics in unity c#
- Category
- C#
- Title
- c# creating exceptions
- Category
- C#
- Title
- unity normalize vector2
- Category
- C#
- Title
- unity object array
- Category
- C#
- Title
- linq in c#
- Category
- C#
- Title
- how to deactivate objects through scripts in unity
- Category
- C#
- Title
- c# check if string is all numbers
- Category
- C#
- Title
- C# downloadstirng download old
- Category
- C#
- Title
- how to turn a string in a char list c#
- Category
- C#
- Title
- c# for loop without increment
- Category
- C#
- Title
- unity making homing rocket
- Category
- C#
- Title
- unity load text resources from subfolder
- Category
- C#
- Title
- c# combobox datasource enum
- Category
- C#
- Title
- c# get desktop path
- Category
- C#
- Title
- c# if statement
- Category
- C#
- Title
- Net.ServicePointManager.SecurityProtocol .net framework 4
- Category
- C#
- Title
- what does thismean incsharp public static void Main
- Category
- C#
- Title
- c# string array
- Category
- C#
- Title
- get waht is differnt between two arrays c#
- Category
- C#
- Title
- in unity i want to destroy a gameobject when it hits the edge of the screen
- Category
- C#
- Title
- c# storing value in session
- Category
- C#
- Title
- how to check if a value is inside an array c#
- Category
- C#
- Title
- unix time c#
- Category
- C#
- Title
- c sharp add item to dictionary
- Category
- C#
- Title
- c# input integer
- Category
- C#
- Title
- c# repeat string x times
- Category
- C#
- Title
- unity docs player input
- Category
- C#
- Title
- c# instantiate
- Category
- C#
- Title
- array copy c#
- Category
- C#
- Title
- C# Into To Tring Debug.Log
- Category
- C#
- Title
- unity serializefield
- Category
- C#
- Title
- block wapalyzer from detecting codeigniter
- Category
- C#
- Title
- how to raycast unit
- Category
- C#
- Title
- c# get last character of string
- Category
- C#
- Title
- array syntax c#
- Category
- C#
- Title
- computer
- Category
- C#
- Title
- how to detect if a key is pressed in c#
- Category
- C#
- Title
- c# nullable generic
- Category
- C#
- Title
- unity face direction of movement
- Category
- C#
- Title
- c# random number between 0 and 1
- Category
- C#
- Title
- unity delete specific text in a string
- Category
- C#
- Title
- httpcontext in .net standard
- Category
- C#
- Title
- never lose focus textbox c#
- Category
- C#
- Title
- how to get array of children transform
- Category
- C#
- Title
- c# list tuple
- Category
- C#
- Title
- unity how to see what scen you are in
- Category
- C#
- Title
- random from list c#
- Category
- C#
- Title
- validating file upload asp.net core mvc
- Category
- C#
- Title
- c# inline if
- Category
- C#
- Title
- .net core identity get user id
- Category
- C#
- Title
- c# find element by condition
- Category
- C#
- Title
- compile in one single exe c#
- Category
- C#
- Title
- Assets/Scripts/Snake.cs(177,25): error CS1061: Type `Snake.SnakeBodyPart' does not contain a definition for `SetGridPostion' and no extension method `SetGridPostion' of type `Snake.SnakeBodyPart' could be found. Are you missing an assembly reference?
- Category
- C#
- Title
- bubble sort c#
- Category
- C#
- Title
- .sh script: check if file exist
- Category
- C#
- Title
- mvc input type file
- Category
- C#
- Title
- void start
- Category
- C#
- Title
- override Microsoft.AspNetCore.Authorization.AuthorizeAttribute
- Category
- C#
- Title
- c# .net core memory cache
- Category
- C#
- Title
- leantween id
- Category
- C#
- Title
- how to trigger event when a com device is connected in c#
- Category
- C#
- Title
- c# change colour of console
- Category
- C#
- Title
- unity create primitive
- Category
- C#
- Title
- c# thread sleep
- Category
- C#
- Title
- create dropdown in datatable c# dynamically
- Category
- C#
- Title
- c# region tag
- Category
- C#
- Title
- c# download file
- Category
- C#
- Title
- Instantiate c#
- Category
- C#
- Title
- constructor in inherited class c#
- Category
- C#
- Title
- unity instantiate
- Category
- C#
- Title
- C# graph api upload file one drive
- Category
- C#
- Title
- Move player on planets in unity 2d
- Category
- C#
- Title
- c# uppercase string
- Category
- C#
- Title
- mysql executeScalar only if successful
- Category
- C#
- Title
- iframe set html content c#
- Category
- C#
- Title
- unity 2d joystick controls
- Category
- C#
- Title
- unity calculate path
- Category
- C#
- Title
- c# input
- Category
- C#
- Title
- .net core copy file in folder to root
- Category
- C#
- Title
- write last line txt file c#
- Category
- C#