#[SOLVED] Object Dragging Delay problem

1 messages · Page 1 of 1 (latest)

brazen raft
#
using System.Collections;

using System.Collections.Generic;

using UnityEngine;



public class DragObject : MonoBehaviour

{
    public Rigidbody rb;
    private float mZCoord;
    //checks if the sphere is on the ground
    public bool IsGrounded = true;

    void OnMouseDown()

    {
        mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
        // Store offset = gameobject world pos - mouse world pos
    }
    private Vector3 GetMouseAsWorldPoint()
    {
        // Pixel coordinates of mouse (x,y)
        Vector3 mousePoint = Input.mousePosition;
        // z coordinate of game object on screen
        mousePoint.z = mZCoord;
        // Convert it to world points
        return Camera.main.ScreenToWorldPoint(mousePoint);
    }

    void SetIsGroundedToFalse() 
    {
        IsGrounded = false;
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            IsGrounded = true;
        }
    }
    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            if (IsGrounded)
            {
                Invoke(nameof(SetIsGroundedToFalse), 0.5f);
            }
        }

    }

    private void OnCollisionStay(Collision collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            IsGrounded = true;
        }
    }
    void OnMouseDrag()
    {
        Vector3 drag = GetMouseAsWorldPoint() - transform.position;
        if (IsGrounded)
        {
            rb.AddForce(drag * 5);
        }
    }

}```
#

so the issue is this:

#

if the object is grouned, aka touches the "Ground" tagged platform

#

IsGrounded becomes true so the object can be dragged around

#

once the object leaves the ground, it can have velocity added to it through the drag for half a second

#

the problem surfaces once the object remains grounded

#

once it becomes grounded the function to make the bool IsGrounded falls executes

#

even if I remove the conditional from OnCollisionExit

#

the ball does bounce so in the end it will still execute after the very last and short bounce