- Home /
This script doesn't make smooth jump why ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
// public Rigidbody theRB;
public float jumpForce;
public CharacterController controller;
public float gravityScale;
private Vector3 moveDirection;
// Start is called before the first frame update
void Start()
{
// theRB = GetComponent<Rigidbody>();
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void FixedUpdate()
{
/* theRB.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, theRB.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
if (Input.GetButtonDown("Jumps"))
{
theRB.velocity = new Vector3(theRB.velocity.x, jumpForce, theRB.velocity.z);
} */
//moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
moveDirection = moveDirection.normalized * moveSpeed;
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
controller.Move(moveDirection * Time.deltaTime);
}
}
Answer by carrotboardgame · Aug 29, 2021 at 07:13 AM
I use that but it returns an error like this : error CS1612 Can't return because it is not a variable.
Ah sorry, it's for Rigidbody.
Try like this, in your lines:
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
change it like this:
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
else
{
//gravityScale is positive value 0 ~ 1000 or more
moveDirection.y = moveDirection.y +( Physics.gravity.y * gravityScale );
}
Or you can use AnimatioCurve on it, see about Animation Curve and implment it to jump.
Answer by GeroNL · Aug 29, 2021 at 07:05 AM
Hello, i think you just need to set the velocity of controller, like this :
if (controller.isGrounded && Input.GetButtonDown("Jump"))
{
controller.velocity.y = 10;
}
no need controller.move except for force movement x and z. you can controlling the gravity in project setting -> physics and find value of gravity y. it's already have default value on it.
Your answer
Follow this Question
Related Questions
Advice needed on my first c# 2D jump script 1 Answer
Can't get platforming character to jump 2 Answers
How do I make my character jump? 1 Answer