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 darksblood · Jul 17, 2012 at 01:07 AM · animationcharactercontrollergravityjump

Getting this character controll script working right

Hello Unity Community, I hope everyone's having a great day! My little story is that I'm building a small project mostly for the learning experience. I've got every scripts done except the last piece of the project, the character control script. I've written this and it isn't going as I planned. What I'm trying to accomplish is to have a character who is running in one location. He isn't moving, just playing a run animation. When I make a vertical flick on the touch screen, the character jumps up and of course play a jump animation and then gets pulled back down and play a fall animation while falling. Instead, the result I'm getting is as followed. When I make a quick flick on the screen, the character flies way off screen ,and, when I do a small flick that doesn't move my finger much, he only jumps a little. This behavior also took place when i made an alternate key (space) to make him jump in the editor. The other problem is that when I'm in the editor and my phone and I hit the play button, he falls down from his high altitude while playing the proper animation. However, when I make him jump, he doesn't play the jump animation nor the fall when he comes back down.

I've been looking over the script for 3+ hours and still can't get it to work right. Would anyone care to point out where my logic is failing causing things to not work right? I would really appreciate some help getting past this.

Thank You in advance.

 [RequireComponent(typeof(CharacterController))]
 public class PlayerControl : MonoBehaviour
 {
    
     Vector2 StartPosition;
     public static string MyStringyVar;
    
     public static string mystringvar;
     public static bool OnTheGround;
     float _CharGravity = .2f;
     public static bool KeepRunning = true;
     float Airtime = 0;
     float HowLongInairBeforeItsAFall = .5f;
     public CharacterController _CharController;
     private Vector3 _MoveDirection;
     CollisionFlags _collisionflags;
     public float jumpheight;
    
     void Awake()
     {
     _CharController = GetComponent<CharacterController>();
        
     }
     void Start ()
     {
    
     _MoveDirection = Vector3.zero;
     animation.wrapMode = WrapMode.Loop;
     animation["run"].layer = 3; 
     }
    
     void Update ()
     {
         if(_CharController.isGrounded)
         {
        
       if(Input.touchCount > 0)
         {
           Touch thetouched = Input.touches[0];
            
             switch(thetouched.phase)
             {
                 case TouchPhase.Began:
                     {
                  StartPosition = thetouched.position;
                     break;
                     }
             case TouchPhase.Ended:
                 {
                  
                     float TheStoredfinalChangeInX = Mathf.Abs (thetouched.position.x - StartPosition.x);
                     float TheStoredfinalChangeInY = Mathf.Abs (thetouched.position.y - StartPosition.y);
                      
                       if(TheStoredfinalChangeInX > TheStoredfinalChangeInY)
                         {
                          float TheSignOftheX = Mathf.Sign (thetouched.position.x - StartPosition.x);
                          if(TheSignOftheX > 0)
                             {
                              MyStringyVar = "you went Right";
                             }
                         else
                             {
                             MyStringyVar = "you went left";
                             }
                         }
                       else
                         {
                         float TheSignOftheY = Mathf.Sign (thetouched.position.y - StartPosition.y);
                         if(TheSignOftheY > 0)
                             {
                          MyStringyVar = "UP";
                                 _MoveDirection.y += jumpheight;
                                 Jump();
                             }
                          else
                             {
                             MyStringyVar = "you went Down";
                             }
                         }
                 break;
                 }
             }
         }
             else  //this else is associated with the input
             {
             ContinueRunning ();
             }
         }
               //this else is associated with the if is grounded
        
         if((_collisionflags & CollisionFlags.CollidedBelow) == 0)   // if not on the ground
             {
              Airtime += Time.deltaTime;
                 if(Airtime >= HowLongInairBeforeItsAFall)
                 {
                 // Debug.Log ("time is " + Airtime);   
                 // Debug.Log ("this is a fall");   
                     FallDown ();
                 }
             }
             _CharController.Move (_MoveDirection);
         _MoveDirection.y -= _CharGravity * Time.deltaTime;
  }
    
     void Jump()
     {
     animation.CrossFade ("victory");   
     }
    
     void ContinueRunning ()
     {
             KeepRunningAnim ();
     }
     void FallDown()
     {
         animation.CrossFade ("die");
     }
    
      void KeepRunningAnim()
     {
         animation.CrossFade ("run");
     }
    
    
 }
Comment
Add comment · Show 1
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 ScroodgeM · Jul 17, 2012 at 07:55 AM 0
Share

crossfade is time-delayed method. you will not see changes immediately. try to make more pauses between switches and look for does it take effect

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by darksblood · Jul 17, 2012 at 08:16 PM

I just solved this, it seems I had some variables that I was changing and not setting back to normal for use by others. I also made jump stop animations and then cross-fade.

Comment
Add comment · Show 2 · 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 Pro-Odermonicon · May 21, 2017 at 11:58 PM 0
Share

Well Good Job! (^ ^)

avatar image RobAnthem Pro-Odermonicon · May 22, 2017 at 03:13 AM 0
Share

You just resurrected a 5 year old post to say "good job". Someone with greater moderator powers than I, please close this.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gravity - How to walk on ceiling 1 Answer

Why does my characterController code not apply gravity correctly? 0 Answers

How do I get my "dashing" animation to work? 1 Answer

Jump & Animation Script 2D 0 Answers

Adding a jump feature help? 2 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