Hi All, I have A planet And have Coded A gravity For It, my character And my Planet Are All 2D, Every Thing Is Fine but there IS one problem, If I press A Horizontal Key my character moves fine Around the 2D planet, but When I stop pressing these Button, My charachter continue to Move Around the Planet crazily, I would be Happy if SomeOne Could Help, Here Are the two codes I Attached to the character:
using UnityEngine;
using System.Collections;
public class GravityBody : MonoBehaviour {
public GravityAtrractor Attractor;
private Transform myTransform;
void Start () {
//rigidbody2D.constraints = RigidbodyConstraints.FreezeRotation;
rigidbody2D.gravityScale = 0;
myTransform = transform;
}
void Update () {
Attractor.Attract (myTransform);
}
}
And…
using UnityEngine;
using System.Collections;
public class CharacterMovements : MonoBehaviour {
public float MaxSpeed = 6.0f;
public float jumpSpeed = 1.0f;
public bool FacingRight= true;
public float MoveDirection;
public Vector3 MoveDir;
private Vector3 Copyer;
private Vector2 mover;
public bool Grounded = false;
public Transform groundCheck;
public float GroundRadios = 0.2f;
public LayerMask WhatIsGround;
void Awake(){
groundCheck = GameObject.Find ("GroundCheck").transform;
}
void FixedUpdate () {
Grounded = Physics2D.OverlapCircle (groundCheck.position, GroundRadios, WhatIsGround);
if (MoveDirection > 0.0f) {
rigidbody2D.MovePosition (rigidbody2D.position + (mover*MaxSpeed*Time.deltaTime));
}
if (MoveDirection < 0.0f) {
rigidbody2D.MovePosition (rigidbody2D.position - mover*MaxSpeed*Time.deltaTime);
}
}
void Update () {
// jumpRestRemaining -= Time.deltaTime; // Counts down the JumpRest Remaining
MoveDirection = Input.GetAxis ("Horizontal"); /// Move dire AsLie
// if (MoveDirection == 0) {
// rigidbody2D.Sleep();
// }
MoveDir = new Vector3 (Input.GetAxis ("Horizontal"),0,0);//In Move dire farE
Copyer = transform.TransformDirection (MoveDir);
mover.x = Copyer.x;
mover.y = Copyer.y;
// rigidbody2D.velocity = new Vector2 (rigidbody2D.position + mover*MaxSpeed*Time.deltaTime);// Just For test :D
// rigidbody2D.velocity = new Vector2 (MoveDirection * MaxSpeed, rigidbody2D.velocity.y);
if (MoveDirection > 0.0f && !FacingRight) {
Flip();
}
if (MoveDirection < 0.0f && FacingRight) {
Flip();
}
if (Grounded && Input.GetButton("Jump")) { // If the jump button is pressed and the ground is less the 1/2 the hight of the character away from the character:
rigidbody2D.AddRelativeForce (Vector3.up * jumpSpeed * 50); // Adds upward force to the character multitplied by the jump speed, multiplied by 100
}
}
void Flip(){
FacingRight = !FacingRight;
transform.Rotate (Vector3.up, 180.0f, Space.World);
}}
And Here Is The Class I Attached to Planet:
using UnityEngine;
using System.Collections;
public class GravityAtrractor : MonoBehaviour {
public float gravity = -5;
public void Attract (Transform Body){
Vector2 gravityUp = (Body.position - transform.position).normalized;
Vector2 BodyUp = Body.up;
//Body.rigidbody2D.AddForce (gravityUp * gravity);
Body.rigidbody2D.AddForce (gravityUp * gravity*50);
//Body.rigidbody.AddForce (gravityUp * gravity);
Quaternion TargetRotation = Quaternion.FromToRotation (BodyUp, gravityUp) * Body.rotation;
Body.rotation = Quaternion.Slerp (Body.rotation, TargetRotation, 50 * Time.deltaTime);
}}