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 slaQ · Jul 16, 2011 at 03:55 AM · movementcharactercontrollerdirection

4 way direction movement using a Character Controller.

Dear reader,

I'm making a 3d platformer with a 2d sprite character using Sprite Manager 2. I have a working 4-way movement script, using simple [transform.Translate], However the collisions and such are just not as good as with a Character Controller.

This is the script I'm using atm:

     var speed : int = 4;
     var mySprite : PackedSprite;
     var jumpspd : int = 20;
     
     function Start () {
     
     var mySprite : PackedSprite = gameObject.GetComponent("PackedSprite");
     
     }
     
     function Update () {
     
             if(Input.GetButtonDown("Run")){
                 speed = (speed*1.5);
             }
             else if(Input.GetButtonUp("Run")){
                 speed = (speed*0.75);
             }
             
             if(Input.GetButton("left")){
                 transform.Translate(-speed*Time.deltaTime,0,0);
                 mySprite.DoAnim(2);
                 
             }
             else if(Input.GetButton("right")){
                 transform.Translate(speed*Time.deltaTime,0,0);
                 mySprite.DoAnim(3);        
         
             }
             else if(Input.GetButton("up")){
                 transform.Translate(0,0,speed*Time.deltaTime);
                 mySprite.DoAnim(1);
                 
             }
             else if(Input.GetButton("down")){
                 transform.Translate(0,0,-speed*Time.deltaTime);
                 mySprite.DoAnim(0);
                 
             }
             else if(Input.GetButtonUp("left")){
                 mySprite.DoAnim(6);
                 
             }
             else if(Input.GetButtonUp("right")){
                 mySprite.DoAnim(7);        
         
             }
             else if(Input.GetButtonUp("up")){
                 mySprite.DoAnim(5);
                 
             }
             else if(Input.GetButtonUp("down")){
                 mySprite.DoAnim(4);
                 
             }
             
             if(Input.GetButtonUp("Jump")){
                 rigidbody.AddForce(0,jumpspd,0);
             }
         
 
 }


I would like to make the same 4-way movement, but using the Character Controller. As you can see. for every one of the 4 directions I need to play a different animation (using sprite manager 2). And also there needs to be an idle animation for every direction, once the corresponding button has been released.

So preferably I would have the movement split up into four different keys, like I have right now, instead of the getAxis thing. Not sure if thats possible when using a character controller tho. Probably inefficient anyways.

Any help is appreciated!

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 Meater6 · Jul 16, 2011 at 05:44 AM

The character controller I think is the most useful and efficient component for characters. One thing you can make more efficient is to use the Input.GetAxis, which is much easier and simpler.

transform.Translate is one of the worst ways (I think) to move any physical object. For character controllers, you have Move(), by far better.

You can add directions depending on your input, for example:

Direction.z > 0 do anim(1)

Direction.z < 0 do anim(0)

Direction.x > 0 do anim(3)

Direction.x < 0 do anim(2)

It really is that simple. Hope this helps.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Character floats on backwards walk 0 Answers

Jaggie, uneven speed with basic movement script. 0 Answers

Move in the direction its facing C# 2 Answers

Make the player face his movement direction 10 Answers

How to make character face towards movement direction? 4 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