Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jun 25, 2016 at 09:22 AM by TeeKayM for the following reason:

Other

avatar image
1
Question by TeeKayM · Jun 19, 2016 at 12:41 PM · c#scripting problemquaternion

Weapon Sway Script messes up weapon rotation

So I'm in the process of learning Unity 5 and scripting in it. One thing I wanted to give a try was to make a simple weapon sway script like you see in games such as battlefield or csgo. After a bit of documentation I finally got a script going (see below).

 using UnityEngine;
 using System.Collections;
 
 public class WeaponSway : MonoBehaviour {
 
     float mouseX;
     float mouseY;
     float movementX;
     float movementY;
     float smooth = 3;
     public float speed;
 
     Vector3 defaultposition;
     Vector3 newPos;
 
     // Use this for initialization
     void Start () {
 
         defaultposition = transform.localPosition;
     }
     
     // Update is called once per frame
     void Update () {
 
         mouseX = Input.GetAxis("Mouse X");
         mouseY = Input.GetAxis("Mouse Y");
 
         movementX = mouseX * Time.deltaTime * speed;
         movementY = mouseY * Time.deltaTime * speed;
 
         Quaternion final = Quaternion.Euler(defaultposition.x * movementX, defaultposition.y * movementY, 0);
         transform.localRotation = Quaternion.Slerp(transform.localRotation,final,Time.deltaTime*smooth);
 
     }
 }

However, for some reason the simple model of the weapon I'm using changes rotation and the weapon doesn't sway like intended. I can't see the problem in the script.

Here is a gif of what is happening: http://imgur.com/zi6w6Om

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 chubshobe · Feb 21, 2017 at 03:58 AM 0
Share

Quaternion final = Quaternion.Euler(defaultposition.x * movementX, -90, 0); fixes the issue but the gun won't sway on the Y axis.

1 Reply

  • Sort: 
avatar image
0

Answer by JimNero009 · Jun 21, 2016 at 10:00 AM

I'm confused about the quaternion you call final here. Could you output the value of this to the console?

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

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Slerp Rotating 1 Degree and Limiting All Other Rotation 1 Answer

How do I smoothly rotate the GameObejcts with negative 90 degrees 0 Answers

How do I rotate the GameObejcts smoothly with negative degrees 0 Answers

why the effects of the Post Processing stack do not activate through an if-statement 0 Answers

Rotate Player 90 degrees about its Y axis relative to the mouse being dragged between two angles 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