Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question trouble with collision

Discussion in 'Physics' started by Sekudo, Dec 23, 2022.

  1. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    basically, if i try to move forward near the very top of something, i end up getting to fly inside it, until i walk outside of it. my game is a 3rd person camera-relative parkour game. anyone able to figure out how i can fix this? my code is below, and a picture of the issue is attached.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public Transform cam;
    9.  
    10.     public float speed = 6f;
    11.     public float gravity = -9.81f;
    12.     public float jumpHeight = 3f;
    13.  
    14.     public Transform groundCheck;
    15.     public float groundDistance = 0.4f;
    16.     public LayerMask groundMask;
    17.  
    18.     Vector3 velocity;
    19.     bool isGrounded;
    20.  
    21.     public float turnSmoothTime = 0.1f;
    22.     float turnSmoothVelocity;
    23.  
    24.     private void Start()
    25.     {
    26.  
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.  
    33.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    34.  
    35.         if (isGrounded && velocity.y < 0)
    36.         {
    37.             velocity.y = -2f;
    38.         }
    39.  
    40.         float horizontal = Input.GetAxisRaw("Horizontal");
    41.         float vertical = Input.GetAxisRaw("Vertical");
    42.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    43.  
    44.         velocity.y += gravity * Time.deltaTime;
    45.  
    46.         controller.Move(velocity * Time.deltaTime);
    47.  
    48.         if (Input.GetButtonDown("Jump") && isGrounded)
    49.         {
    50.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    51.         }
    52.  
    53.         if (direction.magnitude >= 0.1f)
    54.         {
    55.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    56.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    57.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    58.  
    59.  
    60.  
    61.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    62.             controller.Move(moveDir.normalized * speed * Time.deltaTime);
    63.         }
    64.     }
    65. }
     

    Attached Files:

    Last edited: Dec 23, 2022
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    You should wrap your code in a Code Block first for better readability:

    upload_2022-12-23_18-47-24.png
     
  3. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    ty