- Home /
Question by
KT8144 · Jan 20, 2016 at 03:39 PM ·
movement scriptaxis
How to move an object in y-axis?
I have made a "Gravity Changer" that if I collide it with the player GameObject, it will change the gravity from (0, -9.81, 0) to (9.81,0 ,0). However the player cannot move horizontally in the new set gravity. When I press the default a and d button, it instead goes upwards or affecting the player's vertical speed. How can I make it able to move along the y axis with pressing a and d button?
Here are some scripts written (some may not be relevant):
public float speed;
public float jumpHeight;
public float brakeFactor;
public GameObject rocketBoost;
public float boostSpeed;
public float boostDuration;
public GameObject gravitySwitcher;
public GameObject camera;
private Rigidbody rb;
private bool isFalling = false;
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space) && isFalling == false)
{
GetComponent<Rigidbody> ().AddForce (Vector3.up * jumpHeight, ForceMode.VelocityChange);
}
isFalling = true;
}
void OnCollisionStay ()
{
isFalling = false;
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
if (Input.GetKeyDown(KeyCode.S))
{
rb.AddForce(-brakeFactor * rb.velocity);
}
}
void OnTriggerEnter (Collider other)
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
if (other.tag == "RocketBoost")
{
Destroy (rocketBoost);
rb.AddForce (movement * speed * boostSpeed);
boostDuration = 3 + Time.time;
if (boostDuration <= 0)
{
rb.AddForce (movement * speed);
}
}
if (other.tag == "GravityChanger")
{
Destroy (gravitySwitcher);
Physics.gravity = new Vector3 (9.81f, 0f, 0f);
camera.transform.rotation = Quaternion.Euler (0f, 0f, 90f);
float moveUpward = Input.GetAxis ("Upward");
movement = new Vector3 (0.0f,moveUpward, moveVertical);
rb.AddForce (movement * speed);
}
}
Comment
Your answer
