Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 $$anonymous$$ · Mar 09, 2014 at 09:07 PM · rotationclampfirst-person

Is anyone able to fix my code?

Alright, I've got this code here:

 var speed : float = 5;
 var torsoRotation : float = 0;
 
 function Update(){
   torsoRotation = transform.Rotate(0, Input.GetAxis("Mouse X") * speed, 0, Space.World);
   Mathf.Clamp(torsoRotation, -30, 30);
 }

This is the error I get in Unity:

 Assets/Scripts/mouseRotate.js(5,35): BCE0022: Cannot convert 'void' to 'float'.

I have a full player model. I was trying to get the torso to rotate slightly left and right, as I moved the mouse those directions in first-person. (Just a little bit of realism.) It worked! I added a variable to store the rotation in, and then I clamped it, so it would not rotate infinitely. Then, it did not work.

Of course, my logic is probably completely wrong. Is anyone able to explain what I did wrong? Am I unable to store the rotation?

Comment
Add comment
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

3 Replies

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

Answer by xtvs · Mar 09, 2014 at 09:40 PM

transform.Rotate does not return a value, so assigning it to a float doesn't make any sense. Calculate the value you want to rotate by, clamp it, and then rotate the object.

Try this:

 var speed : float = 5;
 var torsoRotation : float = 0;
 function Update() {
   torsoRotation = Input.GetAxis("Mouse X") * speed;
   Mathf.Clamp(torsoRotation, -30, 30);
   transform.Rotate(0, torsoRotation, 0, Space.World);
 }
Comment
Add comment · 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
1

Answer by noooodley · Mar 09, 2014 at 09:15 PM

You are assuming that the method 'Rotate' returns a float when it actually returns nothing (void). Maybe something like:

 function Update() {
      transform.Rotate(0, Input.GetAxis("Mouse X") * speed, 0, Space.World);
      torsoRotation = transform.eulerAngles.y;
 
      //the rest of your code...
 }
Comment
Add comment · 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
0

Answer by SirCrazyNugget · Mar 09, 2014 at 09:21 PM

transform.Rotate doesn't return a value, it merely calls a function which Rotates around the given axis.

Try something like

 function Update(){
    torsoRotation = Mathf.Clamp(torsoRotation + Input.GetAxis("Mouse X") * speed * Time.deltaTime, -30, 30);
    transform.rotation = Quaternion.Euler(0, torsoRotation, 0);
 }

This should, if I've wrote it right, limit the transform Y rotation between -30 and 30 which I think is what you're after.

Comment
Add comment · Show 1 · 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 Kiloblargh · Mar 09, 2014 at 10:17 PM 0
Share

3 people just gave you the right answer, and now you're going off on a tangent- did you understand the explanation? If so, mark an answer correct.

To do 2 axis rotation, parent the object to two empty transforms and use one only for y pivot and one only for x tilt. It will make things easier than trying to rotate the same transform on two axes at once.

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

What is wrong with this script? 2 Answers

Sprite facing object to mouse 0 Answers

How do I clamp my rotation? 4 Answers

An object reference is required for the non-static field, method, or property 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges