Hi! I'm making my first game with Unity. It's going to be a 3D platformer but for some reason the gravity just stopped working. I can't figure out what's wrong with it, so I'm hoping somebody will be able to help. My gravity is set to -9.81. Here is the full code for the player: Code (CSharp): public float speed; public float jumpForce; bool canJump; CharacterController cc; public Vector3 movement; void Start() // On start { cc = GetComponent<CharacterController>(); // Getting the CharacterController to assign it to cc } void Update() // Once per frame { cc.Move(movement * Time.deltaTime); // Setting cc's movement to movement Vector3 movement.y += Physics.gravity.y * Time.deltaTime; // Gravity (still bugged, fix requires loss of double jump) movement = ((transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"))) * speed; // Movement and controls // Double jumps (start) if(cc.isGrounded) // Checking if cc is on a surface { if(Input.GetButtonDown("Jump")) // Checking if the player pressed the jump button { movement.y = jumpForce; // Jump canJump = true; // Setting canJump to true for double jump } canJump = true; // Setting canJump to true while on floor } else // Checking if cc is NOT on a surface { if(canJump) // Checking if canJump is true { if(Input.GetButtonDown("Jump")) // Checking if the player pressed the jump button { movement.y = jumpForce; // Jump canJump = false; // Restricting a triple jump } } } // Double jumps (end) } Any help would be appreciated! PS: It needs to work with my game's double jump mechanic.
At Line 18, you are assigning a Vector3 value to movement. This overwrites the value you assigned to movement.y at Line 16. Not sure it will fix your problem, but try swapping Lines 16 and 18 with each other.
Just swapping the lines won't totally fix it. You'll want to fully preserve the y component of the movement variable.
I think you're right. At Lines 25 and 38, he's potentially got the same problem. Not sure, of course, but I think what we've got here is a common error for new Unity programmers, which is thinking that changes to a variable linked to action take effect immediately. The only time it appears movement affects action is at Line 14 (which maybe ought to be moved to the end of Update, and/or moved to FixedUpdate). When computing the next movement, one needs to accumulate all the relevant drivers (gravity, player input, current velocity) by summing them. Applying them individually just means the last one you assign is the only one you'll see taking any effect.
I tried both moving line 14 to the end of void Update() and putting it in void FixedUpdate(), but neither worked.
Also, if it helps, here is a script for the camera: Code (CSharp): public float sensitivity; float horizontal; float vertical; float desiredXValue; float desiredYValue; public bool customOffset; public Transform target; public Transform pivot; public Vector3 offset; Quaternion rotation; void Start() // On start { if (!customOffset) // Checking if customOffset is NOT true { offset = target.position - transform.position; // Automatically setting a camera offset } pivot.transform.position = target.transform.position; // Moving pivot to the target pivot.transform.parent = target.transform; // Setting the pivot's parent to the target Cursor.lockState = CursorLockMode.Locked; // Locking the cursor } void Update() // Once per frame { transform.LookAt(pivot); // Making the camera look at the pivot horizontal = Input.GetAxis("Mouse X") * sensitivity; // Setting the horizontal float to be Mouse X multiplied by the sensitivity target.Rotate(0, horizontal, 0); // Rotating the y axis by horizontal vertical = Input.GetAxis("Mouse Y") * sensitivity; // Setting the vertical float to be Mouse Y multiplied by the sensitivity pivot.Rotate(-vertical, 0, 0); // Rotating the x axis by vertical multiplied by -1 to make it more natural rotation = Quaternion.Euler(desiredXValue, desiredYValue, 0); // Rotating the camera angle transform.position = target.position - rotation * offset; // Orbiting around the pivot desiredXValue = pivot.eulerAngles.x; desiredYValue = target.eulerAngles.y; }
I just noticed something: The gravity is working, just really, really slowly. Like I have to sit there for 1 minute just to go down by 1.5. I still don't know why it's happening though... also, jumping barely does anything, but it's there. Changing the gravity all the way to -750 increases the speed of falling to normal... but why so much? Shouldn't -9.81 be enough?
This means your code is still overwriting the y velocity instead of letting it build up. Please read the earlier posts in this thread.
So I managed to fix the problem. For anyone else with this issue, here are the changes that fixed it: Code (CSharp): public float speed; public float jumpForce; float gravity; bool canJump; CharacterController cc; public Vector3 movement; void Start() // On start { cc = GetComponent<CharacterController>(); // Getting the CharacterController to assign it to cc } void Update() // Once per frame { movement = ((transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"))) * speed; // Movement and controls gravity += Physics.gravity.y * Time.deltaTime; // Gravity (still bugged, fix requires loss of double jump) movement = new Vector3(movement.x, gravity, movement.z); // Double jumps (start) if(cc.isGrounded) // Checking if cc is on a surface { if(Input.GetButtonDown("Jump")) // Checking if the player pressed the jump button { movement.y = jumpForce; // Jump canJump = true; // Setting canJump to true for double jump } canJump = true; // Setting canJump to true while on floor gravity = 0; } else // Checking if cc is NOT on a surface { if(canJump) // Checking if canJump is true { if(Input.GetButtonDown("Jump")) // Checking if the player pressed the jump button { movement.y = jumpForce; // Jump canJump = false; // Restricting a triple jump } } gravity += Physics.gravity.y * Time.deltaTime; } // Double jumps (end) cc.Move(movement * Time.deltaTime); // Setting cc's movement to movement Vector3 } This worked but the jump is a little teleporty (is that a word?) but I can fix that later.