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 TheRichardGamer · Sep 13, 2014 at 11:53 PM · c#transformfunctioneffectvector

How to make SwayScript and Pistol Script work together

So whenever I try to play with my sway script and my pistol script, they bug each other out by making the pistol go back and forth a little bit because the pistol script handles the pistols position, and so does the sway script. And I wanna get rid of this problem, making it so that it won't go back and forth in the same place by somehow intergrating the scripts so that they work properly.

Sway Script:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponSway : MonoBehaviour {
     
     public float mouseSensitivity = 10f;
     public float maxMoveAmount = 0.5f;
     public float minMoveAmount;
     public float smoothSpeed = 3f;
     
     private static Vector3 startPos;
     
     private float factorX;
     private float factorY;
 
     public bool moving;
     
     public bool bRotate;
     public float smoothRotationSpeed = 2f;
     public float rotateAmount = 45f;
     
     //============================================
     // FUNCTIONS
     //============================================
     
     void Start()
     {
         startPos = transform.localPosition;
     }
     
     void Update()
     {
 
 
         // STORE MOUSE MOVEMENT AMOUNTS, SMOOTH WITH LERP
         factorX = Mathf.Lerp(factorX, -Input.GetAxis("Mouse X") * mouseSensitivity, Time.deltaTime * 10f);
         factorY = Mathf.Lerp(factorY, -Input.GetAxis("Mouse Y") * mouseSensitivity, Time.deltaTime * 10f);
 
         if(factorX <= minMoveAmount)
         {
 
             moving = false;
 
         }
 
         if(factorY <= minMoveAmount)
         {
             
             moving = false;
             
         }
 
         if(factorX == maxMoveAmount || factorX > minMoveAmount)
         {
             
             moving = true;
             
         }
         
         if(factorY == maxMoveAmount || factorY > minMoveAmount)
         {
             
             moving = true;
             
         }
 
         // CLAMP LIMITS
         if (factorX > maxMoveAmount)
             factorX = maxMoveAmount;
         
         if (factorX < -maxMoveAmount)
             factorX = -maxMoveAmount;
         
         if (factorY > maxMoveAmount)
             factorY = maxMoveAmount;
         
         if (factorY < -maxMoveAmount)
             factorY = -maxMoveAmount;
         
         // SET TARGET POSITION (START POSITION + MOUSE MOVEMENT)
         Vector3 targetPos = new Vector3(startPos.x + factorX, startPos.y + factorY, startPos.z);
         
         // APPLY POSITION TO WEAPON, SMOOTH WITH LERP
         transform.localPosition = Vector3.Lerp(transform.localPosition, targetPos, smoothSpeed * Time.deltaTime);
         
         // ROTATION
         if (bRotate)
         {
             float tiltAroundZ = Input.GetAxis("Mouse X") * rotateAmount;
             float tiltAroundX = Input.GetAxis("Mouse Y") * rotateAmount;
             Vector3 target = new Vector3(tiltAroundX, 0f, tiltAroundZ);
             transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.Euler(target), Time.deltaTime * smoothRotationSpeed);
         }
     }
     
 }



Pistol Script:

 using UnityEngine;
 using System.Collections;
 
 public class Pistol : MonoBehaviour {
 
     public string name;
     public GameObject obj;
     public float damage;
     public float accuracy;
     public Vector3 defaultPos;
     public Vector3 aimPos;
     private Vector3 curPos;
     private Vector3 nextPos;
     public float aimSpeed;
     public bool hideMouse;
 
     public bool canMove;
 
     public WeaponSway weaponSway;
 
     void Awake ()
     {
 
         nextPos = defaultPos;
 
         if(!weaponSway)
         {
 
             Debug.LogError("Assign the weapon sway script!");
 
         }
 
     }
 
     void Update ()
     {
 
         if(hideMouse)
         {
 
             Screen.lockCursor = true;
 
         }
 
         if(!hideMouse)
         {
             
             Screen.lockCursor = false;
             
         }
 
 
             curPos = obj.transform.localPosition;
             obj.transform.localPosition = Vector3.Lerp(curPos, nextPos, aimSpeed);
 
         if(Input.GetMouseButton(1))
         {
 
             nextPos = aimPos;
 
         }
 
 
         if(Input.GetMouseButtonUp(1))
         {
 
             nextPos = defaultPos;
 
             
         }
 
 
         if(curPos != aimPos && curPos != defaultPos)
         {
 
             weaponSway.enabled = false;
             canMove = true;
 
         }
 
         if(curPos == aimPos || curPos == defaultPos)
         {
             
             weaponSway.enabled = true;
             canMove = false;
             
         }
 
 }
     
 
 
 }
 





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

23 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to make cubes between 2 point and stick them together? 0 Answers

How to pass a Transform Array as an argument into a function in C#? 1 Answer

just update X and Z position of gameobject 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