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: Iss - Album on Imgur
It’s the first script attached to main camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPCLookUpdated : MonoBehaviour
{
Camera cam;
RaycastHit hit;
Ray ray;
public bool isCharacterLookingNPC;
public float targetHitX;
public float targetHitY;
public float targetHitZ;
void Start()
{
isCharacterLookingNPC = false;
cam = GetComponent<Camera>();
}
public void Update()
{
ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
//not very important part
//if (Physics.Raycast(ray, out hit, 10f))
//{
// if (hit.collider.tag == "NPC")
// {
// isCharacterLookingNPC = true;
// Debug.Log("isCharacterLookingNPC = true");
// }
// else
// {
// isCharacterLookingNPC = false;
// Debug.Log("isCharacterLookingNPC = false");
// }
//}
//and it's important part already
if (Physics.Raycast(ray, out hit, 100f))
{
if (hit.collider)
{
targetHitX = hit.collider.transform.position.x;
targetHitY = hit.collider.transform.position.y;
targetHitZ = hit.collider.transform.position.z;
Debug.Log(hit.point);
}
}
}
}
And it’s the second one attached to “game object1” (cube):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletWallHit : MonoBehaviour
{
FPCLookUpdated fpclookupdated;
public GameObject maincam;
//public GameObject cube;
void Start()
{
}
// Update is called once per frame
void Update()
{
fpclookupdated = maincam.GetComponent<FPCLookUpdated>();
gameObject.transform.position = new Vector3(fpclookupdated.targetHitX, fpclookupdated.targetHitY, fpclookupdated.targetHitZ) ;
}
}
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!