#How can I modify the Rigidbody of another object?

13 messages · Page 1 of 1 (latest)

ebon axle
#

Attempting to create a gravity zone that launches the player when in contact.
Here is what I have:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GavityZone : MonoBehaviour
{
    public float gravityModifier = 10f;

    private void OnTriggerStay(Collider other)
    {
        var hitObj = other.gameObject;
        Debug.Log("contact");
        
        var modifiedRB = hitObj.GetComponent<Rigidbody>();
        Debug.Log("Rigidbody retrieved ");

        if (modifiedRB != null)
        {

            Debug.Log("force applied");
            Vector3 forceDirection = transform.up;
            modifiedRB.AddForce(forceDirection * gravityModifier, ForceMode.Impulse); 
        }
    }
}
fierce mulch
#

Looks like it's working

ebon axle
#

the force isn't being applied thou

#

contact is happening, but the force isn't being applied **

fierce mulch
queen gulch
#

Does your playerObj have a rigidbody ?

ebon axle
#

playerObj doesnt but its parent does

#

trying to launch the player (capsule) similar to the sphere

queen gulch
#

can u try var modifiedRB = hitObj.GetComponentInParent<Rigidbody>(); instead for reaching the rigidbody

ebon axle
#

got it working !

#

yes @queen gulch it was the parent component I needed to retrieve :))

#

thank you!