I have gotton my character to move and jump. For some reason when I land from a jump I then move in pulses. The movement is a start then stop and so on after. It doen’t happen when I change the play speed to certain speeds but does when I change it to other certain speeds. For example with a speed of 5 it is fine unless I jump into one of the red things which just have capsule round collision. However at seed 6 it does the weird movement when I land after jumping. When the weird movement is happening I cannot jump. I have tested and it is running the jump code.
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.XR;
public class PlayerMovement : MonoBehaviour
{
Rigidbody rb;
[SerializeField] float MovementSpeed = 5.0f;
[SerializeField] float JumpInitialVelocity = 5.0f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Vector3 NewPlayerVelocity = Vector3.zero;
Vector3 CurrentVelocity = rb.velocity; //Players current velocity before update
//This input code need the unity input manager
//
//Jump movement
if (Input.GetKeyDown(KeyCode.Space)) //The down on Get Key Down means that it is only true on pressed and you must release it to trigger it again
{
Debug.Log("hi");
if (rb.velocity.y == 0) //If vertical velocity is 0 then player is on ground so player can only jump when on the ground
{
rb.velocity = new Vector3(rb.velocity.x, JumpInitialVelocity, rb.velocity.z);
}
}
//Bellow is WASD movement input converted into game player movement
else if (rb.velocity.y == 0) //If vertical velocity is 0 then player is on ground so player can only move when on the ground
{
//
//Forward and back movement
if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //If W and S are both not pressed then stop forward and back movement if on ground
{
if (rb.velocity.y == 0)
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
}
}
else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S)) //If W and S are both being pressed then stop forward and back movement if on ground
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
}
else //If none of the above is true then move as normal
{
if (Input.GetKey(KeyCode.W))
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, MovementSpeed);
}
if (Input.GetKey(KeyCode.S))
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, -MovementSpeed);
}
}
//
//Left and right movement
if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D)) //If A and D are both not pressed then stop forward and back movement if on ground
{
if (rb.velocity.y == 0)
{
rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
}
}
else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D)) //If W and S are both being pressed then stop forward and back movement if on ground
{
rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
}
else //If none of the above is true then move as normal
{
if (Input.GetKey(KeyCode.A))
{
rb.velocity = new Vector3(-MovementSpeed, rb.velocity.y, rb.velocity.z);
}
if (Input.GetKey(KeyCode.D))
{
rb.velocity = new Vector3(MovementSpeed, rb.velocity.y, rb.velocity.z);
}
}
}
}
}
The movement is a little dificult to explain. It only moves for a second then stops then repeats this and it stops me jumping.I think this is something to do with the collision but I don’t know. Does anyone know what is going on here?