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 K_Tec · Jun 26, 2017 at 09:24 AM · scripting problemtransformcharactercontroller

How would i rewrite this script with a Character Controller? C#

Hello Everybody,

i was working for some Time on my Player and i wrote a Script. It is working well- but i can glitch throught walls and objects. I searched in Forums that i could use the Character Controller. I searched for the Character Controller Scripting but i did not really understand how i can use this in my script. Can somebody show me how i implement a CC in my script- and its working like before? Transform.Translate was not a good idea! And the Character should walk and run at the same speed; if the framerate is 10 or 200, so if should be in Update() but * Time.deltaTime- or is this normal at the CC??

Please Help, Thanks in Advance,

Heres my Code:

 public float walkspeed;
     public float runspeed;
     [HideInInspector]
     public Rigidbody rigid;
     public float jumpspeed;
     public GameObject cam;
     [HideInInspector]
     public bool isrunning;
     [HideInInspector]
     public bool walkshake;
     public Rigidbody rigidb;
     
     
 
     void Update()
     {
         if (Input.GetKey("left shift") && Input.GetAxis("Vertical") > 0)
         {
             float trans = Input.GetAxis("Vertical") * runspeed;
             float strafe = Input.GetAxis("Horizontal") * runspeed;
             trans *= Time.deltaTime;
             strafe *= Time.deltaTime;
             transform.Translate(strafe, 0, trans);
             isrunning = true; //this bools are for the camera- play the run or walk shake anim- first person
             walkshake = false;
         }
 
         else if (Input.GetKeyDown("space"))
         {
             rigidb.AddForce(0, jumpspeed, 0);
 
         }
 
         else
         {
             float trans = Input.GetAxis("Vertical") * walkspeed;
             float strafe = Input.GetAxis("Horizontal") * walkspeed;
             trans *= Time.deltaTime;
             strafe *= Time.deltaTime;
             transform.Translate(strafe, 0, trans);
             if (trans > 0) {
                 walkshake = true;
             }
             else
             {
 
                 walkshake = false;
 
             }
             isrunning = false;
         }
     
 
         
         
 
 
 
     }
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

Answer by Major · Jun 26, 2017 at 06:27 PM

To use the CC you have to call the function ChatacterController.Move (). How this function works is by taking the current position of the player (transform.position), and then adding the next position.

The "next position" is defined by your input. I would recommend storing this as a Vector3 because it more easioy allows you to normalize your direction vectors (in my opinion). This is important because with out doing so, you will move faster when moving diagonally.

This is pretty simple to set up: new Vector3 (horizontal input, 0, vertical input).normalized

Make sure this vector is convertes to world space using transform.TransformVector ().

Then call CC.Move(): CC.Move (input speed Time.deltaTime);

Now for jumping. The CC already gets grounding info, and this bool can be accessed with CharacterController.isGrounded.

To actually jump you need to store a vertical velocity. You will have to solve for this velocity using the handy equations: KE = 0.5mv^2 PE = mgh Set then equal, and solve for v, and you get v = sqrt (2gh) where g is gravity and h is jump height. It is important to note that g must be positive.

So to sum up, set your vertical speed variable to sqrt (2gh) whenever you jump.

Next up we must make the CC behave according to gravity. To do this we are going to substract from our vertical speed variable whenever we are grounded. Using v = at this is pretty simple:

vertical speed -= g * Time.deltaTime

Note: when the CC is grounded, set your vertical speed to something like this:

vertical speed = - g * Time.deltaTime

In my experience the CC does not t being grounded very reliably otherwise.

So finally, the last thing to do is to apply the certical speed to the player using the CC.Move() function. This can be done like so:

CC.Move (transform.up × vertical speed × Time.deltaTime)

Hope his help, and I hope I did all this correctly!

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 K_Tec · Jun 26, 2017 at 07:48 PM 0
Share

Thank you :)) Thats was exactly what i needed- i didnt found all functions in the Forums before. Thank you for helping me =)

Best Regards,

$$anonymous$$

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

122 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

Related Questions

Player acts weirdly when I release movement input 0 Answers

Is it possible to intercept variable alteration/function calls on an object (From within the object)? 0 Answers

Inverted Char control 1 Answer

Controller Movement Problem 1 Answer

Transforming a GameObject with an unattached script 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