I am working on a rigidbody controller and as of right now it has some issues I cant solve. I would like my movement to be continous and if though i am checking if the player is grounded it is still jumping while in the air. Thanks in advance for any feedback its greatly appreciated!
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
private Rigidbody _playerRB;
private PlayerInputSystem _playerInput;
private float playerIdNumber;
private string uniquePlayName;
private float jumpForce = 10.0f;
[Header("IsGounded")]
public LayerMask whatIsGround;
public Transform groundCheck;
private float groundCheckRadius = 0.1f;
private float speed = 10.0f;
private float acceleration = 1.0f;
private float currentSpeed;
private void Awake()
{
_playerRB = GetComponent<Rigidbody>();
_playerInput = new PlayerInputSystem();
_playerInput.Player.Enable();
}
private void OnEnable()
{
_playerInput.Player.Jump.performed += Jump;
_playerInput.Player.Move.performed += Move;
}
private void OnDisable()
{
_playerInput.Player.Jump.performed -= Jump;
_playerInput.Player.Move.performed -= Move;
}
private void Jump(InputAction.CallbackContext obj)
{
if (Physics.CheckSphere(groundCheck.position, groundCheckRadius, whatIsGround))
{
_playerRB.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
private void Move(InputAction.CallbackContext obj)
{
float horizontal = obj.ReadValue<Vector2>().x;
float vertical = obj.ReadValue<Vector2>().y;
transform.rotation *= Quaternion.Euler(new Vector3(0, horizontal, 0));
currentSpeed += vertical * acceleration * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed, 0, speed);
transform.position += transform.right * currentSpeed * Time.deltaTime;
}
}
Again thanks in advance for the help.
I'm trying to make move script but it seems impossible to me because I stared at code for like an hour I even rewrote it but same problem. It appears that after I added gravity and groundcheck things, my character cant run or even walk at set speed (he is moving very slow). Can someone please help me with it please cuz' I'm lost
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//VARIABLES
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
private Vector3 moveDirection;
private Vector3 velocity;
[SerializeField] private bool isGrounded;
[SerializeField] private float groundCheckDistance;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float gravity;
//REFERENCES
private CharacterController controller;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
Move();
}
private void Move()
{
isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float moveZ = Input.GetAxis("Vertical");
moveDirection = new Vector3(0, 0, moveZ);
if(isGrounded)
{
if(moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
Walk();
}
else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
Run();
}
else if(moveDirection == Vector3.zero)
{
Idle();
}
moveDirection *= moveSpeed;
}
controller.Move(moveDirection * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void Idle()
{
}
private void Walk()
{
moveSpeed = walkSpeed;
}
private void Run()
{
moveSpeed = runSpeed;
}
}
I found the problem. Problem was with spawn area or call it how you want. If you'll move further the problem is no longer there.
Look at this video here: https://youtu.be/xfxgwmuPQsA
I'm not sure if someone is going to help me with this problem but here is my issue any ways
My player movement script was working perfectly fine before I enter this code and I'm pretty sure it's something in my script from the past that is causing my character mesh to have a huge offset downwards from my character controller yes, I have the curves set up probably here is the Ik tutorial I followed to get this issue. Thanks for your help you're a life saver for anyone who does help. Here is the video https://www.youtube.com/watch?v=MonxKdgxi2w
public class PlayerMovement : MonoBehaviour
{
#region Variables
private Vector3 Velocity;
private Vector3 PlayerMovementInput;
private Vector2 PlayerMouseInput;
private float xRot;
private CharacterController controller;
public Transform PlayerCamera;
Animator anim;
public float DefaultSpeed;
public float Speed;
public float RunSpeed;
public float Jumpforce;
public float Sensitivity;
public float Gravity = -9.81f;
private Vector3 rightFootPosition, leftFootPosition, leftFootIKPosition, rightFootIkPosition;
private Quaternion leftFootIkRotation, rightFootIKRotation;
private float lastPekvisPositionY, lastRightFootPositioonY, lastLeftFootPositionY;
[Header("Feet Grounder")]
public bool enableFeetIk = true;
[Range(0, 2)] [SerializeField] private float heightFromGroundRaycast = 1.14f;
[Range(0, 2)] [SerializeField] private float raycastDownDIstance = 1.5f;
[SerializeField] private LayerMask environmentLayer;
[SerializeField] private float pelvisOffset = 0f;
[Range(0, 1)] [SerializeField] private float pelvisUpAndDownSpeed = 0.28f;
[Range(0, 1)] [SerializeField] private float feetToIkPositionSpeed = 0.5f;
public string LeftFotAnimVariableName = "LeftFootCurve";
public string rightFootAnimVariableName = "RightFootCurve";
public bool useProIkFeature = false;
public bool showSolverDebug = true;
#endregion
# region Initialization
private void Start()
{
controller = GetComponentInChildren<CharacterController>();
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
anim = GetComponentInChildren<Animator>();
}
private void Update()
{
PlayerMovementInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
PlayerMouseInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
MovePlayer();
MoveCamera();
}
#endregion
#region PlayerMovement
private void MovePlayer()
{
Vector3 MoveVector = transform.TransformDirection(PlayerMovementInput);
if (PlayerMovementInput != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
Speed = DefaultSpeed;
//WALK
anim.SetFloat("Speed", 0.5f, 0.1f, Time.deltaTime);
}
else if (PlayerMovementInput != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
Speed = RunSpeed;
//RUN
anim.SetFloat("Speed", 1, 0.1f, Time.deltaTime);
}
else if (PlayerMovementInput == Vector3.zero)
{
//IDLE
anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);
}
if (controller.isGrounded)
{
Velocity.y = -1f;
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Jump());
Velocity.y = Jumpforce;
}
}
else
{
Velocity.y -= Gravity * -2f * Time.deltaTime;
}
controller.Move(MoveVector * Speed * Time.deltaTime);
controller.Move(Velocity * Time.deltaTime);
}
private void MoveCamera()
{
xRot -= PlayerMouseInput.y * Sensitivity;
transform.Rotate(0f, PlayerMouseInput.x * Sensitivity, 0f);
PlayerCamera.transform.localRotation = Quaternion.Euler(xRot, 0f, 0f);
xRot = Mathf.Clamp(xRot, -90f, 90f);
}
private IEnumerator Jump()
{
anim.SetLayerWeight(anim.GetLayerIndex("Jump Layer"), 1);
anim.SetTrigger("Jump");
yield return new WaitForSeconds(0.983f);
anim.SetLayerWeight(anim.GetLayerIndex("Jump Layer"), 0);
}
#endregion
#region FeetGrounding
private void FixedUpdate()
{
if(enableFeetIk == false) { return; }
if( anim == null) { return; }
AdjustFeetTarget(ref rightFootPosition, HumanBodyBones.RightFoot);
AdjustFeetTarget(ref leftFootPosition, HumanBodyBones.LeftFoot);
FeetPositionSolver(rightFootPosition, ref rightFootIkPosition, ref rightFootIKRotation);
FeetPositionSolver(leftFootPosition, ref leftFootIKPosition, ref leftFootIkRotation);
}
private void OnAnimatorIK(int layerIndex)
{
if (enableFeetIk == false) { return; }
if (anim == null) { return; }
MovePelvisHeight();
anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1);
if(useProIkFeature)
{
anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, anim.GetFloat(rightFootAnimVariableName));
}
MoveFeetToIkPoint(AvatarIKGoal.RightFoot, rightFootIkPosition, rightFootIKRotation, ref lastRightFootPositioonY);
anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1);
if (useProIkFeature)
{
anim.SetIKPositionWeight(AvatarIKGoal.LeftFoot, anim.GetFloat(LeftFotAnimVariableName));
}
MoveFeetToIkPoint(AvatarIKGoal.LeftFoot, leftFootIKPosition, leftFootIkRotation, ref lastLeftFootPositionY);
}
#endregion
#region FeetGroundingMethods
void MoveFeetToIkPoint (AvatarIKGoal foot, Vector3 positionIkHolder, Quaternion rotationIkHolder, ref float lastFootPositionY)
{
Vector3 targetIkPosition = anim.GetIKPosition(foot);
if(positionIkHolder != Vector3.zero)
{
targetIkPosition = transform.InverseTransformDirection(targetIkPosition);
positionIkHolder = transform.InverseTransformPoint(positionIkHolder);
float yVariable = Mathf.Lerp(lastFootPositionY, positionIkHolder.y, feetToIkPositionSpeed);
targetIkPosition.y += yVariable;
lastFootPositionY = yVariable;
targetIkPosition = transform.TransformPoint(targetIkPosition);
anim.SetIKRotation(foot, rotationIkHolder);
}
anim.SetIKPosition(foot, targetIkPosition);
}
private void MovePelvisHeight()
{
if(rightFootIkPosition == Vector3.zero || leftFootIKPosition == Vector3.zero || lastPekvisPositionY == 0)
{
lastPekvisPositionY = anim.bodyPosition.y;
return;
}
float lOffsetPosition = leftFootIKPosition.y = transform.position.y;
float rOffsetPosition = rightFootIkPosition.y = transform.position.y;
float totalOffset = (lOffsetPosition < rOffsetPosition) ? lOffsetPosition : rOffsetPosition;
Vector3 newPelvisPosition = anim.bodyPosition + Vector3.up * totalOffset;
newPelvisPosition.y = Mathf.Lerp(lastPekvisPositionY, newPelvisPosition.y, pelvisUpAndDownSpeed);
anim.bodyPosition = newPelvisPosition;
lastPekvisPositionY = anim.bodyPosition.y;
}
private void FeetPositionSolver(Vector3 fromSkyPosition, ref Vector3 feetIkPositon, ref Quaternion feetIkRotations)
{
RaycastHit feetOutHit;
if (showSolverDebug)
Debug.DrawLine(fromSkyPosition, fromSkyPosition + Vector3.down * (raycastDownDIstance + heightFromGroundRaycast), Color.yellow);
if(Physics.Raycast(fromSkyPosition, Vector3.down, out feetOutHit, raycastDownDIstance + heightFromGroundRaycast, environmentLayer ))
{
feetIkPositon = fromSkyPosition;
feetIkPositon.y = feetOutHit.point.y + pelvisOffset;
feetIkRotations = Quaternion.FromToRotation(Vector3.up, feetOutHit.normal) * transform.rotation;
return;
}
feetIkPositon = Vector3.zero;
}
private void AdjustFeetTarget (ref Vector3 feetPositions, HumanBodyBones foot)
{
feetPositions = anim.GetBoneTransform(foot).position;
feetPositions.y = transform.position.y + heightFromGroundRaycast;
}
#endregion
}
The problem is that when I import the code to the character it does not add the Player Input automatically and also it doesnt move at all when I press neither the controller nor the keyboard, Ive checked the code so many times but ill paste it here, it doesnt work if I add the player input manually either :(
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(PlayerInput))]
public class TwinStickMovement : MonoBehaviour
{
[SerializeField] private float playerSpeed = 5f;
[SerializeField] private float gravityValue = -9.81f;
[SerializeField] private float controllerDeadzone = 0.1f;
[SerializeField] private float gamepadRotateSmoothing = 1000f;
[SerializeField] private bool isGamepad;
private CharacterController controller;
private Vector2 movement;
private Vector2 aim;
private Vector3 playerVelocity;
private PlayerControls playerControls;
private PlayerInput playerInput;
private void Awake()
{
controller = GetComponent<CharacterController>();
playerControls = new PlayerControls();
playerInput = GetComponent<PlayerInput>();
}
private void OnEnable()
{
playerControls.Enable();
}
private void OnDisable()
{
playerControls.Disable();
}
void Update()
{
HandleInput();
HandleMovement();
HandleRotation();
}
void HandleInput()
{
movement = playerControls.Controls.Movement.ReadValue<Vector2>();
aim = playerControls.Controls.Aim.ReadValue<Vector2>();
}
void HandleMovement()
{
Vector3 move = new Vector3(movement.x, 0, movement.y);
controller.Move(move * Time.deltaTime * playerSpeed);
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
void HandleRotation()
{
}
}```
I created a movable 2D ball character but when it stops moving the ball continues spinning for a bit without moving, how can I fix this?
Here is the existing code:
public class Controller : MonoBehaviour
{
public float TotalJumpForce;
public float TotalDropForce;
public float jumps;
public float speed;
public float jumpForce;
public float dropforce;
bool isGrounded = false;
public Transform isGroundedChecker;
public float checkGroundRadius;
public LayerMask groundLayer;
Rigidbody2D rb;
void Start()
{
stalltimer = 1000;
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
TotalJumpForce = rb.velocity.y + jumpForce;
TotalDropForce = rb.velocity.y + dropforce;
CheckIfGrounded();
Move();
Jump();
Drop();
}
void Move()
{
float x = Input.GetAxisRaw("Horizontal");
float moveBy = x * speed;
rb.velocity = new Vector2(moveBy, rb.velocity.y);
}
void Jump()
{
if (Input.GetKeyDown("w"))
{
if (jumps > 0)
rb.velocity = new Vector2(rb.velocity.x, TotalJumpForce);
jumps = jumps - 1;
}
}
void Drop()
{
if (Input.GetKeyDown("s"))
{
rb.velocity = new Vector2(rb.velocity.x, TotalDropForce);
}
}
void CheckIfGrounded()
{
Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
if (collider != null)
{
isGrounded = true;
jumps = 2;
stalls = 3;
}
else
{
isGrounded = false;
}
}
}
Changing angular and linear drag doesn't make a difference. Changing the gravity scale doesnt seem to make a diffrence either.
You could check for input and reset the rb.angularVelocity in case there is no input
void Move()
{
var x = Input.GetAxisRaw("Horizontal");
var moveBy = x * speed;
rb.velocity = new Vector2(moveBy, rb.velocity.y);
if(Mathf.Approximately(move, 0f))
{
rb.angularVelocity = 0;
}
}