Here is the code I currently have however I want to be able to move mid-jump.
as of now if I jump while moving I will carry on in that direction however I can't change the direction I am heading while in the air. This code was made using unity and the player that this code is attached to is a capsule.
private CharacterController charController;
[SerializeField] private AnimationCurve jumpFallOff;
[SerializeField] private float jumpMultipliyer;
[SerializeField] private KeyCode jumpKey;
private bool isJumping;
private void Awake()
{
charController = GetComponent<CharacterController>();
}
private void Update()
{
PlayerMovement();
}
private void PlayerMovement()
{
float horziInput = Input.GetAxis(horizontalInputName) * movementSpeed;
float vertInput = Input.GetAxis(verticalInputName) * movementSpeed;
Vector3 forwardMovement = transform.forward * vertInput;
Vector3 rightMovement = transform.right * horziInput;
charController.SimpleMove(forwardMovement + rightMovement);
JumpInput();
}
private void JumpInput()
{
if(Input.GetKeyDown(jumpKey) && !isJumping)
{
isJumping = true;
StartCoroutine(JumpEvent());
}
}
private IEnumerator JumpEvent()
{
charController.slopeLimit = 90.0f;
float timeInAir = 0.0f;
do
{
float jumpForce = jumpFallOff.Evaluate(timeInAir);
charController.Move(Vector3.up * jumpForce * jumpMultipliyer * Time.deltaTime);
timeInAir += Time.deltaTime;
yield return null;
} while (!charController.isGrounded && charController.collisionFlags != CollisionFlags.Above);
charController.slopeLimit = 45.0f;
isJumping = false;
}
Related
I'm currently working on a FPS "Speedrun" game & I want to add "Sliding" to the players movement. I already added "Crouching", but currently I'm stuck at getting the sliding done. (I'm using the Character Controller) I've tried many things, but nothing really worked. Heres the current PlayerMovement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
private Rigidbody rb;
public Transform cameraTransform;
public HeadBobController headBobController;
[Header("Movement")]
private float speedMultiplier = 1f;
public float speed = 12f;
public float sprintSpeed = 20f;
public float slopeSpeed = 0.01f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float sensitivity = 2f;
public float cameraRotationLimit = 85f;
[Header("Countermovement")]
public float counterMovement = 0.175f;
private float threshold = 0.01f;
public float maxSlopeAngle = 35f;
[Header("Sliding / Crouch")]
public float slideSpeed;
float originalHeight;
public float reducedHeight;
float targetHeight = 2f;
public float timeToCrouch = 0.25f;
float timeElapsed = 0;
bool canStopCrouching;
bool wantStopCrouch;
bool isRunning = false;
public bool isCrouching = false;
bool canUseHeadbob = true;
bool WillSlideOnSlopes = true;
bool IsSliding
{
get
{
if (controller.isGrounded && Physics.Raycast(transform.position, Vector3.down, out RaycastHit slopeHit, 3f))
{
hitPointNormal = slopeHit.normal;
return Vector3.Angle(hitPointNormal, Vector3.up) > controller.slopeLimit;
} else
{
return false;
}
}
}
[Header("Headbob")]
[SerializeField] private float walkBobSpeed = 14f;
[SerializeField] private float walkBobAmount = 0.05f;
[SerializeField] private float sprintBobSpeed = 18f;
[SerializeField] private float sprintBobAmount = 0.11f;
[SerializeField] private float crouchBobSpeed = 8f;
[SerializeField] private float crouchBobAmount = 0.025f;
private float defaultYPos = 0;
private float timer;
private Vector3 hitPointNormal;
[Header("Ground Check")]
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
public bool isGrounded;
[Header("Camera")]
float cameraRotationX = 0f;
float cameraRotationY = 0f;
public Camera camera;
public Transform orientation;
[Header("Other")]
public GameObject speedlines;
public ParticleSystem jumpParticle;
void Start()
{
rb = GetComponent<Rigidbody>();
controller = GetComponent<CharacterController>();
originalHeight = controller.height;
defaultYPos = camera.transform.localPosition.y;
//Cursor.lockState = CursorLockMode.Locked;
//Cursor.visible = false;
}
public void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
move = Vector3.ClampMagnitude(move, 1f);
if (Input.GetKey(KeyCode.LeftShift) && !isCrouching)
{
controller.Move(move * sprintSpeed * Time.deltaTime);
isRunning = true;
headBobController.amplitude = 0.1f;
headBobController.frequency = 30f;
} else
{
controller.Move(move * speed * speedMultiplier * Time.deltaTime);
isRunning = false;
headBobController.amplitude = 0.0426f;
headBobController.frequency = 19.1f;
}
if (Physics.Raycast(camera.transform.position, Vector3.up, 1f))
{
canStopCrouching = false;
} else
{
canStopCrouching = true;
}
if (canStopCrouching && wantStopCrouch && !Physics.Raycast(camera.transform.position, Vector3.up, 1f))
{
wantStopCrouch = false;
StopCrouch();
}
if (Input.GetKeyDown(KeyCode.LeftControl) && isGrounded)
{
//controller.height = Mathf.Lerp(reducedHeight, originalHeight, timeElapsed);
StartCrouch();
} else if(Input.GetKeyUp(KeyCode.LeftControl) && isGrounded && !Physics.Raycast(camera.transform.position, Vector3.up, 1f))
{
//controller.height = Mathf.Lerp(originalHeight, targetHeight, timeElapsed);
StopCrouch();
} else if(Input.GetKeyUp(KeyCode.LeftControl) && isGrounded && Physics.Raycast(camera.transform.position, Vector3.up, 1f))
{
wantStopCrouch = true;
}
if (Input.GetKey(KeyCode.LeftControl) && isRunning && isGrounded)
{
//Sliding
}
if (canUseHeadbob && isGrounded)
{
if (Mathf.Abs(move.x) > 0.1f || Mathf.Abs(move.z) > 0.1f)
{
timer += Time.deltaTime * (isCrouching ? crouchBobSpeed : isRunning ? sprintBobSpeed : walkBobSpeed);
camera.transform.localPosition = new Vector3(camera.transform.localPosition.x, defaultYPos + Mathf.Sin(timer) * (isRunning ? sprintBobAmount : walkBobAmount), camera.transform.localPosition.z);
}
}
if(WillSlideOnSlopes && IsSliding)
{
move += new Vector3(hitPointNormal.x, -hitPointNormal.y, hitPointNormal.z) * slopeSpeed;
}
if (isRunning)
{
speedlines.active = true;
} else {
speedlines.active = false;
}
controller.Move(move * speed * speedMultiplier * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
controller.Move(-transform.forward * 0.1f);
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
jumpParticle.Play();
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
// Rotate the player based on mouse input
float mouseX = Input.GetAxis("Mouse X") * sensitivity;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity;
cameraRotationY += mouseX;
cameraRotationX -= mouseY;
cameraRotationX = Mathf.Clamp(cameraRotationX, -cameraRotationLimit, cameraRotationLimit);
cameraTransform.localEulerAngles = new Vector3(cameraRotationX, cameraRotationY, 0f);
transform.eulerAngles = new Vector3(0f, cameraRotationY, 0f);
}
private void StartCrouch()
{
controller.height = reducedHeight;
speedMultiplier = 0.5f;
isCrouching = true;
}
private void StopCrouch()
{
controller.height = originalHeight;
speedMultiplier = 1f;
isCrouching = false;
}
}
It would be very nice if anyone could try to explain me how to implement it into this code using the Character Controller! :)
I've tried multiple Youtube Tutorial solutions & a lot in Online Forums, nothing really worked for me for some reason. Often the system wouldn't even work (nothing happened). I want the player to have the crouching ability (which is implemented) & to slide when isRunning & when the player isGrounded
I am learning Unity, 3d free look camera and no errors, the only problem is the face and hands of the character doesn't rotate, only the armor of the character is rotating when I go to a different direction. I am using a free viking prefab from unity asset store as game character, the prefab has two child the face&hands and the armor. On the line "child = transform.GetChild(0).transform;" when I replace the 0 with 1 only the face and hands rotate. How can I make the whole prefab rotate when moving around?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Player playerInput;
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
[SerializeField]
private float playerSpeed = 2.0f;
[SerializeField]
private float jumpHeight = 1.0f;
[SerializeField]
private float gravityValue = -9.81f;
[SerializeField]
private float rotationSpeed = 4f;
private Transform cameraMain;
private Transform child;
private void Awake()
{
playerInput= new Player();
controller= GetComponent<CharacterController>();
}
private void OnEnable()
{
playerInput.Enable();
}
private void OnDisable()
{
playerInput.Disable();
}
private void Start()
{
cameraMain = Camera.main.transform;
child = transform.GetChild(0).transform;
}
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector2 movementInput= playerInput.PlayerMain.Move.ReadValue<Vector2>();
Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right *
movementInput.x);
move.y = 0f;
controller.Move(move * Time.deltaTime * playerSpeed);
// Changes the height position of the player..
if (playerInput.PlayerMain.Jump.triggered && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
if (movementInput != Vector2.zero)
{
Quaternion rotation = Quaternion.Euler(new Vector3(child.localEulerAngles.x,
cameraMain.localEulerAngles.y, child.localEulerAngles.z));
child.rotation = Quaternion.Lerp(child.rotation, rotation, Time.deltaTime *
rotationSpeed);
}
}
}
so i'm trying to achieve a player thats stops whenever the user lets go of the key, i don't want it to slide, i have a vertical variable and a horizontal variable that stores the axis, if these values are less or more than zero, than the bool isMoving is set to true, else it's set to false, this works wonderful on the vertical azis but not on the horizontal axis, i tried even making if statements checking when the keys are pressed and when they are not
like this
if(Input.GetKeyDown(KeyCode.A))
{
isMoving = true;
}
if(Input.GetKeyDown(KeyCode.D))
{
isMoving = true;
}
if(Input.GetKeyUp(KeyCode.A))
{
isMoving = false;
}
if(Input.GetKeyUp(KeyCode.D))
{
isMoving = false;
}
this also did not work,
this is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("Player Values")]
[SerializeField] private float playerSpeed = 50f;
[SerializeField] private float jumpHeight = 7f;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask ground;
private float checkLenght = 0.1f;
private Rigidbody rb;
private float horizontal;
private float vertical;
// Crouch Scales
private Vector3 initialScale;
private Vector3 crouchScale;
// Counter Movement
private bool isMoving;
private bool isOnGround;
private float counterDrag = 10f;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// Crouch Scales
initialScale = new Vector3(1f, 1f, 1f);
crouchScale = new Vector3(1f, 0.5f, 1f);
// Player Movement
MovePlayer();
// Counter Movement
ApplyCounterMovement();
// Check Ground
isOnGround = Physics.CheckSphere(groundCheck.position, checkLenght, ground);
}
private void Update()
{
CheckInput();
}
private void MovePlayer()
{
horizontal = Input.GetAxis("Horizontal") * playerSpeed;
vertical = Input.GetAxis("Vertical") * playerSpeed;
Vector3 movement = transform.forward * vertical + transform.right * horizontal;
rb.AddForce(movement, ForceMode.Acceleration);
}
private void CheckInput()
{
if(Input.GetKeyDown(KeyCode.LeftShift))
{
getDown();
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
getUp();
}
// Check Counter Movement
if (horizontal > 0f || horizontal < 0f)
{
isMoving = true;
}
else
{
isMoving = false;
}
if (vertical > 0f || vertical < 0f)
{
isMoving = true;
}
else
{
isMoving = false;
}
// Check Jump
if(Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
// Crouch
private void getDown()
{
transform.localScale = crouchScale;
transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
}
// Get Up From Crouching
private void getUp()
{
transform.localScale = initialScale;
transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
}
// Apply Counter Movement
private void ApplyCounterMovement()
{
if(isMoving == true)
{
rb.drag = 0f;
return;
}
if(isMoving == false)
{
rb.drag = counterDrag;
}
}
private void Jump()
{
if (isOnGround == false)
{
return;
}
if(isOnGround == true)
{
rb.AddForce(Vector3.up * jumpHeight, ForceMode.VelocityChange);
}
}
}
Your if checks for the vertical into overrule the ones before for the horizontal since both of them set the same field isMoving.
Rather combine them into e.g.
isMoving = horizontal != 0f || vertical != 0f;
I'm making a 2D platformer with Unity. I used this tutorial to write this code but changed it a little bit. I want to support both keyboard and gamepad, so I use the new Input System. I've defined three variables called GroundedRememeber, JumpPressedRemember and JumpPressedRememberTime and basically they work like timers and check if the player leaves the ground and then the player can jump when he is near the ground without need to touch it and I want to use it instead of famous "groundCheck". But the problem is that these timers are not working and the player can jump forever even in the air when I press jump button rapidly. Also, as you can see, I added a LayerMask named "groundLayers" for the player to jump only on this type of objects but when I choose "Ground" in the "groundLayers" slot in the Inspector, the player can't jump anymore.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour, PlayerInputActions.IPlayerActions
{
private PlayerInputActions controls;
[SerializeField] LayerMask groundLayers;
private Rigidbody2D rb;
private Animator anim;
private bool facingRight = true;
private Vector2 moveInput;
[SerializeField] private float jumpForce;
float JumpPressedRemember = 0;
[SerializeField] float JumpPressedRememberTime = 0.2f;
float GroundedRemember = 0;
[SerializeField] float GroundedRememberTime = 0.25f;
[SerializeField] float HorizontalAcceleration = 1;
[SerializeField] [Range(0, 1)] float HorizontalDampingBasic = 0.5f;
[SerializeField] [Range(0, 1)] float HorizontalDampingWhenStopping = 0.5f;
[SerializeField] [Range(0, 1)] float HorizontalDampingWhenTurning = 0.5f;
[SerializeField] [Range(0, 1)] float JumpHeight = 0.5f;
private void Awake()
{
controls = new PlayerInputActions();
controls.Player.SetCallbacks(this);
}
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void PlayerInputActions.IPlayerActions.OnMove(InputAction.CallbackContext context)
{
moveInput = context.ReadValue<Vector2>();
}
void Jump() {
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Force);
GroundedRemember = 0;
JumpPressedRemember = 0;
}
bool TryJump() {
if (GroundedRemember > 0) {
Jump();
return true;
} else {
JumpPressedRemember = JumpPressedRememberTime;
return false;
}
}
void PlayerInputActions.IPlayerActions.OnJump(InputAction.CallbackContext context)
{
jumpForce = context.ReadValue<float>();
switch (context.phase) {
case InputActionPhase.Performed:
TryJump();
break;
}
}
void FixedUpdate()
{
if(facingRight == false && moveInput.x > 0){
Flip();
}else if (facingRight == true && moveInput.x < 0){
Flip();
}
}
void Flip(){
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void OnEnable()
{
controls.Enable();
}
void OnDisable()
{
controls.Disable();
}
void Update()
{
Vector2 GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
Vector2 GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
bool Grounded = Physics2D.OverlapBox(GroundedBoxCheckPosition, transform.localScale, 0, groundLayers);
GroundedRemember -= Time.deltaTime;
if (Grounded)
{
GroundedRemember = GroundedRememberTime;
}
JumpPressedRemember -= Time.deltaTime;
if ((JumpPressedRemember > 0)) {
TryJump();
}
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
float HorizontalVelocity = rb.velocity.x;
HorizontalVelocity += moveInput.x;
if (Mathf.Abs(moveInput.x) < 0.01f)
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenStopping, Time.deltaTime * 10f);
else if (Mathf.Sign(moveInput.x) != Mathf.Sign(HorizontalVelocity))
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenTurning, Time.deltaTime * 10f);
else
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingBasic, Time.deltaTime * 10f);
rb.velocity = new Vector2(HorizontalVelocity, rb.velocity.y);
}
}
If you know how much the player jumps for, then try maybe adding a delay to the next jump
Maybe using the Invoke() function ur just using Coroutines if you know how to use them.
But i would still recommend using a Ground Check since it's practical and just easier and i don't see a reason why you wouldn't use it.
Solved - I ended up removing playerController.SimpleMove and having playerController.Move control all the player's movement.
So I have been trying to make my character wall jump between to walls. I've been able to make it work however, after each successful wall jump the height of the next jump decreases. This gets to the point where it starts wall jumping downwards. I have no idea why it does this. Before each time pressing the space bar, I am resetting the movement Vector3's to zero and then reapplying the correct values to the jump. I have even gone through the console to look at the vertical changes and neither moveDirection and moveAmount get a large enough value change to make this happen.
Because I am just trying this out, below is all the code that is affecting the player.
public float rotateSpeed = 3.0f;
public float walkSpeed = 5.0f;
public float runSpeed = 15.0f;
public float jumpSpeed = 10.0f;
public float acceleration = .05f;
public bool running = false;
public bool jumping = false;
public bool falling = false;
public bool onWall = false;
public bool wallJumping = false;
public bool stay = false;
private CharacterController playerController;
private Animator playerAnimator;
private float speed;
private Vector3 moveAmount;
private float animationSpeed;
private float currentSpeed;
private float targetSpeed;
private float moveSpeed;
float gravity = 10f;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start () {
playerController = GetComponent<CharacterController>();
playerAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
if (onWall)
{
playerAnimator.SetBool("Wall Holding", true);
jumping = false;
}
else
{
playerAnimator.SetBool("Wall Holding", false);
}
if (stay)
{
moveDirection = Vector3.zero;
moveAmount = Vector3.zero;
}
#region Player Controls
if (Input.GetButton("Vertical") && !wallJumping)
{
moveSpeed = (Input.GetKey(KeyCode.LeftShift)) ? runSpeed : walkSpeed;
moveAmount = transform.TransformDirection(Vector3.forward);
}
else
{
moveSpeed = 0;
moveAmount = Vector3.zero;
}
if (playerController.isGrounded){
moveDirection = Vector3.zero;
jumping = false;
wallJumping = false;
onWall = false;
stay = false;
}
else
{
if (!stay)
onWall = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (playerController.isGrounded)
{
moveDirection.y = jumpSpeed;
jumping = true;
}
else if (onWall)
{
moveDirection = Vector3.zero;
moveAmount = Vector3.zero;
wallJumping = true;
moveDirection += transform.forward * jumpSpeed * 2f;
moveDirection += transform.up * jumpSpeed * 2f;
stay = false;
}
}
if (Input.GetKeyDown(KeyCode.F))
{
if (playerAnimator.GetBool("fight"))
{
playerAnimator.SetBool("fight", false);
}
else
{
playerAnimator.SetBool("fight", true);
}
}
#endregion Player Controls
moveDirection.y -= gravity * Time.deltaTime;
Debug.Log(moveDirection.x);
targetSpeed = Input.GetAxis("Vertical") * moveSpeed;
currentSpeed = SpeedFactor(targetSpeed, currentSpeed, acceleration);
if (currentSpeed > walkSpeed)
running = true;
else
running = false;
playerAnimator.SetFloat("movespeed", currentSpeed);
if (!stay)
{
playerController.SimpleMove(currentSpeed * moveAmount);
playerController.Move(moveDirection * Time.deltaTime);
}
}
private float SpeedFactor (float targetSpeed, float currentSpeed, float dilation) {
if ( currentSpeed < targetSpeed) {
currentSpeed += dilation;
if ( currentSpeed > targetSpeed) {
currentSpeed = targetSpeed;
}
}
else if ( currentSpeed > targetSpeed) {
currentSpeed -= dilation;
if ( currentSpeed < targetSpeed) {
currentSpeed = targetSpeed;
}
}
return currentSpeed;
}
void OnControllerColliderHit (ControllerColliderHit hit) {
if (hit.gameObject.tag == "JumpingWall" && !playerController.isGrounded && !onWall && (jumping || wallJumping))
{
onWall = true;
transform.Rotate(0,180,0);
stay = true;
}
}
The simplest explanation is that transform.forward and/or transform.up vectors begin to diverge from your expectations over time. This would explain how it even seems to be jumping "backwards" eventually when you apply the lines below. Are you sure these vectors are oriented as you expect after the jumper collides with a wall (prior to the next jump)?
moveDirection += transform.forward * jumpSpeed * 2f;
moveDirection += transform.up * jumpSpeed * 2f;