Search Unity

Raytrace hit / Collider - Player sinks through terrain slowly like quicksand

Discussion in 'Physics' started by JohnnySix, Dec 24, 2018.

  1. JohnnySix

    JohnnySix

    Joined:
    Nov 9, 2015
    Posts:
    8
    Here's a video to demonstrate.



    I'm just checking a hit on a raytrace, setting a boolean to true when a hit is found, and only applying 'gravity' ( this is a user defined float variable ) to the vertical axis as a transform on the 'player' object.

    I am calculating the hit from the bottom of the object ( no skin ) as I haven't figured out how bounds work in 3d.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    show us your code, it could be a million and a half different things.
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Make sure you're not raycasting under the object, lift the raycast position a bit if so.
    Also, post your code, otherwise it's near impossible to tell what's actually going on.
     
  4. JohnnySix

    JohnnySix

    Joined:
    Nov 9, 2015
    Posts:
    8
    Thanks for replying guys, and Merry Christmas! :)

    I think it's something to do with the offset. I am/was tracing the raycast in the centre of the object, then assigning the offset distance from the centre to the bottom edge as a negation to the hit position, when finding where to 'push' it out the ground if it intersects slightly.

    I was trying to keep it simple - the end goal is simply box collision, but I was trying to avoid using all the character controller stuff as it'd be hugely over-engineered for what will simply be a box on a conveyor belt with a simple jumping mechanic.



    Script is here. Having said using a character controller would be over-engineered, the below is pretty messy...!

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [RequireComponent (typeof (Rigidbody))]
    7.  
    8. public class TestMoveSimple : MonoBehaviour {
    9.  
    10.     public LayerMask CollisionMask; // What can I hit ?
    11.     float XMov = 0f;
    12.     float YMov = 0f;
    13.     float ZMov = 0f;
    14.     public float MyGravity = -80f;
    15.     public float SkinWidth=0.45f; // offset ?::::
    16.     public static string MyState; // REPORT STATUS OF OBJECT
    17.     public float JumpCharge = 0f;
    18.     public float JumpChargeMax = 80f;
    19.     public Vector3 HitPos;  // position of hit :::
    20.     public Vector3 RayTraceOrigin ;
    21.     float CrossSize =2f; // Size of Debug cross
    22.  
    23.     //bools
    24.     private bool KeysDown;
    25.     private bool bHasHit=false;
    26.     private bool bOnGround=false;  //
    27.     private bool bJumpStart=false;  //
    28.     private bool bJumping=false;  //
    29.     private bool bFalling=false;  //
    30.  
    31.  
    32.     // Use this for initialization
    33.  
    34.     Rigidbody MyBox;
    35.     //    Canvas MyCanvas;
    36.     //    Text MyTextA;
    37.     //    Text MyTextB;
    38.  
    39.     void Start() {
    40.  
    41.         MyBox = GetComponent<Rigidbody> ();
    42.         MyBox.mass =1;
    43.         MyBox.useGravity = false;  
    44.         MyBox.isKinematic = true;
    45.         bJumping = false;
    46.     }
    47.    
    48.     // Update is called once per frame
    49.     void Update () {
    50.         KeysDown = false;
    51.  
    52.         if(Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter) ){
    53.             ResetAll ();
    54.         }
    55.  
    56.         if (Input.GetKeyUp (KeyCode.Space) && bOnGround) {
    57.  
    58.             YMov = YMov + JumpCharge;
    59.             JumpCharge = 0; // reset jump to 0 ::
    60.             bJumpStart =true;
    61.             bJumping = true;
    62.         }
    63.  
    64.         if (Input.GetKey (KeyCode.LeftArrow)) {
    65.             XMov += -2;
    66.             KeysDown = true;
    67.         }
    68.  
    69.         if (Input.GetKey (KeyCode.RightArrow)) {
    70.             XMov += 2;
    71.             KeysDown = true;
    72.         }
    73.  
    74.  
    75.         if (Input.GetKey (KeyCode.DownArrow)) {
    76.             ZMov += -2;
    77.             KeysDown = true;
    78.         }
    79.  
    80.         if (Input.GetKey (KeyCode.UpArrow)) {
    81.             ZMov += 2;
    82.             KeysDown = true;
    83.         }
    84.  
    85.         //jumping
    86.         if (Input.GetKey (KeyCode.Space) && bOnGround) {
    87.             JumpCharge += 0.2f +(JumpCharge*0.2f) ;
    88.  
    89.             if (JumpCharge > JumpChargeMax) {
    90.                 JumpCharge = JumpChargeMax; // clamnp jumpcharge
    91.             }
    92.             Debug.DrawRay (MyBox.transform.position + new Vector3 (0f,-0.5f, 0f), Vector3.up*JumpCharge, Color.cyan);
    93.  
    94.         }
    95.  
    96.  
    97.  
    98.         if (Input.GetKeyUp (KeyCode.Space) && bOnGround) {
    99.  
    100.             YMov = YMov + JumpCharge;
    101.             JumpCharge = 0; // reset jump to 0 ::
    102.             bJumpStart =true;
    103.             bJumping = true;
    104.  
    105.         }
    106.  
    107.  
    108.  
    109.  
    110.         DebugDraw ();
    111.         Move();
    112.         MyState += "\n" +"\n Box Pos"+ MyBox.position + "\n RT Origin" + RayTraceOrigin;
    113.         MyState += "\n Velocity" + new Vector3 (XMov * Time.deltaTime, YMov * Time.deltaTime, ZMov * Time.deltaTime);
    114.     }
    115.  
    116.     void MyCollisions() {
    117.         RaycastHit hit;
    118.         bHasHit = false;
    119.         bOnGround = false;
    120.         MyState = "Unknown";
    121.         RayTraceOrigin = MyBox.transform.position - new Vector3 (0f, SkinWidth , 0f);
    122.  
    123.     if (Physics.Raycast (RayTraceOrigin, Vector3.down*15f, out hit, 50f, CollisionMask)) {
    124.  
    125.             HitPos = hit.point;
    126.  
    127.             if (hit.distance < SkinWidth*1.2f && !bJumping) {
    128.                 MyState = " I am on the ground ";
    129.                 YMov = 0f;
    130.                 bHasHit = true;
    131.                 bOnGround = true;
    132.                 bFalling = false;
    133.                 MyBox.position = HitPos+ new Vector3 (0f, SkinWidth , 0f); ///
    134.  
    135.  
    136.             }
    137.         } else {
    138.    
    139.             bOnGround = false;
    140.    
    141.         }  
    142.  
    143.         if (!bOnGround) {
    144.             if (YMov <= 0) {
    145.                 MyState = " I am falling";
    146.                 bFalling = true;
    147.                 bJumping = false;
    148.             }
    149.             if (YMov > 0) {
    150.                 MyState = " I am Jumping";
    151.                 bJumping = true;
    152.                 bFalling = false;
    153.             }
    154.         }
    155.  
    156.         if (MyBox.position.y < -20) {
    157.             ResetAll ();
    158.         }
    159.     }
    160.  
    161.  
    162.     void DebugDraw (){
    163.  
    164.         // draw bounds of box
    165.  
    166.         // show origin //
    167.         Debug.DrawRay (RayTraceOrigin, Vector3.left*CrossSize*2, Color.blue);
    168.         Debug.DrawRay (RayTraceOrigin, Vector3.left*-CrossSize*2, Color.blue);
    169.         Debug.DrawRay (RayTraceOrigin, Vector3.forward*CrossSize*2, Color.blue);
    170.         Debug.DrawRay (RayTraceOrigin, Vector3.forward*-CrossSize*2, Color.blue);  
    171.  
    172.  
    173.         if (!bHasHit) {
    174.             Debug.DrawRay (RayTraceOrigin, Vector3.down * 50f, Color.blue);
    175.         }
    176.  
    177.         if (bHasHit && bFalling) {
    178.             Debug.DrawRay (HitPos, Vector3.left*CrossSize, Color.magenta);
    179.             Debug.DrawRay (HitPos, Vector3.left*-CrossSize, Color.magenta);
    180.             Debug.DrawRay (HitPos, Vector3.forward*CrossSize, Color.magenta);
    181.             Debug.DrawRay (HitPos, Vector3.forward*-CrossSize, Color.magenta);  
    182.  
    183.         }
    184.  
    185.  
    186.         if (bJumping && bHasHit) {
    187.             Debug.DrawRay (HitPos, Vector3.left*CrossSize, Color.white);
    188.             Debug.DrawRay (HitPos, Vector3.left*-CrossSize, Color.white);
    189.             Debug.DrawRay (HitPos, Vector3.forward*CrossSize, Color.white);
    190.             Debug.DrawRay (HitPos, Vector3.forward*-CrossSize, Color.white);  
    191.  
    192.         }
    193.  
    194.         if (bOnGround) {
    195.             Debug.DrawRay (HitPos, Vector3.left*CrossSize, Color.green);
    196.             Debug.DrawRay (HitPos, Vector3.left*-CrossSize, Color.green);
    197.             Debug.DrawRay (HitPos, Vector3.forward*CrossSize, Color.green);
    198.             Debug.DrawRay (HitPos, Vector3.forward*-CrossSize, Color.green);  
    199.  
    200.         }
    201.  
    202.     }
    203.  
    204.  
    205.  
    206.  
    207.     void Move() {
    208.  
    209.         MyCollisions();
    210.  
    211.         if (!KeysDown && Mathf.Abs( XMov)>0.2f  ) {
    212.             XMov = XMov * 0.8f;
    213.             if (Mathf.Abs (XMov) < 0.3f) {
    214.                 XMov = 0.0f;
    215.             }
    216.         }
    217.         if (!KeysDown && Mathf.Abs( ZMov)>0.2f  ) {
    218.             ZMov = ZMov * 0.8f;
    219.             if (Mathf.Abs (ZMov) < 0.3f) {
    220.                 ZMov = 0.0f;
    221.             }
    222.         }
    223.  
    224.         if ( !bOnGround) {
    225.             YMov += MyGravity*Time.deltaTime;  
    226.         }
    227.  
    228.  
    229.         if (bOnGround ) {
    230.             YMov = 0f;
    231.         }
    232.  
    233.         if (bJumpStart && !bOnGround) {
    234.             bJumpStart = false;
    235.         }
    236.         // :: now do movement ::
    237.         Vector3 MyVel ;
    238.  
    239.         MyVel = new Vector3 (XMov*Time.deltaTime, YMov*Time.deltaTime, ZMov * Time.deltaTime);
    240.         MyBox.transform.Translate (MyVel);
    241.  
    242.  
    243.     }
    244.  
    245.  
    246.     void ResetAll(){
    247.         Vector3 MyPos = new Vector3  (12f, 12f,-12f);
    248.         MyBox.position = MyPos;
    249.         MyBox.transform.SetPositionAndRotation (MyPos, Quaternion.identity);
    250.         XMov = 0f;
    251.         YMov = 0f;
    252.         ZMov = 0f;  
    253.     }
    254.  
    255.  
    256.  
    257.  
    258. }
    259.