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 JBoy · Jun 29, 2012 at 10:45 AM · smoothweaponlocalpositionlocalrotationsway

Help me convert this script from using position to rotation? (BIG PROBLEM!)

Hi. I am working on a script that gives my FPS weapon a more realistic 'sway' effect while looking around.

In this script I have used the mouse input and localPosition to create the effect...

 var speed : float = 1;
 var maxAmount : float = 1;
 private var newPos : Vector3;
 private var localPos : Vector3; 
 private var movementX : float;
 private var movementY : float;


 function Start(){
 localPos = transform.localPosition; 
 }

 function Update (){ 
 movementX = Input.GetAxis("Mouse X") * Time.deltaTime * maxAmount;
 movementY = Input.GetAxis("Mouse Y") * Time.deltaTime * maxAmount;
 
 newPos.x = localPos.x - movementX;
 newPos.y = localPos.y - movementY;
 newPos.z = localPos.z;
 
 transform.localPosition = Vector3.Lerp(transform.localPosition,newPos,speed*Time.deltaTime);
 }

I have decided that changing the position of the object isn't the right way to create a realistic effect. I have tried to use localRotation and Quaternion like this...

 var speed : float = 1;
 var maxAmount : float = 1;
 private var newRot : Quaternion;
 private var localRot : Quaternion; 
 private var movementX : float;
 private var movementY : float;
 
 
 function Start(){
 localRot = transform.localRotation; 
 }
 
 function Update (){ 
 movementX = Input.GetAxis("Mouse X") * Time.deltaTime * maxAmount;
 movementY = Input.GetAxis("Mouse Y") * Time.deltaTime * maxAmount;
 
 newRot.x = localRot.x - movementX;
 newRot.y = localRot.y - movementY;
 newRot.z = localRot.z;
 
 transform.localRotation = Quaternion.Lerp(transform.localRotation,newRot,speed*Time.deltaTime);
 }

This script doesn't have any effect of the weapon, it just stays 'static', even if I crank the speed up to 999. So can someone please help me with this problem and show me the right method to go about this? - Please Help!

Comment
Add comment · Show 1
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 Bunny83 · Jun 30, 2012 at 01:15 AM 0
Share

As i said in my other comment, don't multiply delta values with Time.deltaTime. This will result in exactly the opposite effect.

2 Replies

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

Answer by aldonaletto · Jun 29, 2012 at 12:19 PM

This is a recurrent problem in Unity: the Editor fools us by showing the transform.localEulerAngles under the caption Rotation, making everybody think that the XYZ components of a quaternion are the rotation angles around the basic axes. They actually have a totally different meaning, and should never be directly modified.
I changed your script to use localEulerAngles instead:

var speed : float = 1; var maxAmount : float = 1; private var newRot : Vector3; private var localRot : Vector3; private var movementX : float; private var movementY : float;

function Start(){ localRot = transform.localEulerAngles; newRot = localRot; }

function Update (){ movementX = Input.GetAxis("Mouse X") Time.deltaTime maxAmount; movementY = Input.GetAxis("Mouse Y") Time.deltaTime maxAmount; // accumulate Mouse X and Y axes in newRot - notice that Mouse X is the // horizontal movement, what means rotation around Y, and Mouse Y actually // must affect the rotation around X: newRot.y = (newRot.y - movementX) % 360; // keep the angles in the 0..360 range newRot.x = (newRot.x - movementY) % 360; // localRot follows newRot with some delay: localRot = Vector3.Lerp(localRot, newRot, speed*Time.deltaTime); transform.localEulerAngles = localRot; // update actual object rotation } We must never directly Lerp transform.localEulerAngles to the desired rotation, because it may return weird XYZ combinations at some angles - make an initial copy of it, Lerp this copy and use it to update transform.localEulerAngles.

Comment
Add comment · Show 2 · 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 JBoy · Jun 29, 2012 at 11:36 PM 0
Share

Thanks - It REALLY helped me out!

avatar image Berenger · Jun 29, 2012 at 11:57 PM 2
Share

Why do people feel the need to add BIG PROBLE$$anonymous$$, or PLEASE HELP, URGENT etc ? Come on, we don't come here to play tennis, we're here to help, and urgent questions don't get answered faster.

avatar image
1

Answer by Datael · Jun 29, 2012 at 12:18 PM

According to the docs, Input.GetAxis() returns a value between -1 and 1, which means you're only adding fractions of degrees to your rotatation. Try multiplying it by 360 and see if that fixes it.

 movementX = 360.0 * Input.GetAxis("Mouse X") * Time.deltaTime * maxAmount;
 movementY = 360.0 * Input.GetAxis("Mouse Y") * Time.deltaTime * maxAmount;

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 Datael · Jun 29, 2012 at 12:20 PM 0
Share

...and you'll obviously want to adjust your "max amount" accordingly.

You could, in fact, set its value from the camera's field of view / 2. This way a maxAmount value of 1 would mean the weapon would only rotate within the field of view. Although now that I picture this it is probably undesirable behaviour.

avatar image Bunny83 · Jun 30, 2012 at 01:10 AM 1
Share

The mouse axis already returns delta values. Don't multiply them with Time.deltaTime. It will make the movement framerate dependent.

If you quore the sentence, you should read the whole sentence:

The value will be in the range -1...1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.

Delta values don't have a certain unit, so if you accumulate such values, you usually just scale them by an arbitrary factor which should be configurable by the user. This is just the sensitivity.

avatar image Datael · Jun 30, 2012 at 12:42 PM 0
Share

Indeed you are correct. That was irresponsible of me!

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

7 People are following this question.

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

Related Questions

Please help me convert this script from using position to rotation? 0 Answers

Locals How to get rid of them can I disable them??? 1 Answer

rotated position, together with the object (around the origin) 0 Answers

Z-axis relative to an object. 2 Answers

Help with Lerp 0 Answers


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