Search Unity

i have a problem in my code can sum1 help

Discussion in 'Getting Started' started by mouusa2017, Jan 7, 2023.

  1. mouusa2017

    mouusa2017

    Joined:
    Jun 15, 2022
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class FPSController : MonoBehaviour
    {
    public float speed = 10.0f;
    public float sprintSpeed = 15.0f;
    public float crouchSpeed = 5.0f;
    public float jumpSpeed = 10.0f;
    public float gravity = 20.0f;
    public float slideSpeed = 10.0f;
    public float slideThreshold = 45.0f;
    public float slideCounterMovement = 0.2f;
    public float dashSpeed = 20.0f;
    public float dashDuration = 0.5f;
    public float dashCooldown = 1.0f;

    private bool isCrouching;
    private bool isSprinting;
    private bool isSliding;
    private bool isDashing;
    private float dashTimer;
    private float dashCoolTimer;
    private Vector3 moveDirection = Vector3.zero;
    private Vector3 slideDirection = Vector3.zero;
    private Vector3 dashDirection = Vector3.zero;




    void Update()
    {
    CharacterController controller = GetComponent<CharacterController>();

    if (controller.isGrounded)
    {
    Debug.Log("grounded");
    }
    if (controller.isGrounded)
    {
    // We are grounded, so recalculate
    // move direction directly from axes
    moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);

    if (isCrouching)
    {
    moveDirection *= crouchSpeed;
    }
    else if (isSprinting)
    {
    moveDirection *= sprintSpeed;
    }
    else
    {
    moveDirection *= speed;
    }

    if (Input.GetButton("Jump"))
    {
    moveDirection.y = jumpSpeed;
    }

    // Reset sliding
    isSliding = false;
    }
    else
    {
    // If we are falling
    if (moveDirection.y < 0)
    {
    // Check if we are pressing crouch
    if (Input.GetButton("Crouch"))
    {
    // Check if the slope angle is below the threshold
    if (IsGroundSlopeLessThan(slideThreshold))
    {
    // Start sliding
    isSliding = true;

    // Calculate the direction we want to slide in
    slideDirection = Vector3.Slerp(moveDirection, -Vector3.up, slideCounterMovement);

    // Reset y component of moveDirection
    moveDirection.y = 0;
    }
    }
    }
    }

    if (isSliding)
    {
    // Apply slideDirection
    moveDirection = slideDirection;

    // Multiply by slideSpeed
    moveDirection *= slideSpeed;
    }

    if (isDashing)
    {
    // Apply dashDirection
    moveDirection = dashDirection;

    // Multiply by dashSpeed
    moveDirection *= dashSpeed;

    // Decrement dashTimer by deltaTime
    dashTimer -= Time.deltaTime;

    // Check if dashTimer has expired
    if (dashTimer <= 0.0f)
    {
    // Reset dash
    isDashing = false;
    dashDirection = Vector3.zero;
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
    }

    bool IsGroundSlopeLessThan(float threshold)
    {
    RaycastHit hit;
    if (Physics.Raycast(transform.position, Vector3.down, out hit))
    {
    return Vector3.Angle(hit.normal, Vector3.up) < threshold;
    }
    return false;
    }
    }
    }
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Such a post is not a request for help, but a simple do it for me. At least describe the problem and put the code in the appropriate tag to make it readable.
     
  3. mouusa2017

    mouusa2017

    Joined:
    Jun 15, 2022
    Posts:
    2
    ok i solved it
    thanks for the advice
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    In the future, I would ask that you please use code-tags when posting code and not plain text.