Welcome!

Welcome to our community forums, full of great people, ideas and excitement. Please register if you would like to take part.

This is extra text with a test link..

Register Now

Announcement

Collapse
No announcement yet.

RayCasting to turn gravity on

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RayCasting to turn gravity on

    I'm trying to get it so when I press the Fire1 button it will turn ON gravity for the "thingIHit" if it has the tag of "Enemy"
    Raycasting works fine, but turning gravity on, on the rigidbody for the thingIHit does not.
    ERROR: 'Rigidbody' is a type, which is not valid in the given context.
    Code:
    public class RayCastingScript : MonoBehaviour
    {
       
        void Update()
        {
            RaycastHit thingIHit;
    
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out thingIHit))
            {
                if (Input.GetButtonDown("Fire1"))
                {
                    if (thingIHit.collider.gameObject.tag == "Enemy")
                    {
    
                        thingIHit.transform.GetComponent(Rigidbody).useGravity = true;
                    }
                }
                Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * thingIHit.distance, Color.yellow);
                Debug.Log("Did Hit");
            }
        }
    }

  • #2
    Nevermind, figured it out after playing with it some more.
    Code:
    if (thingIHit.collider.gameObject.tag == "Enemy")
                    {
                        thingIHit.rigidbody.useGravity = true;
                    }

    Comment


    • #3
      Cool solution. So your plan is to have them drop out of the sky when hit?

      Comment


      • #4
        Yes! I'm now trying to figure out how to change their velocity/direction when hit. At the moment they start falling because of gravity but still have their initial velocity and direction of when they are launched

        Comment

        Working...
        X