Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Transform position attaches "game object1" to "game object2" characteristic points

Discussion in 'Scripting' started by BelNuts, Dec 17, 2022.

  1. BelNuts

    BelNuts

    Joined:
    Oct 7, 2022
    Posts:
    3
    Hi to all! First of all my apologizes if my description isn't perfect, I'm a sound designer and my coding skills are not great.

    The issue: I'm creating a sound system. And my need for it is that "game object1" (cube in this example) took accurate XYZ position where my first person camera raycast hits "game object2"'s collider (it could be any game object with collider: wall, road etc.).

    And it's almost ready but script moves and fixates "game object1" to "game object2"'s specific points (game objects2's corners, center) and not exactly to the raycast's hitpoint. And beyond that the "game object1" starts to flick almost all the time. This is how it looks like: https://imgur.com/a/AHLWgYm

    It's the first script attached to main camera:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FPCLookUpdated : MonoBehaviour
    6. {
    7.     Camera cam;
    8.     RaycastHit hit;
    9.     Ray ray;
    10.     public bool isCharacterLookingNPC;
    11.     public float targetHitX;
    12.     public float targetHitY;
    13.     public float targetHitZ;
    14.  
    15.     void Start()
    16.     {
    17.         isCharacterLookingNPC = false;
    18.         cam = GetComponent<Camera>();
    19.     }
    20.  
    21.  
    22.     public void Update()
    23.     {
    24.         ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    25.         //not very important part
    26.         //if (Physics.Raycast(ray, out hit, 10f))
    27.         //{
    28.         //    if (hit.collider.tag == "NPC")
    29.         //    {
    30.         //        isCharacterLookingNPC = true;
    31.         //        Debug.Log("isCharacterLookingNPC = true");
    32.  
    33.         //    }
    34.         //   else
    35.         //    {
    36.         //        isCharacterLookingNPC = false;
    37.         //        Debug.Log("isCharacterLookingNPC = false");
    38.         //    }
    39.  
    40.  
    41.         //}
    42.  
    43.         //and it's important part already
    44.         if (Physics.Raycast(ray, out hit, 100f))
    45.         {
    46.             if (hit.collider)
    47.             {
    48.                 targetHitX = hit.collider.transform.position.x;
    49.                 targetHitY = hit.collider.transform.position.y;
    50.                 targetHitZ = hit.collider.transform.position.z;
    51.  
    52.                 Debug.Log(hit.point);
    53.  
    54.  
    55.             }
    56.  
    57.  
    58.         }
    59.     }
    60. }
    61.  
    And it's the second one attached to "game object1" (cube):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletWallHit : MonoBehaviour
    6. {
    7.     FPCLookUpdated fpclookupdated;
    8.     public GameObject maincam;
    9.     //public GameObject cube;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         fpclookupdated = maincam.GetComponent<FPCLookUpdated>();
    21.         gameObject.transform.position = new Vector3(fpclookupdated.targetHitX, fpclookupdated.targetHitY, fpclookupdated.targetHitZ) ;
    22.        
    23.     }
    24. }
    25.  
    I think the problem is in the second script because coordinates debug from the first one runs smooth, there is no jumps + I've already made another systems with this raycast and it works great. Maybe there is a little command that I don't know and must add?

    Thanks in advance!
     
  2. BelNuts

    BelNuts

    Joined:
    Oct 7, 2022
    Posts:
    3
    Solved: my mistake, sorry, in the first script fixed the lines:

    Code (CSharp):
    1.     targetHitX = hit.collider.transform.position.x;
    Must be:

    Code (CSharp):
    1.     targetHitX = hit.point.x;
    and so on