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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ryno9788 · Nov 10, 2012 at 12:48 AM · c#movementcharactercontroller

super messed up speed

I have been working on this new script for my new controller for my fps Game. Right now I believe it will technically how I want it but its now working very fast, too fast!

The problem begins with the basic movement forwards and backwards I wanted to make the controller to be able to still have a little control while falling. Well I had some problems with collision (any object that I walked into I would just go through other than the ground) so I had to change the way my player moves. This is were my problems started. I fixed the collision problem but now my player is moving super fast (I've turned down the speed but I'd like it fixed before future issues) the gravity is having the same issue but the worst part is the jumping. No matter how low I turn it down it will either not go that far or transport above in about 1 or 2 frames. Can someone please find and fix these errors?

 using UnityEngine;
 using System.Collections;
 
 public class basicmove : MonoBehaviour {
     public bool NotClimbing = true;
     public bool HasJumped = false;
     public float speed = 10.0F;
     public float sidestepSpeed = 6.0F;
     public float gravity = 12.0F;
     public float jumpSpeed = 8.0F;
     
     public Vector3 Wall = Vector3.zero;
     // this variable used to meen somthing else but I've neglected to change the name 
     //it does seem unessisary
     private Vector3 moveDirection = Vector3.zero;
     
     void Update() {
         CharacterController controller = GetComponent<CharacterController>();
 
         moveDirection.y -= gravity * Time.deltaTime;// applying gravity 
         controller.Move(moveDirection);
         
     if (controller.isGrounded)
         {    
          
             if(NotClimbing){//you are walking
                 
                         ////////moving axis/////////
                 
             moveDirection = new Vector3
                     (Input.GetAxis("Horizontal")* sidestepSpeed, 0,
                     Input.GetAxis("Vertical")* speed);
                moveDirection = transform.TransformDirection(moveDirection);
 
                 if (Input.GetButtonDown("Jump"))
             {
                 HasJumped = true;
                 
             }
             }
                         ////////moving axis/////////
             
             else //you are on the ground facing a wall
             {
                 moveDirection = new Vector3
                     (Input.GetAxis("Horizontal")* sidestepSpeed, Input.GetAxis("Vertical")* speed,0);
                moveDirection = transform.TransformDirection(moveDirection);
                 
 }
 }
         else{
             
                         ////////moving axis/////////
         HasJumped = false;
 
             if(NotClimbing){    
                 
                 moveDirection = new Vector3
                     (Input.GetAxis("Horizontal")*(sidestepSpeed*2), 0,
                     Input.GetAxis("Vertical")* ((speed/2)-(speed/4)));
                moveDirection = transform.TransformDirection(moveDirection);
             }
             else 
             {
                         ////////moving axis/////////                
                 moveDirection = new Vector3
                     (Input.GetAxis("Horizontal")* sidestepSpeed, Input.GetAxis("Vertical")* speed,0);
                moveDirection = transform.TransformDirection(moveDirection);
 }    }    }
     void LateUpdate() 
     {
     if(HasJumped){
     if (NotClimbing){        
     moveDirection.y = jumpSpeed;}// jump movement
             else{
                 moveDirection.z = (jumpSpeed * -1); //jump movement when climbing
             }
     
         }
     }
 }
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
Best Answer Wiki

Answer by Benproductions1 · Nov 10, 2012 at 07:18 AM

The problem is your not accounting for framerate. In your script your speed will always vary because Update is called every frame, but framerate isn't constant. To fix this there are 2 solutions:

Change Update to FixedUpdate, so that you fix the update rate to 50 times per second. The problem with this is, that at higher framerates the movement feels sluggish, your input axis and jump keys will be less accurate and might not call at all and it has a higher performance impact than Update.

The other solution is to incorporate Time.deltaTime into your movement calculations. Time.deltaTime is the time from now to the last frame in seconds. This means your speed will be set in seconds and will adjust to framerate.

Hope this helped, Benproductions1

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 ryno9788 · Nov 10, 2012 at 04:31 PM 0
Share

this fixed half my problem but how do I apply this to the jumping.

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

10 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Need helping getting a character controller to jump 1 Answer

[C#] Jump on slopes 1 Answer

How do I make my character controller jump when I use this code? 0 Answers

Distribute terrain in zones 3 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