Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision bug?

Discussion in 'Physics' started by Nightm4reProds, Jan 21, 2019.

  1. Nightm4reProds

    Nightm4reProds

    Joined:
    Oct 11, 2017
    Posts:
    7
    Well, Im just trying to create my own FPS Controller for my game to learn and well, you know, knowing what Im using. The thing is that I cant avoid the collider to go into the walls or, for example, if theres an spherical collider in the air, burying itself in the ground. Here's my script and my config. Jugador has rigidbody and box collider, Cube just has collider but even deactivating it still happens the same.

    EDIT: You can see in the script that I've tried all the "movement" methods I have found.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class cameraAdmin : MonoBehaviour
    5. {
    6. public GameObject Target;
    7. Camera cameraPlayerCam;
    8. private float sensibility = 1.5f;
    9. float v;
    10. float h;
    11. private float velocidad = 0.01f;
    12. private float run;
    13. public float fuerzaSalto = 150f;
    14. public bool isGrounded;
    15. Ray rayAim;
    16. private Rigidbody selfRigidbody;
    17. private RaycastHit hit;
    18. // Start is called before the first frame update
    19. void Start()
    20. {
    21. if (GameObject.FindGameObjectWithTag("Player"))
    22. {
    23. Target = GameObject.FindGameObjectWithTag("Player");
    24. }
    25. selfRigidbody = Target.GetComponent<Rigidbody>();
    26. cameraPlayerCam = GetComponent<Camera>();
    27. cameraPlayerCam.transform.parent = Target.transform;
    28. }
    29. // Update is called once per frame
    30. void Update()
    31. {
    32. h = sensibility * Input.GetAxis("Mouse X");
    33. v = sensibility * Input.GetAxis("Mouse Y");
    34. Target.transform.Rotate(0, h, 0);
    35. cameraPlayerCam.transform.Rotate(-v, 0, 0);
    36. rayAim = cameraPlayerCam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
    37. cameraPlayerCam.transform.localPosition = new Vector3(0, 0.08f, 0.03f);
    38. /*
    39. if (Input.GetKey(KeyCode.W))
    40. {
    41. Target.transform.Translate(0, 0, velocidad * run);
    42. }
    43. if (Input.GetKey(KeyCode.D))
    44. {
    45. Target.transform.Translate(velocidad * run, 0, 0);
    46. }
    47. if (Input.GetKey(KeyCode.A))
    48. {
    49. Target.transform.Translate(-velocidad * run, 0, 0);
    50. }
    51. if (Input.GetKey(KeyCode.S))
    52. {
    53. Target.transform.Translate(0, 0, -velocidad * run);
    54. }
    55. */
    56. if (Input.GetKey(KeyCode.LeftShift))
    57. {
    58. run = 2f;
    59. }
    60. else
    61. {
    62. run = 1f;
    63. }
    64. if (Input.GetKey(KeyCode.V))
    65. {
    66. if (GameObject.FindGameObjectWithTag("Player") && !Target.CompareTag("Player"))
    67. {
    68. Target = GameObject.FindGameObjectWithTag("Player");
    69. cameraPlayerCam.transform.parent = Target.transform;
    70. cameraPlayerCam.transform.rotation = Target.transform.rotation;
    71. selfRigidbody = Target.GetComponent<Rigidbody>();
    72. }
    73. }
    74. if ((Input.GetKeyDown(KeyCode.Space)) && (isGrounded))
    75. {
    76. selfRigidbody.AddForce(0, fuerzaSalto, 0);
    77. }
    78. if (Input.GetKeyDown(KeyCode.F))
    79. {
    80. if (Physics.Raycast(rayAim, out hit, 15f))
    81. {
    82. if (hit.collider.CompareTag("NPC"))
    83. {
    84. Target = GameObject.Find(hit.collider.name);
    85. cameraPlayerCam.transform.parent = Target.transform;
    86. cameraPlayerCam.transform.rotation = Target.transform.rotation;
    87. selfRigidbody = Target.GetComponent<Rigidbody>();
    88. }
    89. }
    90. }
    91. }
    92. private void FixedUpdate()
    93. {
    94. float x = Input.GetAxisRaw("Horizontal");
    95. float z = Input.GetAxisRaw("Vertical");
    96. selfRigidbody.position += z * transform.forward * Time.deltaTime * 1f;
    97. selfRigidbody.position += x * transform.right * Time.deltaTime * 1f;
    98. /*
    99. if (Input.GetKey(KeyCode.W))
    100. {
    101. selfRigidbody.MovePosition(transform.position + (transform.forward * Time.deltaTime * velocidad));
    102. }
    103. if (Input.GetKey(KeyCode.D))
    104. {
    105. selfRigidbody.MovePosition(new Vector3(velocidad * run, 0, 0));
    106. }
    107. if (Input.GetKey(KeyCode.A))
    108. {
    109. selfRigidbody.MovePosition(new Vector3(-velocidad * run, 0, 0));
    110. }
    111. if (Input.GetKey(KeyCode.S))
    112. {
    113. selfRigidbody.MovePosition(new Vector3(0, 0, -velocidad * run));
    114. }*/
    115. }
    116. }
     

    Attached Files:

  2. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    Did you try changing the collision detection mode ? If your object is fast and collision detection is set to discrete, it sometimes clip. Try setting it to continuous dynamic (but if you have a lot of objects like that it could have an impact on performance)
     
  3. Nightm4reProds

    Nightm4reProds

    Joined:
    Oct 11, 2017
    Posts:
    7
    Yes. I've tried all the collision modes with almost all the combinations between both objects.
     
  4. Nightm4reProds

    Nightm4reProds

    Joined:
    Oct 11, 2017
    Posts:
    7
    No one, really...?
     
  5. JamesLD

    JamesLD

    Joined:
    Dec 28, 2018
    Posts:
    1
    Try moving the Rigidbody using the AddForce command, instead of updating the positions. Maybe it's forcing the player into a position and overriding any physics forces
     
  6. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    Hi, the first step in the character controller journey is to go for one of the two most common solutions/approaches, dynamic rigidbody or kinematic rigidbody. After reading your code i can tell that you have the dynamic rigidbody but you are setting it position manually, frame by frame (fixedUpdate), like a Kinematic controller works.
    Setting the position frame by frame gives you total control but probably will bring problems regarding collision detection, i don't know if you always have these problems or in certain situations, maybe when moving too fast ?

    Anyway try to decide first the approach. If you go with the dynamic rigidbody apply forces directly to the it(or change the rigidbody.velocity) ...

    If you go with the kinematic rigidbody, well, good luck! Every good and decent controller out there is kinematic ... at least for "serious games". Actually there is a nice workaround, use the depenetration method Physics.ComputePenetration and "depenetrate" the body from colliders (See the doc, there is a nice example there to start with). The movement of course has to be performed in a "Kinematic way", for example via Translate method or rigidbody.Move.
     
    Nightm4reProds likes this.
  7. Nightm4reProds

    Nightm4reProds

    Joined:
    Oct 11, 2017
    Posts:
    7
    Sorry I've been afk for so long. I was centrated in other projects but, yeah, I solved it using Rigidbody.MovePosition instead of Transform.translate. . Thank you so much for your help tho, didn't know about "Kinematic way" and "Rigidbody way".