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
-1
Question by malcolmtwl · Dec 12, 2011 at 11:17 AM · character

Character Rotation

 function UpdateSmoothedMovementDirection ()
 {
             var cameraTransform = Camera.main.transform;
             var grounded = IsGrounded();
             // Forward vector relative to the camera along the x-z plane         
             var forward = cameraTransform.TransformDirection(Vector3.forward);
             forward.y = 0;
             forward = forward.normalized;
             // Right vector relative to the camera
             // Always orthogonal to the forward vector
             var right = Vector3(forward.z, 0, -forward.x);
  
             var v = Input.GetAxisRaw("Vertical");
             var h = Input.GetAxisRaw("Horizontal");

             if (v < -0.2)
                         movingBack = true;
             else
                         movingBack = false;
            
             var wasMoving = isMoving;
             isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
                        
             // Target direction relative to the camera
             var targetDirection = h * right + v * forward;
            
             if (grounded)
             {
                         // Lock camera for short period when transitioning moving & standing still
                         lockCameraTimer += Time.deltaTime;
                         if (isMoving != wasMoving)
                                     lockCameraTimer = 0.0;
                          // We store speed and direction seperately,
                         // so that when the character stands still we still have a valid forward direction
                         // moveDirection is always normalized, and we only update it if there is user input.
                         if (targetDirection != Vector3.zero)
                         {
                                     // If we are really slow, just snap to the target direction
                                     if (moveSpeed < walkSpeed * 0.9 && grounded)
                                     {
                                                 moveDirection = targetDirection.normalized;
                                     }
                                     // Otherwise smoothly turn towards it
                                     else
                                     {
                                                 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
                                                
                                                 moveDirection = moveDirection.normalized;
                                     }
                         }
                        
                         // Smooth the speed based on the current target direction
                         var curSmooth = speedSmoothing * Time.deltaTime;
                        
                         // Choose target speed
                         //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
                         var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
            
                         _characterState = CharacterState.Idle;
                         moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
                        
                         // Reset walk time start when we slow down
                         if (moveSpeed < walkSpeed * 0.3)
                                     walkTimeStart = Time.time;
             }
             else
             {
                         if (jumping)
                                     lockCameraTimer = 0.0;
  
                         if (isMoving)
                                     inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
             }
            
  
                        
 }
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 syclamoth · Dec 12, 2011 at 11:28 AM 2
Share
  1. sscce.org

  2. Please format your code better. This is truly disgusting.

avatar image Kacer · Dec 12, 2011 at 12:09 PM 1
Share

my eyes T_T

1 Reply

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

Answer by fafase · Dec 14, 2011 at 08:29 AM

Hey, I guess you want your guys to move forward and backward with W and S and simply rotate but no displacement with A and D. This simple code should do the trick, they are supposed to be used with the arrow pad though:

var speed = 3.0; //Speed you guy will move forward/backward var rotateSpeed = 3.0; // speed at which it will rotate around Y-axis

function Update () { var controller : CharacterController = GetComponent(CharacterController); // this get you character controller attached to the object

 //rotate around y-axis
 transform.Rotate(0, Input.GetAxis ("Horizontal")* rotateSpeed, 0);
 
 //Move Forward-Backward
 var forward = transform.TransformDirection(Vector3.forward);
 var curSpeed = speed * Input.GetAxis("Vertical");
 controller.SimpleMove(forward * curSpeed);

}

@script RequireComponent(CharacterController)

Let us know if that worked for you. I might have understood your question wrong.

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 malcolmtwl · Dec 15, 2011 at 04:43 PM 0
Share

Oh yes it words fine thankyou ^_^

However, is there a method to rotate it just 90 degrees once ins$$anonymous$$d of continuing to rotate?

avatar image fafase · Dec 15, 2011 at 06:09 PM 1
Share

well you can give it 90deg rotation on the y axis but pb would be that your guy would turn 90deg each frame so you would not even see it spinning so you might have to change the input checking and use a input.getkeydown. Have a look http://unity3d.com/support/documentation/ScriptReference/Input.Get$$anonymous$$eyDown.html and http://unity3d.com/support/documentation/ScriptReference/Transform.Rotate.html

$$anonymous$$y advice, take the code that is working then apply the new input function, look at the unity explanations, and for the transform.rotate, start to alter the parameters until you get what you want. You might even find better than you were expecting and you will learn the effect of each parameters. $$anonymous$$y hint, you need quaternion which is something quite complicated to master.

avatar image malcolmtwl · Dec 23, 2011 at 02:37 PM 0
Share

Thanks a lot to you ^_^

I guess i will have more trouble with quaternion too.

Nice day (:

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

7 People are following this question.

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

Related Questions

Activating things- What on earth am I doing wrong??? 2 Answers

Character Rotation 2 Answers

C# CharacterMotor crashing. What am I doing wrong? 1 Answer

Velocity for movement 0 Answers

Character-Only Shadows 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