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 F3RILLA · Jul 29, 2013 at 10:32 PM · movementaimaimingslowdownmovedirection

Slow Character Movement

Hey everyone,

I have been trying to make my character slow down when the right mouse button ("Fire2") is pressed, as this button allows the player to zoom and aim at an object. I have tried to add an IF Statement but still would not work, can anyone help me with this issue?

This is the player movement script I have wrote by following ETeeskiTutorials on YouTube.

Thanks a lot for the help guys, really appreciate it.

 #pragma strict
 
 //Movement Variables
 var WalkAcceleration : float = 5;
 var WalkDeacceleration : float = 5;
 var MaxWalkingAcceleration : float = 20;
 
 @HideInInspector //Hide next line from inspector
 var WalkDeaccelerationVelocityX : float;
 @HideInInspector //Hide next line from inspector
 var WalkDeaccelerationVelocityZ : float;
 @HideInInspector //Hide next line from inspector
 var HorizontalMovement : Vector2;
 var MainCamera : GameObject;
 
 function Update () 
 {
     //Get and save the velocity of X and Z axis as rigidbody - MaxWalkingAcceleration
     HorizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
     
     //Magniture function returns the length of this vector - MaxWalkingAcceleration
     if (HorizontalMovement.magnitude > MaxWalkingAcceleration)
     {
         HorizontalMovement = HorizontalMovement.normalized; 
         HorizontalMovement *= MaxWalkingAcceleration; //Set speed to MaxWalkingAcceleration
     }
     
     //Apple Horizontal moveemnt to rigidbody - MaxWalkingAcceleration
     rigidbody.velocity.x = HorizontalMovement.x;
     rigidbody.velocity.z = HorizontalMovement.y;
     
     //De-accleration when Character is not moving - WalkDeacceleration
     if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
     {
         //Decrease speed to WalkDeacceleration smoothly
         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0 , WalkDeaccelerationVelocityX, WalkDeacceleration);  
         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0 , WalkDeaccelerationVelocityZ, WalkDeacceleration);
     }
         
     //Rotation Y was inherited from MouseLook script - WalkAcceleration
     transform.rotation = Quaternion.Euler(0, MainCamera.GetComponent(MouseLookScript).CurrentYRotation, 0);
     
     //Horizontal Value was changed from "Input.GetAxis("Horizontal") * WalkAcceleration" to 0 - WalkAcceleration
     rigidbody.AddRelativeForce(0, 0, Input.GetAxis("Vertical") * WalkAcceleration);
     
 }
Comment
Add comment · Show 2
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 ofishpega · Jul 30, 2013 at 02:44 AM 0
Share

I am not even seeing a if Input.GetButtonDown("Fire2") in your script... $$anonymous$$ake sure you set up your buttons in the player settings too, or Unity will have no idea what Fire2 is.

avatar image F3RILLA · Jul 30, 2013 at 03:26 PM 0
Share

That's because I removed it, as it wasn't working :)

1 Reply

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

Answer by Xtro · Jul 30, 2013 at 05:57 PM

1) Read the axis value into a variable at the beginning of the update method (Never call the same input method twice in an update call)

2) Use the variable in the code instead of GetAxis call.

3) After reading the axis into the variable, check the fire button and reset the variable.

It should be like that (C# code)

 var Vertical = Input.GetAxis("Vertical");
 if (Input.GetButtonDown("Fire2")) Vertical = 0;
 //...rest of your code...
 rigidbody.AddRelativeForce(0, 0, Vertical * WalkAcceleration);
Comment
Add comment · Show 1 · 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 F3RILLA · Jul 30, 2013 at 06:07 PM 1
Share

Thanks for the advice, I'll play around with the script in a little while Xtro and make it function.

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

18 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

Related Questions

Aiming With my mouse 1 Answer

Problem with gun rotation after flipping character (2D) 1 Answer

Third Person Aiming 0 Answers

how to make tank aiming 1 Answer

Aim at touch (2D). 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