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 scipiothegreat · Mar 10, 2012 at 10:07 AM · fpsquaterniongunsway

Gun sway script not working

This script is parented to the arm gameobject to make so that when you move your mouse the arms move a little ahead of the camera and then settle back to be aligned with the camera. The problem is that when I move my mouse the arms flip randomly between what I want and being randomly flipped 180 degrees around the Y-axis. This is a modified MouseLook script from standard assets.

 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Mouse Sway")]
 public class MouseSway : MonoBehaviour {
 
     public float sensitivityX = 1F;
     public float sensitivityY = 1F;
     public float offsetY = 0;
 
     public float minimumX = -30F;
     public float maximumX = 30F;
 
     public float minimumY = -30F;
     public float maximumY = 30F;
 
     float rotationX = 0F;
     float rotationY = 0F;
     
     float tSinceLastMoveX = 0F;
     float tSinceLastMoveY = 0F;
     
     Quaternion originalRotation;
 
     void FixedUpdate ()
     {
         
             // Read the mouse input axis
             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
         
             rotationX = ClampAngle (rotationX, minimumX, maximumX);
             rotationY = ClampAngle (rotationY, minimumY, maximumY);
             
             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
         
             transform.localRotation = originalRotation * xQuaternion * yQuaternion;
         offsetY = 0;
     }
     
     void Start ()
     {
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
         originalRotation = transform.localRotation;
     }
     
     public static float ClampAngle (float angle, float min, float max)
     {
         if (angle < -360F)
             angle += 360F;
         if (angle > 360F)
             angle -= 360F;
         return Mathf.Clamp (angle, min, max);
     }
 }
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

2 Replies

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

Answer by scipiothegreat · Mar 18, 2012 at 07:37 PM

Problem fixed. The script posted above works as intended, it just glitched out because of another script.

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
avatar image
0

Answer by syclamoth · Mar 12, 2012 at 12:35 AM

So, let me get this straight- you want the arms (and gun) to follow the mouse-look directly, but you want the camera to lag behind a little?

How about this. Instead of modifying the mouselook script directly, put the mouselook script on the arms, and leave the camera completely seperate. Then, add a new script to the camera, that follows the rotation of the arms!

 void LateUpdate()
 {
     transform.rotation = Quaternion.Slerp(transform.rotation, arms.rotation, Time.deltaTime * smoothingFactor);
     transform.position = arms.position;
 }

This way, the camera doesn't need to exactly know when the arms are moving, but it will still act the way you want it to.

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 scipiothegreat · Mar 12, 2012 at 12:56 AM 0
Share

the script that is posted is attached to the arms and the camera and character still have the default mouselook script.

avatar image syclamoth · Mar 12, 2012 at 01:06 AM 0
Share

Right. Well, I must say, that wasn't really obvious. In any case, I think you've got it the wrong way around- the arms need to use the default FP camera controller, and the camera needs to be slaved to them. This way, the arms will always be where the player points them, but the camera can lag behind a little.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Aim Down Sights and Weapon Sway not working unless animations are not assigned 0 Answers

Transform.position bullet won't align 1 Answer

Gun not rotating with the camera? 1 Answer

Gun sways way too much 0 Answers

Which is better, shooting a bullet from camera or from the gun itself? 3 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