- Home /
This question was
closed Nov 13, 2020 at 02:11 PM by
Complexite for the following reason:
The question is answered, right answer was accepted
Question by
Complexite · Nov 13, 2020 at 12:03 PM ·
controllergravity2d-physics
gravity change via controls(help) 2d,Anyone know how to fix this gravity thing
anyone know how to fix this 2d gravity controller button thingy for players ,\I'm trying to make gravity controllable anyone know how to fix my errors I just started c# so I'm bad at it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gravitycontroller : MonoBehaviour
{
public Vector2 gravity;
public float gravForce = 9.81f;
public int gravPull;
void Start()
{
gravity.x = 0;
gravity.y = -9;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
gravity.y = 1;
}
else if (Input.GetKey(KeyCode.S))
{
gravity.y=-1;
}
else if (Input.GetKey(KeyCode.A))
{
gravity.x= 1;
}
else if (Input.GetKey(KeyCode.D))
{
gravity.x= -1;
}
else if (Input.GetKey(KeyCode.Z))
{
gravity.y=0;
}
}
}
Comment
Answer by sztobar · Nov 13, 2020 at 12:54 PM
What exactly is wrong with your code? You haven't written what doesn't work.
If you want to affect gravity for all rigidbody2d
objects in-game, you have to change gravity in Physics2D
object. Refer to unity documentation: https://docs.unity3d.com/ScriptReference/Physics2D-gravity.html