using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
public Rigidbody rb;
public float MouseSensitivity;
public float MoveSpeed;
public float jumpForce;
void Start ()
{
}
void Update()
{
//Look around
rb.MoveRotation(rb.rotation * Quaternion.Euler(new Vector3(0, Input.GetAxis("Mouse X") * MouseSensitivity, 0)));
//Move
rb.MovePosition(transform.position (transform.forward * Input.GetAxis("Vertical") * MoveSpeed) (transform.right * Input.GetAxis("Horizontal") * MoveSpeed));
//Jump
if (Input.GetKeyDown("space"))
{
print("clicked");
rb.AddForce(Vector3.up * jumpForce);
}
}
}
this is my code and a picture of the player object when I'm trying to jump it doesn't work but it does print clicked I tried to do many things but nothing worked so if you know how to solve the issue please tell me
CodePudding user response:
RigidBody.AddForce
has a second parameter of type ForceMode
that defaults to ForceMode.Force
. That particular ForceMode
is only good for continual application of force (like a rocket's thruster). For a feature like jumping, you'll either want to use ForceMode.Impulse
or ForceMode.VelocityChange
.
// Pick one or the other
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
Also, make sure your jumpForce
value isn't tiny or it'll be an unnoticeably small "jump."
CodePudding user response:
Firstly, physics calculations should generally be calculated inside of FixedUpdate()
. This is described in the unity documentation here.
Edit: To make it more clear for the comment on my post. Input should be located inside of the Update()
method whilst physics calculations should generally be calculated inside of the FixedUpdate()
method.
If it is decided that you want to register input inside of Update() and physics calculations, based on that input, inside of FixedUpdate() then you would use a boolean switch.
Looking at your code I would say that either:
The mass of your Rigidbody
is high and your jumpForce
variable is too low, trying increasing the value of the jumpForce
variable to see if the GameObject
moves.
Your Rigidbody has the Is Kinematic
checkbox selected.
AddForce
is defaulting the ForceMode
to ForceMode.Force
, try using a different force mode like ForceMode.Impulse
to deliver a quick force to the GameObject
. In your case this would look like rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
Hope this helps.