- Home /
This post has been wikified, any user with enough reputation can edit it.
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