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 Lord_Slifer · Jul 26, 2013 at 09:31 PM · rotationnewbiespin

Get a character to rotate instead of revolve

I am new to Unity and working on my first project. I want to make my character rotate as in spin, which it does if I take off gravity. When gravity is on and it is grounded, however, it revolves in a small circle around some invisible point. Here is my code:

 var rollSpeed = 6.0;
 
 var fastRollSpeed = 2.0;
 
 var jumpSpeed = 8.0;
 
 var gravity = 20.0;
 
 var rotateSpeed = 4.0;
 
 var duckSpeed = .5;
 
 
 private var moveDirection = Vector3.zero;
 
 private var grounded : boolean = false;
 
 private var moveHorz = 0.0;
 
 private var normalHeight = 2.0;
 
 private var duckHeight = 1.0;
 
 private var rotateDirection = Vector3.zero;
 
 private var isDucking : boolean = false;
 
 private var isBoosting : boolean = false;
 
 private var isJumping : boolean = false;
 
 var isControllable : boolean = true;
 
 var controller : CharacterController ;
 
 controller = GetComponent(CharacterController);
 
 function FixedUpdate() {
 
     if(!isControllable)
         Input.ResetInputAxes();
     else{
         if (grounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= rollSpeed;
             
             
             if (Input.GetButton("Jump")) {
                 moveDirection.y = jumpSpeed;
                 isJumping = true;
                 }
             if(Input.GetButton ("Boost")) {
                 moveDirection *=fastRollSpeed;
                 isBoosting = true;
             }
             if(Input.GetButton("Duck")) {
                 controller.height = duckHeight;
                 controller.center.y = controller.height/2 + .25;
                 moveDirection *= duckSpeed;
                 isDucking = true;
             }
             if(Input.GetButtonUp("Duck")){
                 controller.height = normalHeight;
                 controller.center.y = controller.height/2;
                 isDucking = false;
             }
             if(Input.GetButtonUp("Boost")){
                 isBoosting = false;
             }
         }
         else
         {if(isJumping == true) {
             if(isBoosting == true){
                 if(Input.GetButton ("Jump")) {
                     moveDirection.y = jumpSpeed * 2;
                     isJumping = false;
                     isBoosting = false;
                     }
                 }
             }
         }
         moveHorz = Input.GetAxis("Horizontal");
         if (moveHorz > 0)
             rotateDirection = new Vector3(0, 1, 0);
         else if (moveHorz < 0)
             rotateDirection = new Vector3(0, -1, 0);
         else
             rotateDirection = new Vector3(0, 0, 0);
         moveDirection.y -= gravity * Time.deltaTime;
         
         var flags = controller.Move(moveDirection * Time.deltaTime) ;
         controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
         grounded = ((flags & CollisionFlags.CollidedBelow) !=0 );
         }
     }
         

 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jul 26, 2013 at 09:45 PM

Your issue is on line 44. You are using the 'Horizontal' axis for both movement and rotation when your character is grounded. My guess is that you added the rotation to a starter script that just moved the character (no rotation). The solution is to remove "Horzontal" from the calculation:

 moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
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

15 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

Related Questions

Spin a 2D object with mouse drag 2 Answers

Flip over an object (smooth transition) 3 Answers

Rotation stops when reach the limit. 4 Answers

Rotating projectile 2 Answers

Stop rotation on collision 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