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 28, 2012 at 07:17 AM · weaponlocalpositionlocalrotationsway

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

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

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? - Thanks

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 Mizuho · Jun 28, 2012 at 07:31 AM 0
Share

Try using the euler angles ins$$anonymous$$d (`transform.localEulerAngles`).

avatar image hijinxbassist · Jun 28, 2012 at 07:35 AM 0
Share

Its best to 'read' the local euler, dont expect necessarily realistic results by manipulating it directly. If you can use Euler Angles (tho quaternions can be a headache in and of themselves!)

If you are unsure of the difference, i mean

 Quaternion.Euler(....

Granted localEuler will give you some kind of result, you will find out the hard way: when you reach a limit, it will no longer respond correctly.

avatar image JBoy · Jun 29, 2012 at 09:31 AM 0
Share

Could you please show me how to implement this into my script? - I am not a genius...

avatar image syclamoth · Jun 29, 2012 at 09:39 AM 1
Share

Try thinking outside the box. Presumably you created your weapon in some 3d-modelling tool? If it has animations for firing etc, why not make a 'movement sway' animation, too? This way, you can make it look good and realistic, and you won't have to worry about any complex quaternion arithmetic to achieve the desired effect.

Once you've done that, just play the animation at a speed dependent on the player's movement rate, and you're all set.

0 Replies

· Add your reply
  • Sort: 

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

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

Transfer axis global rotation on local axis of another object 0 Answers

Delay child objects rotation 0 Answers

Script won't set right rotation? 1 Answer

Locals How to get rid of them can I disable them??? 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