• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DarkFang100 · Jan 19, 2018 at 03:47 AM · rotationplayerflashlighthorror

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!!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mehul-Rughani · Jan 19, 2018 at 04:09 AM 0
Share

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. :)

avatar image raoul_95 · Jan 19, 2018 at 04:43 AM 0
Share

Quaternion x=transform.rotation;

//you cannot do like this x.y=90f;

//Instead x=new Quaternion(x.x,90f,x.z,x.w);

avatar image DarkFang100 · Jan 23, 2018 at 02:56 AM 0
Share

@Mehul-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.

avatar image DarkFang100 · Feb 01, 2018 at 04:02 AM 0
Share

I already found the problem. I childed the flashlight to the camera. Thanks though.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DarkFang100 · Jan 23, 2018 at 02:53 AM 0
Share

@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. Maybe 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

avatar image KittenSnipes · Feb 01, 2018 at 02:10 AM 0
Share

Try putting it in the Update method

avatar image DarkFang100 · Feb 01, 2018 at 04:02 AM 0
Share

@dev-waqas @KittenSnipes 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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make enemy teleport right next to player? 4 Answers

Player walks in the direction the vr camera is looking? 2 Answers

Saving System? 1 Answer

Player rotates with camera(face same direction as the camera) 1 Answer

Player Move-Rotation 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges