Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Feb 22 at 04:30 PM by ZeroIQ_Scripter for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by ZeroIQ_Scripter · Feb 22, 2020 at 04:00 PM · scripting problemtransformsliderslidingslide

Why is my Player sliding in one direction?

So if I press left shift then my Player slides to one direction. but if I look all up then it goes to x and when i look all the way down then it goes to z axis. Any ideas why?

Script:

     if (Input.GetKey (KeyCode.LeftShift)) {
         canSlide = true;
         GotTheLastLook = true;
     }

     if (GotTheLastLook) {
         f = fpsCam.localRotation.x;
         GotTheLastLook = false;
     }

     if (canSlide) {
         SlideKeyPressed = true;
         Sliding = true;
         canSlide = true;
     }
         
     if (SlideKeyPressed) {
         SlidingTime -= Time.deltaTime;
     }
         
     if (Sliding) {
         transform.localScale = new Vector3 (1, slidingScale, 1);
         canSlide = false;
     }
         
     if (SlidingTime <= 0.9f) {
         rb.AddForce (new Vector3(0, 0, f * Time.deltaTime * slidingSpeed));
         WalkingAllowed = false;
     }

     if (SlidingTime <= 0f) {
         transform.localScale = new Vector3 (1, normalScale, 1);
         SlidingTime = 1.0f;
         Sliding = false;
         SlideKeyPressed = false;
         canSlide = false;
         if (rb.velocity.magnitude > .01) {
             rb.velocity = Vector3.zero;
         }
     }

     if (SlidingTime >= 1) {
         WalkingAllowed = true;
     }
     //---------------------------------------------------------------------
Comment
Add comment · Show 6
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 alwayscodeangry · Feb 22, 2020 at 06:00 PM 1
Share

Couple of questions:

  • Can your player look to the left and right as well as up and down (like in a regular FPS)?

  • What type of object is fpsCam? (I'm guessing it's a Transform?)

avatar image ZeroIQ_Scripter alwayscodeangry · Feb 22, 2020 at 08:07 PM 0
Share
  1. Yes

  2. fpsCam is the normal Camera.

avatar image ZeroIQ_Scripter alwayscodeangry · Feb 22, 2020 at 08:10 PM 0
Share

Yes fpsCam is a transform

avatar image ZeroIQ_Scripter alwayscodeangry · Feb 22, 2020 at 08:23 PM 0
Share

@alwayscodeangry also when i looked all the way down x rotation was 100 then the slide was pretty smooth now i clamped it to 90 now it isnt so smooth. Like if i look everywhere the slide is bad but when its x rotation is 100 the slide gets smooth

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by alwayscodeangry · Feb 23, 2020 at 12:45 AM

The problem here is on line 27:

 rb.AddForce (new Vector3(0, 0, f * Time.deltaTime * slidingSpeed));  

 

What's happening here is that AddForce is applying a force of some magnitude along the global z-axis (so the world forwards / backwards). This is why it appears to be correct when facing certain directions. The 'f' variable is also a problem as it's being set based on the tranform's x-rotation (i.e. the camera's pitch (or up / down rotation) in the case of the FPS camera). I'd just get rid of it completely.
 

What I think you actually want to do is apply the force along the camera's local axis. You can get this from the camera's transform like so:

 rb.AddForce(fpsCam.transform.forward * Time.deltaTime * slidingSpeed);

 

Hopefully this will give you the desired behaviour or at least get you on the right track :)
 

Note: I'm assuming it's the z-axis you want to move along. You can change to 'right' for the x-axis or even 'up' for the y-axis.

Comment
Add comment · Show 4 · 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 ZeroIQ_Scripter · Feb 23, 2020 at 10:44 AM 0
Share

Yes i want to addforce to forward. but also in the last direction like in cod. So you press the slide button and then you slide in the last look rotation.

avatar image ZeroIQ_Scripter · Feb 23, 2020 at 12:41 PM 0
Share

@alwayscodeangry How can I get the last look rotation? Like I tried with Vectors and Quaternions and i still cant get it i made so many scripts and nothing works.

avatar image alwayscodeangry ZeroIQ_Scripter · Feb 23, 2020 at 02:21 PM 0
Share

I'm afraid I don't know what you mean by 'last look rotation'. Could you clarify?

avatar image ZeroIQ_Scripter alwayscodeangry · Feb 23, 2020 at 02:33 PM 0
Share

@alwayscodeangry So lets say your y rotation is 20 and if you press shift to slide then you addforce to 20(the y rotation) and while you are sliding you can look everywhere but still sliding to the rotation of 20. but only y rotation (left and right).

Follow this Question

Answers Answers and Comments

238 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 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 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

Does anyone know how to create sliding movement? 1 Answer

Line render from an object to a click 0 Answers

My gameobject only moves to the right once and then stops, why? 3 Answers

Oculus VR Drag Slider With Finger 0 Answers

Trying to make an object "Flee" from the player but it won't work? 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