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 Dolmek · Jun 04, 2018 at 09:26 AM · movementscript.ground detectionimprove

Help Improve movement script

Hi guys there is anyway to improve this code? when the player move down on a surface that have a slope it doesn't touch the ground and make "little jump" that is unrealistic and kind of annoying.

I gonna quote the whole code I have, if you have any suggestion is welcomed.

 /// <summary> 
 /// Charactercontrollercs.cs 
 /// Character Controller in CSharp v2.3 
 /// </summary> 
 using UnityEngine; 
 
 public class CharacterControllercs : MonoBehaviour { 
              
     //Var definition 
     public bool swimming = false;                    //Can be triggert to slow down the movements (like when u swim) 
     public string moveStatus = "idle";                //movestatus for animations 
      
     //Movement speeds 
     private float jumpSpeed = 8.0f;                    //Jumpspeed / Jumpheight 
     private float gravity = 20.0f;                    //Gravity for jump 
     private float runSpeed = 10.0f;                    //Speed when the Character is running 
     private float walkSpeed = 4.0f;                    //Speed when the Character is walking (normal movement) 
     private float rotateSpeed = 250.0f;                //Rotationspeed of the Character 
     private float walkBackMod = 0.75f;                //Speed in Percent for walk backwards and sidewalk 
      
     //Internal vars to work with 
     private float speedMod = 0.0f;                    //temp Var for Speedcalculation 
     private bool grounded = false;                    //temp var if the character is grounded 
     private Vector3 moveDirection = Vector3.zero;    //move direction of the Character 
     private bool isWalking = false;                    //toggle var between move and run 
     private bool jumping = false;                    //temp var for jumping 
     private bool mouseSideButton = false;            //temp var for mouse side buttons 
     private float pbuffer = 0.0f;                    //Cooldownpuffer for SideButtons 
     private float coolDown = 0.5f;                   //Cooldowntime for SideButtons 
     private CharacterController controller;            //CharacterController for movement 
      
     //Every Frame 
     void Update () 
     { 
         //Set idel animation 
         moveStatus = "idle"; 
         isWalking = true; 
              
         // Hold "Run" to run 
         if(Input.GetAxis("Run") != 0) 
             isWalking = false; 
          
         // Only allow movement and jumps while grounded 
         if(grounded) { 
              
             //movedirection 
             moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical")); 
              
             //pushbuffer to avoid on/off flipping 
             if(pbuffer>0) 
                 pbuffer -=Time.deltaTime; 
             if(pbuffer<0)pbuffer=0; 
                             
             //Automove Sidebuttonmovement 
             if((Input.GetAxis("Toggle Move") !=0) && pbuffer == 0){ 
                 pbuffer=coolDown; 
                 mouseSideButton = !mouseSideButton; 
             } 
             if(mouseSideButton && ((Input.GetAxis("Vertical") != 0) || Input.GetButton("Jump")) || (Input.GetMouseButton(0) && Input.GetMouseButton(1)))
                 mouseSideButton = false;             
              
             //L+R MouseButton Movement 
             if (Input.GetMouseButton(0) && Input.GetMouseButton(1) || mouseSideButton) 
                 moveDirection.z += 1; 
             if (moveDirection.z > 1)
                 moveDirection.z = 1;
                  
             //Strafing move (like Q/E movement     
             moveDirection.x -= Input.GetAxis("Strafing"); 
                  
               // if moving forward and to the side at the same time, compensate for distance 
               if(Input.GetMouseButton(1) && (Input.GetAxis("Horizontal") != 0) && (Input.GetAxis("Vertical") != 0)) { 
                 moveDirection *= 0.7f; 
               } 
                                
             //Speedmodification / is moving forward or side/backward 
             speedMod = ((Input.GetAxis("Vertical") < 0) || (Input.GetMouseButton(1) && (Input.GetAxis("Horizontal")) != 0) || Input.GetAxis("Strafing") != 0) ? walkBackMod : 1.0f;
              
             //Use run or walkspeed 
             moveDirection *= isWalking ? walkSpeed * speedMod : runSpeed * speedMod; 
              
             //reduce movement by 70% when swimming is toggled    
             moveDirection*= swimming ? 0.7f : 1; 
                        
             // Jump! 
             if(Input.GetButton("Jump")){ 
                 jumping = true; 
                 moveDirection.y = jumpSpeed; 
             } 
              
             //movestatus normal movement (for animations)               
             if((moveDirection.x == 0 ) && (moveDirection.z == 0)) 
                 moveStatus = "idle";             
             if(moveDirection.z > 0) 
                 moveStatus = isWalking ? "walking" : "running"; 
             if(moveDirection.z < 0) 
                 moveStatus = isWalking ? "backwalking" : "backrunning";         
             if(moveDirection.x > 0) 
                 moveStatus = isWalking ? "sidewalking_r" : "siderunning_r"; 
             if(moveDirection.x < 0) 
                 moveStatus = isWalking ? "sidewalking_l" : "siderunning_l";     
              
             //movestatus swim movement (for animations)               
             if(swimming){ 
                 if((moveDirection.x == 0 ) && (moveDirection.z == 0)) 
                     moveStatus = "swimidle";             
                 if(moveDirection.z > 0) 
                     moveStatus = isWalking ? "swim" : "swimfast"; 
                 if(moveDirection.z < 0) 
                     moveStatus = isWalking ? "backswim" : "backswimfast";         
                 if(moveDirection.x > 0) 
                     moveStatus = isWalking ? "sideswim_r" : "sideswimfast_r"; 
                 if(moveDirection.x < 0) 
                     moveStatus = isWalking ? "sidewswim_l" : "sideswimfast_l";     
                 if(jumping) 
                     moveStatus = "swimup";                     
             }   
              
             //transform direction 
             moveDirection = transform.TransformDirection(moveDirection);         
          
         } 
         // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down. 
         if(Input.GetMouseButton(1)) { 
             transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0); 
         } else { 
             transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0); 
         } 
          
         //Apply gravity 
         moveDirection.y -= gravity * Time.deltaTime; 
          
         //Get CharacterController 
         controller = GetComponent<CharacterController>(); 
         //Move Charactercontroller and check if grounded 
         grounded = ((controller.Move(moveDirection * Time.deltaTime)) & CollisionFlags.Below) != 0; 
          
         //Reset jumping after landing 
         jumping = grounded ? false : jumping; 
          
         //movestatus jump/swimup (for animations)       
         if(jumping) 
             moveStatus = "jump"; 
         if(jumping && swimming) 
             moveStatus = "swimup";     
     } 
 }  

