- Home /
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");
}
}
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
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.
You just resurrected a 5 year old post to say "good job". Someone with greater moderator powers than I, please close this.
Your answer
Follow this Question
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