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 Bincredible · Sep 30, 2013 at 07:10 PM · rotationmovementcharacterunity4

Help with Character Controller

Hey, I have made a Character Controller that moves around, it works fine but when you go from right to left or forward to backward the transition between directions isn't smoothed and the transform.forward just changes instantly. I was wondering if anyone knew how to make the transition smooth so the the Player rotates from the current rotation to the next for example right to left. Here is my current Code, if anyone can help that would be nice.

 private var moveSpeed : float = 12.0;
 private var Drag = 3;
 private var ADrag = 3;
 private var grounded : boolean;
 private var jumpVelocity : float = 8;
 
 private var bottomRight = (Vector3.right + Vector3.back).normalized;
 private var bottomLeft = (Vector3.left + Vector3.back).normalized;
 private var topRight = (Vector3.right + Vector3.forward).normalized;
 private var topLeft = (Vector3.left + Vector3.forward).normalized;
 
 function Update(){
 
 if(grounded){
 
 rigidbody.drag = Drag;
 rigidbody.angularDrag = ADrag;
 
 }
 
         if(Input.GetButton("Forwards")){
         transform.forward = Vector3.forward;
             rigidbody.AddForce(transform.forward * moveSpeed);
         }
         if(Input.GetButton("Backwards")){
         transform.forward = Vector3.back;
             rigidbody.AddForce(transform.forward * moveSpeed);
         }
         if(Input.GetButton("Right")){
         transform.forward = Vector3.right;
             rigidbody.AddForce(transform.forward * moveSpeed);
         }
         if(Input.GetButton("Left")){
         transform.forward = Vector3.left;
             rigidbody.AddForce(transform.forward * moveSpeed);
         }
         if(Input.GetButton("Forwards") && Input.GetButton("Right")){
         transform.forward = topRight;
             rigidbody.AddForce(transform.forward * moveSpeed/2);
         }
         if(Input.GetButton("Forwards") && Input.GetButton("Left")){
         transform.forward = topLeft;
             rigidbody.AddForce(transform.forward * moveSpeed/2);
         }
         if(Input.GetButton("Backwards") && Input.GetButton("Right")){
         transform.forward = bottomRight;
             rigidbody.AddForce(transform.forward * moveSpeed/2);
         }
         if(Input.GetButton("Backwards") && Input.GetButton("Left")){
         transform.forward = bottomLeft;
             rigidbody.AddForce(transform.forward * moveSpeed/2);
         }
     
     if(!grounded){
     
     rigidbody.drag = 7;
     rigidbody.angularDrag = 7;
     
     }
     
 }
 
 
 function OnCollisionEnter(){
     grounded = true;
 }
 
 function OnCollisionExit(){
     grounded = false;
 }
 
 function OnCollisionStay(){
     if(Input.GetButtonUp("Jump")){
         rigidbody.velocity.y = jumpVelocity;
     }
 
 }
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 OrbitSoft · Sep 30, 2013 at 07:48 PM

I'm making a game which has driveable tanks and i managed to make it smooth. Here's an example that should work for you:

 var Speed : float;
 var MaxSpeed : float;
 var MinSpeed : float;
 
 var SideSpeed : float;
 var Maxside : float;
 var Minside : float;
 
 function Update() {
 
 // Forward
   if (Input.GetKey("Up")) {
   if (Speed < MaxSpeed) {
   Speed += 0.01;
   }
   } else {
   if (Speed > 0.01) {
   Speed -= 0.01;
   }
 
 // Back
   if (Input.GetKey("Down")) {
   if (Speed > MinSpeed*-1) {
   Speed -= 0.01;
   }
   } else {
   if (Speed < 0.01) {
   Speed += 0.01;
   }

 // left and right here

 transform.position += transform.forward * Speed * Time.deltaTime;

 // move to the side code here
 }

To move on the sides you just need to put the same code but replace Speed with SideSpeed and the min and max values. Hope this helped you.

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

Multiple Cars not working 1 Answer

How to make camera move backwards in relation to player (so behind player), regardless of y rotation of player and camera 1 Answer

Character movement with rotation 1 Answer

Make the character face the direction he is moving? 2 Answers

How to smoothly rotate camera around an object with touch 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