- Home /
I made a rocket! Help!
Hey! Im pretty new to scriping. This is what my Movement script looks like. I dont have rotation on the character yet, But i managed to fix the isGrounded problem i had. The problem i now i have is that when i jump and press left shit i accelerate on the y axis.
Im guessing its because iv added rb.velocity.y in moveDirection and the velocity get multiplied in moveDir. But i have no clue how to fix it
Any help would be appreciated :)
public class PlayerMovement : MonoBehaviour {
//VARIABLES
//MOVEMENT//SPEED
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] public float runSpeed;
[SerializeField] public float rot;
[SerializeField] public float rotSpeed;
//GRAVITY
[SerializeField] private float jumpHeight;
[SerializeField] private float gravity;
public bool isGrounded = true;
// [SerializeField] private float groundCheckDistance;
//[SerializeField] private LayerMask groundMask;
// Variable for Layer for groundcheck
private Vector3 moveDirection;
private Vector3 velocity;
private Animator anim;
Rigidbody rb;
private CharacterController controller;
private void Start()
{
//Debug.Log("Start");
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
}
private void Update()
{
//Debug.Log("test");
Move();
}
//MOVEMENT
private void Move()
{
float moveZ = Input.GetAxisRaw("Vertical");
float ad = Input.GetAxis("Horizontal");
moveDirection = new Vector3(ad, rb.velocity.y, moveZ);
if(isGrounded)
{
if(moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
//Walk
Debug.Log("Walk");
walk();
}
else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
anim.SetFloat("JumpAnim", 0); //Animation stop
//Run
run();
Debug.Log("run");
}
else if(moveDirection == Vector3.zero)
{
//Ìdle
anim.SetFloat("JumpAnim", 0);
idle();
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
//Animation stop
//Jump
jump();
}
}
rb.velocity = moveDirection * moveSpeed;
}
private void idle()
{
Debug.Log("Idle");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY ); //
}
private void walk() /////WALK
{
moveSpeed = walkSpeed;
anim.SetFloat("run", 0);
Debug.Log("walk");
float MMX = Input.GetAxis("Horizontal");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY);
anim.SetFloat("InputX",MMX);
}
private void run() ///// RUN
{
moveSpeed = runSpeed;
anim.SetFloat("run", 1);
float MMX = Input.GetAxis("Horizontal");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY);
anim.SetFloat("InputX",MMX);
}
private void jump()
{
anim.SetFloat("JumpAnim", 1);
Debug.Log("jump");
//rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
rb.AddForce((Vector3.up) * jumpHeight, ForceMode.Impulse);
// rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
//velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); //
}
void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.tag == "Plane")
{
isGrounded = true;
print("grounded");
}
}
void OnCollisionExit(Collision theCollision)
{
if (theCollision.gameObject.tag == "Plane");
{
isGrounded = false;
print("not grounded");
}
}
}
Answer by pennzo · Mar 01, 2021 at 03:12 PM
Thank you, So much! This is exactly what i wanted to do! It does make me feel a little stupid tho. I was trying to do what you did there, but my knowledge in c# is very limit! Thank you!
Answer by pauldarius98 · Mar 01, 2021 at 12:52 PM
The problem lies at line 77 because you multiply the direction with the movement speed (which is runSpeed if you press shift). You can't press shift if you are not grounded but if you run and then jump, the movement speed will remain the one from running and thus you will jump higher. A simple solution would be to modify only the x and z axis of the velocity and let the AddForce and gravity to take care of the vertical movement. Try the following code:
private Vector3 moveDirection;
private Vector3 velocity;
private Animator anim;
Rigidbody rb;
private CharacterController controller;
private void Start()
{
//Debug.Log("Start");
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
}
private void Update()
{
//Debug.Log("test");
Move();
}
//MOVEMENT
private void Move()
{
float moveZ = Input.GetAxisRaw("Vertical");
float ad = Input.GetAxis("Horizontal");
moveDirection = new Vector3(ad, rb.velocity.y, moveZ);
if(isGrounded)
{
if(moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
//Walk
Debug.Log("Walk");
walk();
}
else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
anim.SetFloat("JumpAnim", 0); //Animation stop
//Run
run();
Debug.Log("run");
}
else if(moveDirection == Vector3.zero && rb.velocity.y == 0)
{
//Ìdle
anim.SetFloat("JumpAnim", 0);
idle();
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
//Animation stop
//Jump
jump();
}
}
rb.velocity = new Vector3(moveDirection.x * moveSpeed, rb.velocity.y, moveDirection.z * moveSpeed);
}
private void idle()
{
Debug.Log("Idle");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY ); //
}
private void walk() /////WALK
{
moveSpeed = walkSpeed;
anim.SetFloat("run", 0);
Debug.Log("walk");
float MMX = Input.GetAxis("Horizontal");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY);
anim.SetFloat("InputX",MMX);
}
private void run() ///// RUN
{
moveSpeed = runSpeed;
anim.SetFloat("run", 1);
float MMX = Input.GetAxis("Horizontal");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY);
anim.SetFloat("InputX",MMX);
}
private void jump()
{
anim.SetFloat("JumpAnim", 1);
Debug.Log("jump");
//rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
rb.AddForce((Vector3.up) * jumpHeight, ForceMode.Impulse);
// rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
//velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); //
}
void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.tag == "Plane")
{
isGrounded = true;
print("grounded");
}
}
void OnCollisionExit(Collision theCollision)
{
if (theCollision.gameObject.tag == "Plane");
{
isGrounded = false;
print("not grounded");
}
}