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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Why the child object is all the time moving through other objects while the parent is not ?

Discussion in 'Scripting' started by Chocolade, Mar 6, 2018.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    I have a first person player. Named it Player. It's a capsule.
    Attached to the Player: Character Controller , First Person Controller , Rigidbody , Audio Source , And one of my scripts:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class DetectInteractable : MonoBehaviour
    8. {
    9.     public Camera cam;
    10.     public float distanceToSee;
    11.     public string objectHit;
    12.     public bool interactableObject = false;
    13.     public Transform parentToSearch;
    14.     public static bool detected = false;
    15.  
    16.     private RaycastHit whatObjectHit;
    17.     private bool clickForDescription = false;
    18.     private int layerMask = 1 << 8;
    19.  
    20.     private void Start()
    21.     {
    22.  
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         if (Input.GetMouseButtonDown(0))
    28.         {
    29.             clickForDescription = true;
    30.         }
    31.  
    32.         Debug.DrawRay(cam.transform.position, cam.transform.forward * distanceToSee, Color.magenta);
    33.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee, layerMask))
    34.         {
    35.             detected = true;
    36.             objectHit = whatObjectHit.collider.gameObject.name;
    37.             interactableObject = true;
    38.             print("Hit ! " + whatObjectHit.collider.gameObject.name);
    39.         }
    40.         else
    41.         {
    42.             detected = false;
    43.             clickForDescription = false;
    44.             print("Not Hit !");
    45.         }
    46.     }
    47.  
    48.     private void OnGUI()
    49.     {
    50.         if (clickForDescription == true)
    51.         {
    52.             ProcessOnGUI(parentToSearch);
    53.         }
    54.     }
    55.  
    56.     void ProcessOnGUI(Transform parent, int level = 0)
    57.     {
    58.         foreach (Transform child in parent)
    59.         {
    60.             if (child.GetComponent<ItemInformation>() != null)
    61.             {
    62.                 ItemInformation iteminformation = child.GetComponent<ItemInformation>();
    63.                 if (child.name == objectHit)
    64.                 {
    65.                     var centeredStyle = GUI.skin.GetStyle("Label");
    66.                     centeredStyle.alignment = TextAnchor.UpperCenter;
    67.                     GUI.Box(new Rect(
    68.                           Screen.width / 2 - 50 + 20 * level, // <== INDENTATION
    69.                           Screen.height / 2 - 25, 100, 50),
    70.                         iteminformation.description, centeredStyle);
    71.                 }
    72.             }
    73.  
    74.             // Process next deeper level
    75.             ProcessOnGUI(child, level + 1);
    76.         }
    77.     }
    78.  
    79.     public class ViewableObject : MonoBehaviour
    80.     {
    81.         public string displayText;
    82.         public bool isInteractable;
    83.     }
    84. }
    85.  
    Under Player as child there is a Player Camera. It's just a Camera tagged as MainCamera.

    Under the Player Camera as child of the Player Camera I have a Cube.
    The cube is moving with the Player but when I'm getting close to a door or to the floor or any other objects the Player will stop but the Cube will move through inside the other object/s.

    If for example it's a door part of the cube will be in the other side of the door.

    I tried to add to the Cube a box collider and a rigidbody and played with all settings of the rigidbody with or without gravity and is kinematic and also tried all the box collider settings is trigger on/off. Change the box collider size to make it big to make sure it cover it all. Tried other colliders capsule or mesh on the cube nothing helped.

    Tried to play with the Player Rigidbody settings. So far nothing helped. The Player stop when trying move through a door or any object but the cube move through.

    The cube is position a bit in front of the Player so I can see the cube like dragging the cube.

    All the other objects like the door have a box collider ( The door and other objects not my own ).

    The first 4 screenshots are of the Player , Player Camera , Cube and example when I'm near the door the cube is out in the other room while the player is behind the door in the first room.

    The last screenshot show the door inspector.









     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Character controller's don't make use of rigidbody physics, so your player's rigidbody isn't really doing anything. Moving a child rigidbody object with a parent character controller will give you physically incorrect results at best (since the physics object is effectively teleporting every frame) or no simulation at worst.

    You'll most likely need to rethink your setup. What's the purpose of the cube and why does it need to be physically simulated?
     
    Chocolade likes this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    The purpose of the cube is to make it act like a mouse cursor in the game. For example when the cube hit a door do something or if the cube hit a window do something.
    I already did it with another object. I'm using a script to make the object bigger or smaller and rotating and using it for actions like look or to do something in the game. This is my object:

    This is a screenshot of my object. Like the cube the object is a child of the Player Camera. The cube was just for testing.
    On the left it's the scene view window before running the game. On the right after running the game.
    What I want is that the object will be like part of the Player. If the player hit a door and can't move through same the child object. If the Player hit any object and can't move through or maybe can same the child object.

    The other part about interactable action and other stuff I handle it.
    The problem is how to make the child object to act like the Player in cases hitting objects ?

     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    If it's solely being used to determine interactables, I would strongly suggest switching to a raycast solution.

    If it's being used as a guide, like Navi from Zelda, I would make it a separate object that follows the player rather than being literally parented.

    If it's a part of the player and doesn't move, you you can either increase the size of your character controller's capsule to encompass the child object or switch to a rigidbody based controller, but I would be concerned about frustrating the player by having a giant floating object blocking their movement in hard to perceive ways.
     
    Chocolade likes this.
  5. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916

    How can I make the object to move with the Player and the Player Camera ?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ObjectFollowCamera : MonoBehaviour
    7. {
    8.     public Transform cameraToFollow;
    9.     public Transform objectToFollow;
    10.     public float distanceFromCamera;
    11.  
    12.     private void Update()
    13.     {
    14.         objectToFollow.position = new Vector3(cameraToFollow.position.x,cameraToFollow.position.y,cameraToFollow.position.z - distanceFromCamera);
    15.     }
    16. }
    17.  
    Either if I put in the Inspector the Player or Player Camera for cameraToFollow the ocjectToFollow will only move if I move the Player with the keys WSAD but if I move the Player Camera around the object is not moving following the camera.