Is not my code I took from FCC. It work exactly how I wonted. But i can't make it work better with the ground. I'm workin on animation, but I'm not going to attach that part of the code.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Serellyn · Jun 04, 2018 at 12:15 PM

You can find some good information on these links

  • https://www.youtube.com/watch?v=ybljJGA1ksk

  • https://answers.unity.com/questions/388137/controllermove-doesnt-stay-grounded-when-walking-d.html

  • https://answers.unity.com/questions/259509/moving-character-down-a-ramp.html

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
avatar image
0

Answer by Dolmek · Jun 04, 2018 at 03:48 PM

Thank you!

Comment
Add comment · Show 1 · 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 Dolmek · Jun 10, 2018 at 07:56 PM 0
Share

unfortunately non of the suggestion in the links were working. I have solved in theis way:

 //$$anonymous$$ove Charactercontroller and check if grounded 
 grounded = ((controller.$$anonymous$$ove((moveDirection + new Vector3(0, -1, 0))* Time.deltaTime)) & CollisionFlags.Below) != 0;

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

135 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

Related Questions

I need to move an object when touched on an android phone 1 Answer

How do I build up speed while wallrunning? 1 Answer

Help with a character movement script? 1 Answer

How do I modify this to move the camera at the same time the player is moving? 3 Answers

Rare problem to moving an object using script+animation 0 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