- Home /
turning on and off gravity help
am doing my first game where my player has to move in x and y directions according to the inputs given from keyboards and has to fall down if there is no inputs are give (i.e) if no keys are pressed.
is there a way to turn on gravity when there are no inputs and turn off if any keys are pressed.
previously i was doing gravity manually. the i used previously is below
using UnityEngine;
using System.Collections;
public class player_script_2 : MonoBehaviour {
Vector3 move;
float accleration = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
move.y = Input.GetAxis ("Vertical");
move.x = Input.GetAxis("Horizontal");
//Check if no buttons are pressed
if(Input.GetAxis("Vertical") == 0f && Input.GetAxis("Horizontal") == 0f) {
if(transform.position.y >= -4.45f) {
move.y -= 2f;
}
}
print ("before "+ move);
move.Normalize();
print ("after "+ move);
float xdir = move.x * accleration * Time.deltaTime;
float ydir = move.y * accleration * Time.deltaTime;
transform.position = new Vector3(transform.position.x + xdir, transform.position.y + ydir, transform.position.z);
}
}
sadly according to my script, the player's jumping or standing on a higher level wont happen as i have given a check for y position. need help...
Answer by bubzy · Jun 14, 2013 at 06:41 AM
// Update is called once per frame
void Update () {
move.y -= 2f;
move.y = Input.GetAxis ("Vertical");
move.x = Input.GetAxis("Horizontal");
//Check if no buttons are pressed ** not required **
}
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Planet gravity help need guidance no code 1 Answer
Add/remove Gravity with code 1 Answer
How to set gravity for model? 1 Answer