Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as HTML by Jai ( 2 years ago )
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpForce = 10f;
    public float wallJumpForce = 10f;
    public float wallSlideSpeed = 2f;
    public float maxSlideVelocity = -5f;
    public float grappleRange = 10f;
    public LayerMask grappleMask;

    private Rigidbody rb;
    private bool isGrounded;
    private bool isWallSliding;
    private bool isWallJumping;
    private Vector3 wallNormal;
    private bool isGrappling;
    private Vector3 grapplePoint;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        // Player movement
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 moveDirection = new Vector3(moveHorizontal, 0f, moveVertical).normalized;
        rb.velocity = new Vector3(moveDirection.x * moveSpeed, rb.velocity.y, moveDirection.z * moveSpeed);

        // Jumping
        if (isGrounded && Input.GetButtonDown("Jump"))
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }

        // Wall sliding and wall jumping
        if (isWallSliding)
        {
            rb.velocity = new Vector3(rb.velocity.x, Mathf.Clamp(rb.velocity.y, maxSlideVelocity, float.MaxValue), rb.velocity.z);
            if (Input.GetButtonDown("Jump"))
            {
                rb.AddForce(wallNormal * wallJumpForce, ForceMode.Impulse);
            }
        }

        // Grappling hook
        if (Input.GetMouseButtonDown(0) && !isGrappling)
        {
            RaycastHit hit;
            if (Physics.Raycast(transform.position, transform.forward, out hit, grappleRange, grappleMask))
            {
                isGrappling = true;
                grapplePoint = hit.point;
                // Implement grappling behavior
            }
        }
    }

    void FixedUpdate()
    {
        // Check if player is grounded
        isGrounded = Physics.Raycast(transform.position, Vector3.down, 0.1f);
    }

    void OnCollisionEnter(Collision collision)
    {
        // Check if player is wall sliding
        foreach (ContactPoint contact in collision.contacts)
        {
            if (Vector3.Dot(contact.normal, Vector3.up) < 0.1f)
            {
                isWallSliding = true;
                wallNormal = contact.normal;
                break;
            }
        }
    }

    void OnCollisionExit(Collision collision)
    {
        // Reset wall sliding state
        isWallSliding = false;
        isWallJumping = false;
    }
}

 

Revise this Paste

Your Name: Code Language: