- Home /
How to make a flashlight (childed to the player) rotate with the player?
So I'm making a hospital horror game and was trying to figure out how to make the flashlight rotate up and down as the player rotates up and down (same x rotation, basically), but the script says that when I try to set the flashlight's rotational x value (this is in fixed update by the way) equal to the player's rotational x value, the error given is this:
"Assets/Scripts/FlashlightLook_Y.cs(12,22): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable"
I don't know how this works or how to correct the error, so someone please give me some help and tell me how to do this. Thanks in advance! Also P.S. maybe we could work together?? IDK it's just an idea but thanks a ton!!
Hello, As per your error you can not directly assign any value to transform. for example: you can not do
transform.rotation.x=12
you have to take one variable and then assign that variable to transform.rotation for example:
Quaternion tempRotation = transform.rotation;
tempRotation.x=12;
trasform.rotation = tempRotation;
Hope you are looking for this. :)
Quaternion x=transform.rotation;
//you cannot do like this x.y=90f;
//Ins$$anonymous$$d x=new Quaternion(x.x,90f,x.z,x.w);
@$$anonymous$$ehul-Rughani you should've put that as a reply not a comment and also thanks for the suggestion but I am working with dev-waqas' reply to see if his is any good.
I already found the problem. I childed the flashlight to the camera. Thanks though.
Answer by dev-waqas · Jan 19, 2018 at 05:09 AM
You need to store value of rotaion in some variable first.
var rot= transform.rotaion;
Then you can change value of
rot.x = player.x;
Don't forget to reassign value to the rotation.
transform.rotation = rot;
@dev-waqas Where do I put those sentences of code? Like, in Update, at the start (behind function start), in Start (function start)? Sorry, it's not clear the way you put it. $$anonymous$$aybe you could paste the whole script for me? Thanks for the suggestion though. If you can specify what you mean, I'll accept your reply. :D
@dev-waqas @$$anonymous$$ittenSnipes I'm sorry guys but I realised that I had to do put the flashlight under the camera and now it works. Thanks for your suggestions though!