Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 watercolorheartdev · Oct 17, 2017 at 09:42 PM · rotationrigidbodyplayer

C#, Unity 2017.2.0f3, Rotating RigidBodyFPS Controller 90 and -90 degrees on button press?

My current code attached to the Player One RigidbodyFPSController in Standard Assets is as follows:

 // Update is called once per frame
 void Update () {
     //rotate the player with Q and E
     if (Input.GetKeyDown("q"))
     {
         transform.Rotate(0, 90, 0);
     }


     if (Input.GetKeyDown("e"))
     {
         transform.Rotate(0, -90, 0);
     }
 }

However, when actually pushing the buttons, the player seems to snap back to how they were facing and not rotating. How do I get it to do what I want?

Comment
Add comment · Show 9
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 unit_nick · Oct 18, 2017 at 02:58 AM 1
Share

Input.GetAxis used in the controller is a read only value. It is being read after you have rotated your player and setting the players rotation based on that.

If you want to manipulate the controller you will have to add your own offset value to the value read by GetAxis. eg

 // in your code. lets say it's in $$anonymous$$yClass 
 // use Edit > Project Settings > Script Execution Order if you need to make sure this script runs before FPSController
 public static float myYOffset = 0;
 private void Update()
 {
     if (Input.Get$$anonymous$$eyDown("q"))
     {
         myYOffset += 90;
     }
     if (Input.Get$$anonymous$$eyDown("e"))
     {
         myYOffset -= 90;
     }
 }

.

 // in FPSController
 Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), $$anonymous$$yClass.myYOffset, Input.GetAxis("Vertical"));


avatar image watercolorheartdev unit_nick · Oct 21, 2017 at 11:24 PM 0
Share

I tried your code, and replaced the $$anonymous$$yClass with the name of the script also attached to the object (PlayerOne in my case) and it flags it as "PlayerOne does not exist in the current context" and that targetVelocity cannot directly contain members such as fields or methods.

avatar image unit_nick watercolorheartdev · Oct 22, 2017 at 02:27 AM 0
Share

Well I know my code works. Post both your scripts if you want me to show you how to implement it.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by AlienNova · Oct 21, 2017 at 11:52 PM

I would highly suggest reading https://docs.unity3d.com/ScriptReference/Transform.Rotate.html to fully understand what Rotate() does

The code you're looking for is more along the lines of

 if (Input.GetKey(KeyCode.Q))
     transform.Rotate (0, 1 * time.DeltaTime * speed, 0);
 if (Input.GetKey(KeyCode.E))
     transform.Rotate (0, -1 * time.DeltaTime * speed, 0);

Speed is how fast/slow you want it to rotate

Comment
Add comment · Show 3 · 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 watercolorheartdev · Oct 23, 2017 at 03:47 AM 0
Share

Is that formatted right? It's telling me time and speed don't exist in the current context while calling it within void Update () { xxxx }

avatar image AlienNova watercolorheartdev · Oct 24, 2017 at 01:55 AM 0
Share

$$anonymous$$ake sure your declaring speed first. float speed = (Enter number here); in your class

avatar image watercolorheartdev AlienNova · Oct 24, 2017 at 03:53 PM 0
Share

Thanks, do you know what is causing this red flag now? http://prntscr.com/h1g5or

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

127 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

Related Questions

Rigidbody.AddForce and incorrect rotation 2 Answers

How to have an object inherit rotation of player 1 Answer

How to make a player rotate in a direction with a slope of the ground. 0 Answers

Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers

Pysics not working as expected 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