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 Dino0040 · Jul 31, 2012 at 12:41 PM · rotationmovementgundynamicangles

Need help with my weapon movement script

The script is intended to rotate my Gun on the x angle when I move my mouse horizontal. My script works fine as long MoveOnX is positive, But when its negative, it changes the x, y and z rotation. Can someone help me? ## GunMovement.js ##

 public var MoveAmount : float = 1;
  
 public var MoveSpeed : float = 2;
  
 public  var GUN: GameObject;
 public  var CAM: GameObject;
  
  
 public var MoveOnX : float;
  
 public var MoveOnY: float;
    
 public var DefaultPos : Vector3;
 
 public var DefaultRot : Vector3;
     
 public var NewGunPos : Vector3;
 
 public var NewGunRot : Vector3;
     
 public var ONOff : boolean = false;
     
 function Awake(){
 
 
   DefaultPos = transform.localPosition;
   DefaultRot = transform.localEulerAngles;
   ONOff = true;
  
 }
 
 
 
 function LateUpdate () {
 
  
    if(ONOff == true){
      
  MoveOnX = Input.GetAxis( "Mouse X") * Time.deltaTime * MoveAmount;
  MoveOnY = Input.GetAxis( "Mouse Y") * Time.deltaTime * MoveAmount;
  NewGunPos = new Vector3 ( DefaultPos.x+ MoveOnX, DefaultPos.y + MoveOnY, DefaultPos.z);
  NewGunRot = Vector3 ( DefaultRot.x + MoveOnX * 800, DefaultRot.y, DefaultRot.z);
  GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition , NewGunPos, MoveSpeed * Time.deltaTime);
  GUN.transform.localEulerAngles = Vector3.Lerp(GUN.transform.localEulerAngles , NewGunRot, MoveSpeed * Time.deltaTime);
  
 }
 
 else{
 
    ONOff = false;
    
  GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition, DefaultPos , MoveSpeed *Time.deltaTime);
 
 
 }
 
 }
Comment
Add comment · Show 3
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 DESTRUKTORR · Jul 31, 2012 at 03:51 PM 0
Share

For the record: Use code tags (highlight all the code, block by block, and click the "101010" button) when pasting code. It's much appreciated by both those looking to answer your question, and those who had the same question, and are looking to the answers for guidance.

avatar image Dino0040 · Jul 31, 2012 at 06:09 PM 0
Share

Did what you said; changes nothing.

avatar image Loius · Jul 31, 2012 at 08:12 PM 1
Share

Your whitespace and brackets are very funky, which makes it tricky to read. Code should look at least similar to this (there are lots of different "where brackets go" beliefs, but they should line up somehow)

 for ( stuff ) {
   do;
 }
 if ( thing ) {
   if ( not another ) {
     while ( false ) { print; }
   } else {
     win;
   }
 }

When you have something like

 }
 }

, it can get really tricky to read.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Akill · Jul 31, 2012 at 08:03 PM

Can you give some background as to why you are doing this? Could your problem possible be solved by parenting your gun to the players camera?

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 Loius · Jul 31, 2012 at 08:21 PM

It looks like a gimbal lock issue. Try setting the gun's .rotation to a Quaternion.Euler instead of directly changing its euler angles.

This produces smoother results for me:

  NewGunRot.x += MoveOnX * 800; // = Vector3 ( DefaultRot.x + MoveOnX * 800, DefaultRot.y, DefaultRot.z);
  //GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition , NewGunPos, MoveSpeed * Time.deltaTime);
  GUN.transform.localRotation = Quaternion.Euler( NewGunRot );
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

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

9 People are following this question.

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

Related Questions

How to make player move the way the joystick is moving [Example Shown] 1 Answer

How to make dynamic movement for gun. 0 Answers

How could I play the correct animations for a character who always rotates to look at the mouse, but takes World relative input? 0 Answers

Rotation used as movement 4 Answers

Joystick for WASD & Joystick for Camera? 0 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