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 /
avatar image
0
Question by GamerDude27 · Sep 09, 2018 at 07:15 PM · gun scriptweapon systeminertiasway

Problems With Weapon Sway

Hey, I've got this code for my current weapon sway system:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponSway : MonoBehaviour
 {
     // Variables
     Vector3 vLastFacing;
     float flMaxViewModelLag = 1.5f;
 
     //-----------------------------------------------------------------------------
     // Purpose: Use this for initialization
     //-----------------------------------------------------------------------------
     void Start ()
     {
 
     }
 
     //-----------------------------------------------------------------------------
     // Update is called once per frame
     // Purpose: Calculates the sway the weapon has when looking around
     //-----------------------------------------------------------------------------
     Vector3 origin;
     Quaternion angles;
     Quaternion original_angles;
 
     void Update ()
     {
         Vector3 vOriginalOrigin = origin;
         Quaternion vOriginalAngles = angles;
 
         // Calculate our drift
         Vector3 forward = new Vector3();
         VectorExtensionMethods.AngleVectors(angles, forward, Vector3.zero, Vector3.zero);
 
         if (Time.deltaTime != 0.0f)
         {
             Vector3 vDifference = new Vector3();
             VectorExtensionMethods.VectorSubtract(forward, vLastFacing, vDifference);
 
             float flSpeed = 5.0f;
 
             // If we start to lag too far behind, we'll increase the "catch up" speed.
             // Solves the problem with fast cl_yawspeed, m_yaw or joysticks rotating quickly.
             // The old code would slam m_vecLastFacing with origin causing the viewmodel to pop to a new position
             float flDiff = vDifference.magnitude;
             if ((flDiff > flMaxViewModelLag) && (flMaxViewModelLag > 0.0f))
             {
                 float flScale = flDiff / flMaxViewModelLag;
                 flSpeed *= flScale;
             }
 
             // FIXME:  Needs to be predictable?
             VectorExtensionMethods.VectorMA(vLastFacing, flSpeed * Time.deltaTime, vDifference, vLastFacing);
 
             // Make sure it doesn't grow out of control!!!
             Vector3.Normalize(vLastFacing);
             VectorExtensionMethods.VectorMA(origin, 5.0f, vDifference * -1.0f, origin);
 
             Debug.Assert(vLastFacing.IsValid());
         }
 
         Vector3 right = new Vector3();
         Vector3 up = new Vector3();
         VectorExtensionMethods.AngleVectors(original_angles, forward, right, up);
 
         // Mouse X/Y
         float factorX = -Input.GetAxis("Mouse X");
         float factorY = -Input.GetAxis("Mouse Y");
         if (factorX > 180.0f)
         {
             factorX -= 360.0f;
         }
         else if (factorY < -180.0f)
         {
             factorY += 360.0f;
         }
 
         // If viewmodel lag equals 0 return to original origin/angles
         if (flMaxViewModelLag == 0.0f)
         {
             origin = vOriginalOrigin;
             angles = vOriginalAngles;
         }
 
         //FIXME: These are the old settings that caused too many exposed polys on some models
         // X
         VectorExtensionMethods.VectorMA(origin, -factorX * 0.035f, forward, origin);
         VectorExtensionMethods.VectorMA(origin, -factorX * 0.03f, right, origin);
         VectorExtensionMethods.VectorMA(origin, -factorX * 0.02f, up, origin);
 
         // Y
         VectorExtensionMethods.VectorMA(origin, -factorY * 0.035f, forward, origin);
         VectorExtensionMethods.VectorMA(origin, -factorY * 0.03f, right, origin);
         VectorExtensionMethods.VectorMA(origin, -factorY * 0.02f, up, origin);
     }
 }

Now what I'm unsure about is how to make it actually work in-game. My previous system had this to get it working:

 Vector final = new Vector (def.x + factorX, def.y + factorY, def.z);
 transform.localPosition = Vector.Lerp (transform.localPosition, final, Time.deltaTime = _smooth);

But I don't know how to do this for my current system.

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

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

88 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

Related Questions

Gun Rotation Sway 0 Answers

Enabling a gun to shoot if its in your hands 1 Answer

Rigidbody inertia recalculation after detaching child colliders ? 2 Answers

Error: Actor::updateMassFromShape 0 Answers

Help me convert this script from using position to rotation? (BIG PROBLEM!) 2 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