Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Custom gravity pulling in weird directions.

$
0
0
I have a game I'm trying to make where the player jumps between planets in a 2D space. I have the "jump between planets" part down but my gravity is not working well. The way it currently works is that the player get's pulled to the nearby planet. I can jump just fine and the gravity pulls down well, the problem is when I move side to side. When I move to the left and then let go of the left arrow key, my player acts as if another force is pulling him to the right. Vice Versa if I move to the right. I currently use the RotateAround to move my player left and right but I have used Transform.Translate before and it does the same thing. I don't want to use addforce because I want my player to move 1:1 with what I'm pressing instead of sliding in directions and I think the addforce just covers up the problem of my player getting dragged back. using UnityEngine; using System.Collections; [RequireComponent(typeof(BoxCollider2D))] [RequireComponent(typeof(Rigidbody2D))] public class PlayerMovement : MonoBehaviour { GameObject nearby_planet; int jump, cooldown; Vector2 movement; bool touch, can_switch; Rigidbody2D rigid; Vector3 dir; void Start() { rigid = GetComponent(); jump = 2; can_switch = true; cooldown = 60; } void Update() { if (can_switch == false) { --cooldown; if (cooldown == 0) { can_switch = true; cooldown = 500; } } Debug.DrawRay(transform.position, (nearby_planet.transform.position - transform.position).normalized * 100, Color.red); } void FixedUpdate () { if (Input.GetAxis("Horizontal") > 0) transform.RotateAround(nearby_planet.transform.position, Vector3.forward, -20 * Time.deltaTime); else if (Input.GetAxis("Horizontal") < 0) transform.RotateAround(nearby_planet.transform.position, Vector3.forward, 20 * Time.deltaTime); transform.up = transform.position - nearby_planet.transform.position; Jump(); } private void Jump() { if (Input.GetKeyDown(KeyCode.UpArrow) && jump > 0) { rigid.AddForce((transform.position - nearby_planet.transform.position).normalized * 80000 * Time.smoothDeltaTime); jump--; } if (touch == false) { rigid.AddForce(-(transform.position - nearby_planet.transform.position).normalized * 1000 * Time.smoothDeltaTime); } if (touch == true && !Input.GetKeyDown(KeyCode.UpArrow)) { jump = 2; } } public void ClosestPlanet(GameObject planet) { if (nearby_planet != planet && can_switch == true) { can_switch = false; nearby_planet = planet; transform.up = transform.position - nearby_planet.transform.position; } } private void OnCollisionStay2D(Collision2D col) { if (col.gameObject.tag == "Planet") { touch = true; } } private void OnCollisionExit2D(Collision2D col) { if (col.gameObject.tag == "Planet") { touch = false; } } public GameObject Nearby_Planet { get { return nearby_planet; } set { nearby_planet = value; } } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles