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
-1
Question by DoubleS · Mar 28, 2013 at 11:21 AM · charactercontroller

My Character Doesn't Move Properly

I started with ThirdPersonController.js, which comes with unity for my character controller script, but now I've got stucked. My character doesn't move forward when i pressed only one direction(like only 'w' or 's'). He's always moves slightly left, but looks directly forward. I've edited ThirdPersonController, so i can't compare it with mine, though i compared UpdateSmoothedMovementDirection() and couldn't see any problem. And character sometimes stops moving for a moment while running and jumping.

 #pragma strict
 #pragma downcast
 
 @script RequireComponent(CharacterController)
 
 var idleAnimation : AnimationClip;
 var walkAnimation : AnimationClip;
 var runAnimation : AnimationClip;
 var sprintAnimation : AnimationClip;
 var jumpAnimation : AnimationClip;
 var runJumpAnimation : AnimationClip;
 
 var idleAnimationSpeed = 0.5;
 var walkMaxAnimationSpeed = 0.75;
 var trotMaxAnimationSpeed = 1.0;
 var runMaxAnimationSpeed = 1.0;
 var sprintMaxAnimationSpeed = 1.0;
 var jumpAnimationSpeed = 1.15;
 var landAnimationSpeed = 1.0;
 var runJumpAnimationSpeed = 1.0;
 
 //Combo Animations
 var comboA1 : AnimationClip;
 var comboA2 : AnimationClip;
 var comboA3 : AnimationClip;
 var comboA4 : AnimationClip;
 
 
 private var _animation : Animation;
 
 enum CharacterState {
     Idle = 0,
     Walking = 1,
     Trotting = 2,
     Running = 3,
     Jumping = 4,
     Sprinting = 5,
     RunJumping = 6,
     ComboA1 = 7,
     ComboA2 = 8,
     ComboA3 = 9,
     ComboA4 = 10,
     Hit = 11,
     Unsheathe = 12,
     Sheathe = 13,
 }
 
 var _characterState : CharacterState;
 
 private var moveSpeed = 0.0;
 private var walkSpeed = 5.0;
 private var trotSpeed = 7.0;
 private var trotAfterSeconds = 3.0;
 private var speedSmoothing = 10.0;
 private var verticalSpeed = 0.0;
 private var runSpeed = 25.0;
 private var sprintSpeed = 50.0;
 
 private var lockCameraTimer = 0.0;
 
 private var inAirControlAcceleration = 3.0;
 
 private var collisionFlags : CollisionFlags; 
 
 private var moveDirection = Vector3.zero;
 
 private var jumpHeight = 3.0;
 private var inAirAcceleration = 3.0;
 private var gravity = 20.0;
 private var canJump = true;
 private var canRunJump = false;
 private var jumpRepeatTime = 0.05;
 private var jumpTimeout = 0.15;
 private var groundedTimeout = 0.25;
 
 private var jumping = false;
 private var jumpingReachedApex = false;
 private var lastJumpButtonTime = -10.0;
 private var lastJumpTime = -1.0;
 private var lastJumpStartHeight = 0.0;
 private var movingBack = false;
 private var isMoving = false;
 private var walkTimeStart = 0.0;
 private var inAirVelocity = Vector3.zero;
 private var lastGroundedTime = 0.0;
 private var isControllable = true;
 
 //Combo Controller
 private var lastComboTime = 29.0;
 private var comboBreakTime = 30.0;
 private var comboASpeed = 1.0;
 private var comboAForwardSpeed = 0.2;
 private var attackKeyUp = false;
 private var comboCount = 0;
 private var moveCombo = false;
 private var canCombo = true;
 private var moveForward = false;
 private var canWalk = true;
 
 //Silah
 var WeaponControllerScript : WeaponController;
 
 //Clothing
 var ClothingControllerScript : ClothingController;
 
 //Düşmanlara gözüken hedef
 var target : Transform;
 
 //Dayak yeme
 var hitAnimation : AnimationClip;
 private var hitAnimationSpeed = 1.0;
 
 var currentChar : Transform;
 
 var inBattle = false;
 
 var battlePosition : AnimationClip;
 private var battlePositionSpeed = 1.0;
 
 var swinging = false;
 
 private var lastComboCount = 0;
 
 //Ready or Sheathe?
 private var RoS = false;
 
 private var damageTaken = false;
 
 //Unsheathe Animation
 var unsheatheAnimation : AnimationClip;
 private var unsheatheAnimationSpeed = 1.0;
 private var rightArm : Transform;
 
 //Shathe Animation
 var sheatheAnimation : AnimationClip;
 private var sheatheAnimationSpeed = 1.0;
 
 var noBattleCondition = true;
 
 
 function Awake(){
 
     moveDirection = transform.TransformDirection(Vector3.forward);
     
     _animation = GetComponent(Animation);
     
     }
 
 function Start () {
     
     WeaponControllerScript = gameObject.GetComponent(WeaponController);
     WeaponControllerScript.WhichWeapon("Sword4", "PlayerWeapon");
     WeaponControllerScript.ReadyOrSheathe("Sword4", false);
     
     ClothingControllerScript = gameObject.GetComponent(ClothingController);
     ClothingControllerScript.WhichTop("Top2", "Clothing");
     
     animation[hitAnimation.name].layer = 1;
     
     rightArm = transform.Find("Armature/pelvis/hips/spine/ribs/ribs_001/shoulder_R/upper_arm_R");
     
     animation[unsheatheAnimation.name].layer = 2;
     animation[sheatheAnimation.name].layer = 2;
     
 }
 
 function UpdateSmoothedMovementDirection(){
     
     var cameraTransform = Camera.main.transform;
     var grounded = IsGrounded();
         
     //Kameraya göre ileri
     var forward = cameraTransform.TransformDirection(Vector3.forward);
     forward.y = 0;
     forward = forward.normalized;
         
     var right = Vector3(forward.z, 0, -forward.x);
         
     
     var v = Input.GetAxis("Vertical");
     var h = Input.GetAxis("Horizontal");
         
     if(v < -0.2)
             movingBack = true;
     else
             movingBack = false;
             
     var wasMoving = isMoving;
     isMoving = Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1;
             
     var targetDirection = h * right + v * forward;
         
     if(grounded){
         lockCameraTimer += Time.deltaTime;
         if(isMoving != wasMoving)
             lockCameraTimer = 0.0;
             
         if(targetDirection != Vector3.zero){
             if(moveSpeed < walkSpeed * 0.9 && grounded)
                     moveDirection = targetDirection.normalized;
                     
             else{
                 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 360, 1000);
                     
                 moveDirection = moveDirection.normalized;
             }
         }
         
         var curSmooth = speedSmoothing * Time.deltaTime;
             
         var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
             
         if(Input.GetButton("Fire1")){
             targetSpeed *= sprintSpeed;
             canRunJump = true;
             _characterState = CharacterState.Sprinting;
                     
         }
                 
         else if(Time.time - trotAfterSeconds > walkTimeStart){
             targetSpeed *= trotSpeed;
             canRunJump = false;
             _characterState = CharacterState.Trotting;
         }
                 
         else if(Mathf.Abs(v) + Mathf.Abs(h) < 0.4){
             targetSpeed *= walkSpeed;
             canRunJump = false;
             _characterState = CharacterState.Walking;
         }
                     
         else{
             targetSpeed *= runSpeed;
             _characterState = CharacterState.Running;
             canRunJump = false;
         }
             
         moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
             
         if(moveSpeed < walkSpeed * 0.3);
             walkTimeStart = Time.time;
             
             
             
         }
             
         else{
             if(jumping)
                 lockCameraTimer = 0.0;
                 
             else if(isMoving)
                 inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
     }        
 }
     
 function ApplyJumping(){
 
     if(lastJumpTime + jumpRepeatTime > Time.time)
         return;
     
     if(IsGrounded()){
         if(canJump && Time.time < lastJumpButtonTime + jumpTimeout){
             verticalSpeed = CalculateJumpVerticalSpeed(jumpHeight);
             SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
         }
     }
 }
 
 function ApplyGravity(){
     if(isControllable){
         var jumpButton = Input.GetButton("Jump");
         
         if(jumping && !jumpingReachedApex && verticalSpeed <= 0.0){
             jumpingReachedApex = true;
             canRunJump = false;
             SendMessage("DidJumpReachApex", SendMessageOptions.DontRequireReceiver);
     }
         
         if(IsGrounded()){
             verticalSpeed = 0.0;
             target.localPosition.y = 0.0;
         }
             
         else
             verticalSpeed -= gravity * Time.deltaTime;
     }
 }
 
 function CalculateJumpVerticalSpeed(targetJumpHeight : float){
     return Mathf.Sqrt(2 * targetJumpHeight * gravity);
 }
 
 function DidJump(){
     jumping = true;
     jumpingReachedApex = false;
     lastJumpTime = Time.time;
     lastJumpStartHeight = transform.position.y;
     lastJumpButtonTime = -10;
     
     _characterState = CharacterState.Jumping;
 }
 
 function Combo(){
 
     if(canCombo){
     
         var hit : RaycastHit;
         
         attackKeyUp = true;
         canWalk = false;
         moveForward = true;
         if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit)){    
             if(hit.collider.tag == "Enemy" && hit.distance <= 3.0){
                 moveForward = false;
                 Debug.Log(hit.distance);
             }
         }
         SpeedZero();
         RandomCombo();
         lastComboTime = 0;
         canCombo = false;
         
         if(!RoS && noBattleCondition){
             _characterState = CharacterState.ComboA1;
             WeaponControllerScript.ReadyOrSheathe("Sword4", true);
             RoS = true;
         }
         else if(comboCount == 2){
             _characterState = CharacterState.ComboA2;
             lastComboCount = 2;
         }
         else if(comboCount == 3){
             _characterState = CharacterState.ComboA3;
             lastComboCount = 3;
         }
         else if(comboCount == 4){
             _characterState = CharacterState.ComboA4;
             lastComboCount = 4;
         }
     }
 }
 
 function RandomCombo(){
     comboCount = Random.Range(1, 5);
     if(comboCount == lastComboCount){
         if(comboCount == 4){
             comboCount -= 1;
         }
         else{
             comboCount += 1;
         }
     }
 }
 
 /*function AttackDirection(){
 
     var cameraTransform = Camera.main.transform;
     
     var forward = cameraTransform.TransformDirection(Vector3.forward);
     forward.y = 0;
     forward = forward.normalized;
     var right = Vector3(forward.z, 0, -forward.x);
     
     var direction = Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward;
     moveDirection = Vector3.RotateTowards(moveDirection, direction, 360, 1000);
     moveDirection = moveDirection.normalized;
     
 }*/
 
 function ComboBreaker(){
     
     animation.Stop();
     canWalk = true;
     SpeedNormalized();
     lastComboTime = 0.0;
     attackKeyUp = false;
     canCombo = true;
     
     if(noBattleCondition){
         SS("Sheathe");
     }
     
 }
 
 function SpeedZero(){
     moveSpeed = 0.0;
     walkSpeed = 0.0;
     trotSpeed = 0.0;
     trotAfterSeconds = 0.0;
     speedSmoothing = 0.0;
     verticalSpeed = 0.0;
     runSpeed = 0.0;
     sprintSpeed = 0.0;
 }
 
 function SpeedNormalized(){
 
     moveSpeed = 0.0;
     walkSpeed = 5.0;
     trotSpeed = 7.0;
     trotAfterSeconds = 3.0;
     speedSmoothing = 10.0;
     verticalSpeed = 0.0;
     runSpeed = 25.0;
     sprintSpeed = 50.0;
 }
     
 
 function Animations(){
 
     if(animation.IsPlaying(runJumpAnimation.name) || animation.IsPlaying(jumpAnimation.name) || animation.IsPlaying(hitAnimation.name)){
         canCombo = false;
     }
     
     if(!animation.IsPlaying(runJumpAnimation.name) && !animation.IsPlaying(jumpAnimation.name) && !animation.IsPlaying(hitAnimation.name)){
         canCombo = true;
     }
     
     if(animation.IsPlaying(comboA1.name) || animation.IsPlaying(comboA2.name) || animation.IsPlaying(comboA3.name) || animation.IsPlaying(comboA4.name)){
         swinging = true;
     }
     
     if(!animation.IsPlaying(comboA1.name) && !animation.IsPlaying(comboA2.name) && !animation.IsPlaying(comboA3.name) && !animation.IsPlaying(comboA4.name)){
         swinging = false;
     }
 }
 
 function Hit(){
 
     if(!damageTaken){
         ComboBreaker();
         
         _characterState = CharacterState.Hit;
         currentChar.Translate(Vector3.forward * Time.deltaTime * -50);
 
         damageTaken = true;
         DamageCount();
     }
     
     
 }
 
 function DamageCount(){
     yield WaitForSeconds(0.7);
     damageTaken = false;
     _characterState = CharacterState.Idle;
 }
 
 //Unsheathe, sheathe
 function SS(state : String){
     if(state == "Unsheathe"){
         WeaponControllerScript.ReadyOrSheathe("Sword4", true);
         _animation[unsheatheAnimation.name].AddMixingTransform(rightArm);
         _animation[unsheatheAnimation.name].speed = unsheatheAnimationSpeed;
         _animation[unsheatheAnimation.name].wrapMode = WrapMode.Once;
         _animation.Play(unsheatheAnimation.name);
         RoS = true;
     }
     
     if(state == "Sheathe"){
         WeaponControllerScript.ReadyOrSheathe("Sword4", false);
         _animation[sheatheAnimation.name].AddMixingTransform(rightArm);
         _animation[sheatheAnimation.name].speed = sheatheAnimationSpeed;
         _animation[sheatheAnimation.name].wrapMode = WrapMode.Once;
         _animation.Play(sheatheAnimation.name);
         RoS = false;
     }
 }
         
 
 function Update () {
     
     if(Input.GetButtonDown("Jump")){
         lastJumpButtonTime = Time.time;
     }
     
     UpdateSmoothedMovementDirection();
     
     ApplyGravity();
     
     ApplyJumping();
     
     Animations();
         
     if(attackKeyUp){
         lastComboTime += 1;
     }
     
     if(lastComboTime > comboBreakTime){
         ComboBreaker();
     }
     
     if(lastComboTime > 15){
         moveForward = false;
     }
     
     if(comboCount >= 5){
         comboCount = 1;
     }
     
     if(moveForward){
             transform.Translate(Vector3.forward * Time.deltaTime * 5);
         }
         
     if(Input.GetButtonDown("Attack")){
         Combo();
     }
     
     var movement = moveDirection * moveSpeed + Vector3(0, verticalSpeed, 0) + inAirVelocity;
     movement *= Time.deltaTime;
     
     if(inBattle && !RoS && !noBattleCondition){
         SS("Unsheathe");
     }
     
     if(!inBattle && RoS && !noBattleCondition){
         SS("Sheathe");
     }
     
     if(lastComboTime > 20){
         canCombo = true;
     }
     
         
     
     var controller : CharacterController = GetComponent(CharacterController);
     collisionFlags = controller.Move(movement);
     
     if(_animation){
         
         if(_characterState == CharacterState.Hit){
                 _animation[hitAnimation.name].speed = hitAnimationSpeed;
                 _animation[hitAnimation.name].wrapMode = WrapMode.Once;
                 _animation.CrossFade(hitAnimation.name);
             }
             
         else if(_characterState == CharacterState.Jumping){
             if(!jumpingReachedApex){
                 if(canRunJump){
                     _animation[runJumpAnimation.name].speed = runJumpAnimationSpeed;
                     _animation[runJumpAnimation.name].wrapMode = WrapMode.ClampForever;
                     _animation.CrossFade(runJumpAnimation.name);
                 }
                 else{
                     _animation[jumpAnimation.name].speed = jumpAnimationSpeed;
                     _animation[jumpAnimation.name].wrapMode = WrapMode.ClampForever;
                     _animation.CrossFade(jumpAnimation.name);
                 }
             }
             else{
                 _animation[jumpAnimation.name].speed = -landAnimationSpeed;
                 _animation[jumpAnimation.name].wrapMode = WrapMode.ClampForever;
                 _animation.CrossFade(jumpAnimation.name);
             }
         }
         
         else{
         
             if(_characterState == CharacterState.ComboA1){
                 _animation[comboA1.name].speed = comboASpeed;
                 _animation[comboA1.name].wrapMode = WrapMode.Once;
                 _animation.Play(comboA1.name);
             }
             else if(_characterState == CharacterState.ComboA2){
                 _animation[comboA2.name].speed = comboASpeed;
                 _animation[comboA2.name].wrapMode = WrapMode.Once;
                 _animation.CrossFade(comboA2.name);
             }
             else if(_characterState == CharacterState.ComboA3){
                 _animation[comboA3.name].speed = comboASpeed;
                 _animation[comboA3.name].wrapMode = WrapMode.Once;
                 _animation.CrossFade(comboA3.name);
             }
             else if(_characterState == CharacterState.ComboA4){
                 _animation[comboA4.name].speed = comboASpeed;
                 _animation[comboA4.name].wrapMode = WrapMode.Once;
                 _animation.CrossFade(comboA4.name);
             }
             
             else if(canWalk){
             
                 if(controller.velocity.sqrMagnitude < 0.1 && inBattle){
                     _animation[battlePosition.name].speed = battlePositionSpeed;
                     _animation.CrossFade(battlePosition.name);
                 }
             
                 else if(controller.velocity.sqrMagnitude < 0.1 && !inBattle){
                     _animation[idleAnimation.name].speed = idleAnimationSpeed;
                     _animation.CrossFade(idleAnimation.name);
                 }
                 
                 else if(_characterState == CharacterState.Running){
                     _animation[runAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, runMaxAnimationSpeed);
                     _animation.CrossFade(runAnimation.name);
                 }
                 else if(_characterState == CharacterState.Sprinting){
                     _animation[sprintAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, sprintMaxAnimationSpeed);
                     _animation.CrossFade(sprintAnimation.name);
                 }
                 else if(_characterState == CharacterState.Trotting){
                     _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, trotMaxAnimationSpeed);
                     _animation.CrossFade(walkAnimation.name);
                 }
                 else if(_characterState == CharacterState.Walking){
                     _animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, walkMaxAnimationSpeed);
                     _animation.CrossFade(walkAnimation.name);
                 }
             }
         }
                 
             
     }
         
         
     
     if(IsGrounded()){
         transform.rotation = Quaternion.LookRotation(moveDirection);
     }
     
     else{
         var xzMove = movement;
         xzMove.y = 0;
         if(xzMove.sqrMagnitude > 0.001){
             transform.rotation = Quaternion.LookRotation(xzMove);
         }
     }
     
     if(IsGrounded()){
         lastGroundedTime = Time.time;
         inAirVelocity = Vector3.zero;
         if(jumping){
             jumping = false;
             SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
         }
     }
 }
 
 
 function OnControllerColliderHit(hit : ControllerColliderHit){
 
     if(hit.moveDirection.y > 0.01)
         return;
 }
 
 function GetSpeed(){
     return moveSpeed;
 }
 
 function IsJumping(){
     return jumping;
 }
 
 function IsGrounded(){
     return(collisionFlags & CollisionFlags.CollidedBelow) != 0;
 }
 
 function GetDirection(){
     return moveDirection;
 }
 
 function IsMovingBackwards(){
     return movingBack;
 }
 
 function GetLockCameraTimer(){
     return lockCameraTimer;
 }
 
 function IsMoving() : boolean{
     return Mathf.Abs(Input.GetAxisRaw("Vertical")) +     Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5;
 }
 
 function HasJumpReachedApex(){
     return jumpingReachedApex;
 }
 
 function IsGroundedWithTimeout(){
     return lastGroundedTime + groundedTimeout > Time.time;
 }
 
 function Reset(){
     gameObject.tag = "Player";
 }
Comment
Add comment · Show 2
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 DoubleS · Mar 30, 2013 at 10:41 AM 0
Share

Interesting, there is nothing wrong with code, when i play with character controllers center(y axis) it walks correctly. But he's feet enters to the terrain which is bad. This is started after i updated my character asset.

avatar image DoubleS · Apr 01, 2013 at 02:36 PM 0
Share

I reverted to the prefab and it worked well.

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

10 People are following this question.

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

Related Questions

Controlled sphere/ Ball game 2 Answers

Character Controller can pass through Collider 1 Answer

Character Controller, changing collider's size? 0 Answers

Character movement relative to both camera and transform 2 Answers

Player Animation and control panel 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