I have Vector3 variable. I need it to change position with my player object.
(at fact this variable is RaycastHit with changed hit.point.y)
I dont need this variable to be in player transform position.
i want it to save position relatively to my player.
i have big ship with big cannons, this cannons can rotate to mouse click (with rotateToward). But when my ship moves - raycastHit variable stands in same position as be4, i want it to move with my ship to save cannon rotation.
sry for my language
You can do this in update if you want an update of the current player position each frame or you can do it whenever you want if you want an update just when you want.
A Vector3 is saved in a variable ‘by value.’ That is, when you assign something to a Vector3, a copy is made. There are a couple of ways you can get what you want. lets assume your code is something like this:
var v3Saved = hit.point;
v3Saved.y += 0.25;
So we have a hit with a modified ‘y’ value. This is a copy of the position. It will not change. But we can track the game object it hit:
var transTarget = hit.transform;
And we can get the offset between the hit point and the current position of the target:
var v3Offset = v3Saved - trasnTarget.position;
So at any future time you want that position you can:
var currentTargetLocation = transTarget.position + v3Offset;
I can think of an alternate solution that is a bit hacky, but in the right situation might work well. You can place an empty game object at the point of the hit and make that empty game object a child of the target. Assuming you had an empty game object you could do: