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 Shar1ngan · Feb 08, 2013 at 02:39 PM · animationmovementplayercharactercontrollerplatform

Animated platform and twitching player

Hello, i use 2d toolkit and i do 2d platformer.
I make platform by animation, he moves from left to right and next ping pong. I use Character Controller too. I find some script in answers.unity3d.com for player movement with the platform. But player is twitching if stay on platform...

Code for user controls:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControls : MonoBehaviour {
     
     public float runSpeed         = 1.5F;               
     public float Jump         = 9.0F;               
     public float Gravity         = 9.0F;
     public float JumpSpeed      = 9.0F;
     public int lifes = 3;
     
     public int moveDirection    = 0;               
 
     public AudioClip hitSound;  
     public AudioClip Hitmesound;                               
     public bool hit      = false; 
     public bool real_hit      = false; 
     public Vector3 move     = Vector3.zero; 
        public tk2dAnimatedSprite anim;
     private CharacterController controller;
     public int currframe = 1;
     private bool isrun=false;   
 
     //PLATFORM
     private Transform activePlatform;
     private Vector3 activeLocalPlatformPoint;
     private Vector3 activeGlobalPlatformPoint;
     private Vector3 lastPlatformVelocity;
     private Vector3 moveDistance;
     private Vector3 newGlobalPlatformPoint;
     //END PLATFORM
 
 
 
     void Start() {
         anim=GetComponent<tk2dAnimatedSprite>();
             anim.animationEventDelegate = AnimationEventDelegate;
         controller = GetComponent<CharacterController>(); 
     } 
 
 
     void Update() {
 
     
         if (controller.isGrounded) {               
             move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
             //right
             if(move.x > 0)
             {
                 move.x*=runSpeed;
                 moveDirection = 1;
                 if(!hit)
                     playAnim(anim,0);
             }
 
             //left
             if(move.x < 0)
             { 
                 moveDirection = 0; 
                 move.x*=runSpeed;
                 if(!hit)
                     playAnim(anim,1);
             }
 
             //stop
             if(move.x==0 && !hit)
             {
                 if(moveDirection==1)
                     anim.Play("Stop_r");
                 else
                     anim.Play("Stop_l");
             }
 
             //Hit
             if(Input.GetButtonDown ("Fire1"))
             {
                 hit=true;
                 real_hit=true;
                 PlaySound(hitSound,0);
                 if(moveDirection == 1)
                     playAnim(anim,6);
                 else
                     playAnim(anim,8);
                 
             }
 
             //Jump
             if (Input.GetButtonDown ("Jump")) 
             {
                 if (moveDirection==1)      
                     anim.Play("Jump");
                 else
                     anim.Play("Jump_left");
                 hit=false;
                 real_hit=false;
                 move.y=Jump; 
             }
 
             //Wait for last hit frame
             if(currframe==2 && hit==true)
             {
                 hit=false;
                 real_hit=false;
             }
 
             }
          else
          {
                 //Move in jump
                 move.x = Input.GetAxis ( "Horizontal" );
                 move.x*= JumpSpeed;
                 if (controller.collisionFlags == CollisionFlags.Above)
                     move.y = 0;                       
          }
 
 
            //PLATFORM 
         if (activePlatform != null) {
             newGlobalPlatformPoint= activePlatform.TransformPoint(activeLocalPlatformPoint);
             moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
             if (moveDistance != Vector3.zero)
                 controller.Move(moveDistance);
             lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
         }
         else {
             lastPlatformVelocity = Vector3.zero;
         }
         
         activePlatform = null;
         //END PLATFORM1
 
 
 
             move.y -= Gravity; 
         controller.Move(move * Time.deltaTime);
 
 
         //PLATFORM
         if (activePlatform != null) {
             activeGlobalPlatformPoint = transform.position;
             activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
         }
         //END PLATFORM
     }
     void  OnControllerColliderHit ( ControllerColliderHit hit  ){
         // Make sure we are really standing on a straight platform
         // Not on the underside of one and not falling down from it either!
         if (hit.moveDirection.y < -0.9f && hit.normal.y > 0.5f) {
             activePlatform = hit.collider.transform;    
         }
     }
 
     public void PlaySound (AudioClip soundName, float soundDelay) {
         audio.clip = soundName;
         audio.Play();
         audio.Play(44100);
     }
 
     void playAnim(tk2dAnimatedSprite theSprite, int theClip)
     {
       if (theSprite.clipId != theClip)
       {
          theSprite.Play(theClip);
       }
     }
 
      void AnimationEventDelegate(tk2dAnimatedSprite sprite, tk2dSpriteAnimationClip clip, tk2dSpriteAnimationFrame frame, int frameNum)
     {
         currframe=frameNum;
     }
 
 
 } 
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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

Moving Platform and CharacterController.Move not working. The player falls 3 Answers

How do you move a character using a slider? 1 Answer

How to move player after animation 2 Answers

How to unblock movement after animation? 0 Answers

Tweaking character movement 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