Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Casting a ray from one point of a gameobject to another [2D]

Discussion in 'Scripting' started by CardboardKnight, Mar 15, 2021.

  1. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    I am currently trying to cast a ray from the top of a rope segment to it's base for the purpose of collision point detection.
    I figured out that the first point of the ray can be placed at the rigidbody's position, this gives me a point at the top of the rope.
    My problem comes when trying to figure out how to find the second point of the raycast, especially when considering that ingame the rope will move and rotate thanks to the physics system.
    Is there a way to determine a point to use as the second point of the ray i am trying to cast? Can i do so automatically through a script, or am i forced to use a set point for each segment?
     
  2. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    It seems i was able to get a ray cast through the gameobject by adding a child gameobject to the rope segment and positioning it at the tail end of the rope.
    Then through the script i simply get the transform of the child gameobject and cast a ray between the child gameobject's position and the parent's rigidbody's position.

    While this works i am left wondering if there isn't a way to do it through script without adding a child gameobject to the rope. Any hints would be appreciatted
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    This is a perfectly reasonable way to go. If you need the position of the end of the rope, you gotta get it somehow. If the end of the rope is an actual GameObject then you don't need the extra one. But I doubt the extra GameObject would EVER show up meaningfully in speed performance!!
     
  4. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    Thanks for the reply.
    To be honest i wasnt worrying so much about the performance than the clarity of the code and hierarchy.
    For curiosity's sake however, is it possible to do it without adding a child?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    I don't know the orientation of your rope segments, but if the pivot point (transform.position) of the segment is at the "base", then the ray should be able to go in whatever direction the rest of the body of the segment goes. So if that is "down" in local space, then it would just be
    -transform.up
    for that particular segment.
     
  6. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    Using -transform.right allowed me to remove the child object, the ray is perfectly cast. Big thanks to the both of you!