FreshRSS

Normální zobrazení

Jsou dostupné nové články, klikněte pro obnovení stránky.
PředevčíremHlavní kanál
  • ✇Recent Questions - Game Development Stack Exchange
  • RigidBody floating in th airmartin suchodol
    I added a death animation among some other things in case of death. But after playing it the player is airborne, so I might need to change the pivot. But I'm wondering if this can't be done with some code The player's death is then handled by this code: private void Die() { Debug.Log(gameObject.name + " died."); isDead = true; isRegenerating = false; //gameObject.SetActive(false); player.AnimController.SetDeathState(); WorldController.Instance.
     

RigidBody floating in th air

I added a death animation among some other things in case of death. But after playing it the player is airborne, so I might need to change the pivot. But I'm wondering if this can't be done with some code

The player's death is then handled by this code:

private void Die()
    {
        Debug.Log(gameObject.name + " died.");
        isDead = true;
        isRegenerating = false;
        //gameObject.SetActive(false);
        player.AnimController.SetDeathState();
        WorldController.Instance.isGameOver = true;
        if (WorldController.Instance.ghostModerEnabled)
        {
            WorldController.Instance.isGhostMode = true;
        }
        //Destroy(gameObject);
    }

enter image description here

I originally thought it might be enough to manipulate the collider on the player to lay down along with the animation so the player stays lying in the air, but that doesn't really work

Edit: 0

The collider in the event of death does not exactly match the position of the player's body. Overall, it looks like this.

The player's character is made up of an empty object containing the character model from the mixamo. All scripts and the capsule collider broadcast on the empty object presenting the player. I know the colliders should look different. But it's something I haven't addressed yet. In case of death, the capsule collider stays in its original position. I tried applying the collider to the character model as well, but the situation is basically the same.

enter image description here

enter image description here

enter image description here

enter image description here

  • ✇Recent Questions - Game Development Stack Exchange
  • How can I check if the rigidbody is movingmartin suchodol
    The problem is that rb.velocity probably returns too small values using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovementController : MonoBehaviour { public float moveSpeed = 5f; public float sprintSpeed = 10f; public float crouchSpeed = 2.5f; public float jumpForce = 2f; public float rotationSpeed = 200f; public float dashSpeed = 100f; public float dashCooldown = 12f; public bool isGrounded; public bool isJu
     

How can I check if the rigidbody is moving

The problem is that rb.velocity probably returns too small values

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovementController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float sprintSpeed = 10f;
    public float crouchSpeed = 2.5f;
    public float jumpForce = 2f;
    public float rotationSpeed = 200f;
    public float dashSpeed = 100f;
    public float dashCooldown = 12f;
    public bool isGrounded;
    public bool isJumpAllowed = false;
    public KeyCode dashKey = KeyCode.Q;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    private Rigidbody rb;
    private Vector3 velocity;
    private bool isJumping = false;
    private bool isCrouching;
    private bool isSprinting;
    [SerializeField] private bool isDashing;
    [SerializeField] private float lastDashTime = 0f;
    
    private Player player = null;
    
    public void Init(Player pPlayer)
    {
        player = pPlayer;
    }
    
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        lastDashTime = -dashCooldown;
    }

    // Update is called once per frame
    public void UpdatePMC()
    {
        if (Input.GetButtonDown("Jump") && isGrounded && isJumpAllowed)
        {
            rb.AddForce(new Vector3(0f, jumpForce, 0f), ForceMode.VelocityChange);
            isJumping = true;
        } else {
            isJumping = false;
        }
        
        if ((Input.GetKeyDown(dashKey) && isGrounded) && Time.time - lastDashTime >= dashCooldown)
        {
            isDashing = true;
            lastDashTime = Time.time;
        }
        
        playerAnim();
    }
    
    public void FixedUpdatePMC()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = (transform.right * x + transform.forward * z).normalized;
        
        if (Input.GetKey(KeyCode.LeftShift))
        {
            rb.MovePosition(transform.position + move * sprintSpeed * Time.fixedDeltaTime);
            isSprinting = true;
        }
        else if (Input.GetKey(KeyCode.C))
        {
            rb.MovePosition(transform.position + move * crouchSpeed * Time.fixedDeltaTime);
            isCrouching = true;
        }
        else if (isDashing)
        {
            Vector3 dashDirection = transform.forward;
            RaycastHit dashHit;

          
            if (Physics.Raycast(transform.position, dashDirection, out dashHit, dashSpeed * Time.fixedDeltaTime))
            {
                rb.MovePosition(dashHit.point);
            }
            else
            {
                rb.MovePosition(transform.position + dashDirection + dashDirection * dashSpeed * Time.fixedDeltaTime);
                //rb.MovePosition(transform.position + move + dashDirection * dashSpeed * Time.fixedDeltaTime);
            }
           
            isDashing = false;
        } else if (isJumping)
        {
            
        }
        else
        {
            rb.MovePosition(transform.position + move * moveSpeed * Time.fixedDeltaTime);
            isSprinting = false;
            isCrouching = false;
        }

        
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        
        
        if (Physics.Raycast(ray, out hit, 100f, groundMask))
        {
            Vector3 targetPosition = hit.point;
            targetPosition.y = transform.position.y; 

            Vector3 direction = (targetPosition - transform.position).normalized;
            Quaternion lookRotation = Quaternion.LookRotation(direction);
            transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.fixedDeltaTime * rotationSpeed);
        }
    }

    private void playerAnim()
    {
        if (isGrounded)
        {
            Debug.Log("Player is moving: " + rb.velocity);
            if (rb.velocity != Vector3.zero)
            {
                if (isSprinting)
                {
                    player.AnimController.SetMovementState(MovingState.Running);
                } 
                else
                {
                    player.AnimController.SetMovementState(MovingState.Walking);
                }
            }
            else
            {
                player.AnimController.SetMovementState(MovingState.Idle);
            }
        }
        else
        {
            player.AnimController.SetMovementState(MovingState.Idle);
        }
    }
}

I also tried comparing with sqrMagnitude but the result is basically the same.

If I'm comparing velicity with vector. Sometimes the animation switches for a split second, because even if the player is not moving, there is still some movement.

❌
❌