Changing first person item's position and rotation at runtime?

I know you had another long thread before about similar stuff but you'll have to excuse my bad memory with how all of that went before... I'm still not entirely certain why you aren't able to use a state preset on your item's FirstPersonPerspectiveItem that adjusts its position offset, e.g. during the "Aim" state. Maybe you already explained this in the last thread, so forgive me if that's the case! But generally speaking you should be able to just use a state preset that forces the item's position offset to whatever value you want. And then if there are cases where states conflict with each other and whatnot you can use state blocking to force a particular state preset to be active/inactive at a certain time, etc.

Also, as a side point, GameObject.Find is not a reliable way to get a reference to an object when there are likely other objects with the same name in the scene (which I think I even saw in your hierarchy that there are). So I'd just use an inspector reference like you're already doing.
 
The position offset is where I want it to be. However, the transform values (namely position and rotation) of the item itself would need to dynamically change at runtime in order to look correct. Is this possible?
 
Ahh sorry, I see what you mean now. I don't think there's any reason you can't just directly modify the transform of a perspective item through code - I just tested it in the demo scene. I'd remove GameObject.Find which is probably referencing the wrong object, and just use the inspector reference. Or if you need to get the reference during run-time, then use a tag or component unique to that gameobject, or even just give it a unique name that you can rely on being the same.
 
Oh, that makes sense. I'll keep the inspector reference and ditch GameObject.Find. Here's the updated code:
Desktop Screenshot 2020.12.05 - 18.18.41.19.png
Looks ok, let's test it:
Desktop Screenshot 2020.12.05 - 18.14.52.16.png
Annnnd...not what I wanted ?
Perhaps I need an else statement placing the transform values back to where they were before?
 
Another note is that Transform.Rotate will "add" rotation to a transform, so if it already has another rotation set then the end result will not be the values you've put in there. It'd be better to use transform.rotation = Quaternion.Euler(x, y, z).

And yes, you'd definitely want to set those values back to what they were before in an else statement
 
A couple of things to look at:

- Setting Transform.position sets the position of the object in world space, not local space. However, the position values you see in the inspector for a transform are actually its local position (i.e. the offset from its parent). So you may want to use Transform.localPosition instead.
- Transform.rotation works the same way - I believe you can use Transform.localRotation
 
Top