I have this problem that my character doesn’t want to jump with rigidbody.addforce. I can jump with transform.translate but it just teleports a few inches above. What is the best option I can use: Transform.translate or Rigidbody.Addforce?
public float moveSpeed;
public float JumpLength;
private Camera cam_;
[SerializeField]
private bool IsOnGround;
// Use this for initialization
void Start() {
cam_ = Camera.main;
IsOnGround = true;
}
// Update is called once per frame
void Update()
{
//Movement
Vector3 mousePosition = cam_.ScreenToViewportPoint(Input.mousePosition);
Vector3 direction = new Vector3();
if (mousePosition.x < 0.5f)
{
direction = Vector3.left;
}
else
{
direction = Vector3.right;
}
transform.Translate(direction * moveSpeed * Time.deltaTime);
//Jump
if (Input.GetMouseButtonDown(0) && IsOnGround)
{
Debug.Log("MouseButtonClicked");
GetComponent<Rigidbody>().AddForce(Vector3.up * JumpLength); //Transform.translate or Rigidbody.Addforce???
IsOnGround = false;
}
}
//If the player has touched the ground
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground"))
{
Debug.Log("GroundTouched");
IsOnGround = true;
}
}
}