- Home /
movement with changing gravity,Movement controll with changed gravity
Hey, i'm pretty new to unity and got stuck... I want to create a game where a sphere is controlled using WASD and in certain situations, the gravity of the game is changed, so that the player has to controll the ball "in a 90° angle" where the ball is attached to the wall...I hope you get what i mean :D
to change gravity i used:
public class ChangeGravity : MonoBehaviour {
public bool normal;
public bool left;
public bool right;
public bool up;
private float gravity = Physics.gravity.y;
// Use this for initialization
void Start () {
if (normal){
Physics.gravity = new Vector3(0, gravity, 0);
}
if (left){
Physics.gravity = new Vector3(gravity, 0, 0);
}
if (right){
Physics.gravity = new Vector3(-gravity, 0, 0);
}
if(up) {
Physics.gravity = new Vector3(0, -gravity, 0);
}
}
such that i get enable a certain gravity whenever i need it...
for moving the player I use this script:
public class Movement : MonoBehaviour {
public Rigidbody rb;
public float speed = 2000;
public Camera cam;
public float jumpforce;
// Update is called once per frame
void FixedUpdate () {
//Forward
if(Input.GetKey("w"))
{
rb.AddForce(cam.transform.forward*Time.deltaTime * speed);
}
//Backwards
if (Input.GetKey("s"))
{
rb.AddForce(-cam.transform.forward * Time.deltaTime * speed);
}
//Left
if (Input.GetKey("a"))
{
rb.AddForce(-cam.transform.right * Time.deltaTime * speed);
}
//Right
if (Input.GetKey("d"))
{
rb.AddForce(cam.transform.right * Time.deltaTime * speed);
}
//Jump (at the moment also in the air)
if (Input.GetKeyDown("space")){
rb.AddForce(0, jumpforce * Time.deltaTime * 100, 0);
}
}
}
with a 360° third person camera and moving into the direction the camera is looking when pressing "W".
My problem is now:
if the gravity is changed to "right", how do i move the ball? When pressing "a" the ball should go "up"...
i tried this:
if (Input.GetKey("a"))
{
rb.AddForce(-cam.transform.up * Time.deltaTime * speed);
}
but this does not seem to work...
any ideas on how to fix this?
Thanks in advance for any help!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220612161001im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sphere + rigidbody + character controller 1 Answer
Rigidbody--Addforce on a Spherical Platform(A Globe) 2 Answers
WASD view not working 1 Answer