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. Dismiss Notice

Collisions with picked up item

Discussion in 'Scripting' started by Jeremy00, Jun 27, 2014.

  1. Jeremy00

    Jeremy00

    Joined:
    Jun 27, 2014
    Posts:
    2
    I have a question about collisions before I start writing a script. I have a script that will set an object in front of the camera(first-person). The script is attached to the first person camera(c#).


    fuction Update() {
    MyObject.position = transform.position + transform.forward;
    }



    The issue is I don't want the object to go through walls when I move the First Person Camera. Since the script on the camera literally putting the object in front wherever I move it will ignore collissions. I set up the mesh/box/custom collisions on the item and they are ignored when I move through a wall because the script is more important.

    So what I was thinking about doing was making a raycast off of MyObject and detecting distance from the MyObject. When the distance is very close to 0, that means it is hitting a wall (my environment), I would stop moving the controller (my camera/controller).

    Is this a good idea, or is there a better way to force collisions on the object while the camera script is moving it or should I try raycasting distance between MyObject and the walls.

    EDIT: I am currently studying collisions and triggers.. wondering if they could be used?
    So... when the raycast hits the collider, it would cause a collision event and on that event I would stop the movement on that event.

    Edit #2: I was also thinking about making the MyObject a child of the camera/controller using instantiate. But I can't find a solid example.
     
    Last edited: Jun 27, 2014
  2. dwd31415

    dwd31415

    Joined:
    Nov 14, 2013
    Posts:
    25
    If you want to instantiate a GameObject as a child of an other GameObject you have to use this code:
    Code (CSharp):
    1. GameObject myObject=Instaniate(gameObject) as GameObject;
    2. myObject.transforn.parent=transform;
     
  3. Jeremy00

    Jeremy00

    Joined:
    Jun 27, 2014
    Posts:
    2
    Got it. Thank you. So does the child take the collision properties of the parent?
    If I was to make a blank GameObject a child.. and the parent had collision, would the child retain the collison?
     
  4. dwd31415

    dwd31415

    Joined:
    Nov 14, 2013
    Posts:
    25
    The child of a GameObject will be transformed in the local space of its parent. That is done over matrix operations and only affects the position,rotation and scale, therefore it won't have any impact on the collsion system.