- Home /
Changing the position of an object hit with a RaycastHit
Hey guys so I'm getting sending out a raycast to check if I hit an object with the tag Weapon on it. I then am trying to set the position of the weapon to 0, 0, 0 although for some odd reason it is refusing to work. This is my current code:
hit.transform.tag = "Weapon1";
hit.transform.parent = GameObject.Find("WeaponParent").transform;
hit.transform.localPosition = new Vector3(0, 0, 0);
hasWeapon1 = true;
This code looks fine to me. Are you sure the raycast is happening? Have you debugged?
Yes. the debug didn't reveal anything useful and the raycast is happening because everything else in it is working fine. Just the position change is not
Hmm, can you post the rest of the code? It will likely give a clue to what is happening
Answer by ItsDJJuju · Jun 02, 2019 at 12:57 AM
Instead of getting the object hit and changing the position from that. I changed my code to find the GameObject's tag which I changed to Weapon1 and then changed the position to 0, 0, 0 from there. Changed code: hit.transform.tag = "Weapon1"; hit.transform.parent = GameObject.Find("WeaponParent").transform; GameObject.FindGameObjectWithTag("Weapon1").transform.localPosition = new Vector3(0, 0, 0);
But his is the exact same as your original code that you posted, except for changing hasWeapon1 to true
Yeah the code in the pastebin and the one I posted above should all be the same other than a few $$anonymous$$or changes. But ins$$anonymous$$d of using hit.transform.localPosition, i changed it to GameObject.FindGameObjectWithTag("Weapon1").transform.localPosition because for some odd reason it refuses to work with hit.transform.
That function is pretty expensive to do findgameobjectwithtag, though glad you got it working. Seems like there must be something strange going on.