Home > Software engineering >  How to throw object to the direction the player is facing?
How to throw object to the direction the player is facing?

Time:06-14

I'm trying to throw an object after it is being held. Here is how the pickup works:

 public void PickupObject()
        {

        physicsObject = lookObject.GetComponentInChildren<PhysicsObjects>();
        currentlyPickedUpObject = lookObject;
        pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
        priorConstraints = pickupRB.constraints;   
        pickupRB.constraints = RigidbodyConstraints.FreezeAll;   
        pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
         physicsObject.playerInteractions = this;
        pickupRB.isKinematic = true;
        pickupRB.transform.parent = PickupParent.transform;
        //   pickupRB.isKinematic = true;
        StartCoroutine(physicsObject.PickUp()); 

    }

in the update():

 if (currentlyPickedUpObject != null)
        {
            currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
            currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
            currentSpeed *= Time.fixedDeltaTime;
            pickupRB.transform.position = PickupParent.position;
            // pickupRB.transform.SetParent(PickupParent.transform);
            Vector3 direction = PickupParent.position - pickupRB.position;
            pickupRB.velocity = direction.normalized * currentSpeed;
            Vector3 camerDirection = mainCamera.transform.forward;
            // Throw object
            if (Throw)
            {
                pickupRB.constraints = RigidbodyConstraints.None;
                pickupRB.isKinematic = false;
                Debug.Log("Object is being thrown"); 
                pickupRB.AddForce(camerDirection * 100);
            }
            Throw = false;
        }

as shown above I was trying to add force to the direction the player to be able to throw the item in that direction in this part:

Vector3 camerDirection = mainCamera.transform.forward;
if (Throw)
            {
            pickupRB.constraints = RigidbodyConstraints.None;
            pickupRB.isKinematic = false;
                Debug.Log("Object is being thrown"); 
                pickupRB.AddForce(camerDirection * 100);
            }
            Throw = false;

The line is printed in the console when the throw button is pressed but nothing happens. How to fix this?

CodePudding user response:

I think you're throwing the object but you're not releasing it, so on the next frame the currentlyPickedUpObject is not null so you go right back to moving it with the PickupParent again. Try setting it to null inside your if (Throw) statement so you don't keep moving it:

if (currentlyPickedUpObject != null)
    {
        currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
        currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
        currentSpeed *= Time.fixedDeltaTime;
        pickupRB.transform.position = PickupParent.position;
        // pickupRB.transform.SetParent(PickupParent.transform);
        Vector3 direction = PickupParent.position - pickupRB.position;
        pickupRB.velocity = direction.normalized * currentSpeed;
        Vector3 camerDirection = mainCamera.transform.forward;
        // Throw object
        if (Throw)
        {
            pickupRB.constraints = RigidbodyConstraints.None;
            pickupRB.isKinematic = false;
            Debug.Log("Object is being thrown"); 
            pickupRB.AddForce(camerDirection * 100);
            currentlyPickedUpObject = null; // <--- NEW
        }
        Throw = false;
    }

CodePudding user response:

The problem is when you pick the object up you are setting the pickupRB kinematic with line pickupRB.isKinematic = true So to fix this, you should add pickupRB.isKinematic = false before you add force to the rigidbody.

  • Related