- Home /
              This post has been wikified, any user with enough reputation can edit it. 
            
 
            Soft turn with joystick
I have 2 scripts that are causing movement the first one gets the input the other one is a finance state machine. in player input the on screen joysticks are normalized, i used a bool to send a message,
   if(movement.x < 0){
   SendMessage("RotateMe", AdvancedMovement.Turn.left);
 }
and then the advanced movement script changes that into an action. I want to know how can i make it so that full left and right will make the player walk left and right and if you are a little off 89-0 then you turn while walking. 
(Input)
using UnityEngine; using System.Collections;
[RequireComponent( typeof( CharacterController ) )] public class CameraRelativeControl : MonoBehaviour {
     public Joystick moveJoystick;
     public Joystick rotateJoystick;
     public Transform cameraTransform;
     public Transform cameraPivot;
     public Vector2 rotationSpeed = new Vector2( 50, 25 );
     public float speed = 6.0f;
     public float jumpSpeed = 16.0f;
     public float inAirMultiplier = 0.25f;
     
     private Vector3 velocity;
     private Transform thisTransform;
     private CharacterController character;
     
     void Start ()
     {
             thisTransform = (Transform) GetComponent( typeof( Transform ) );
             character = (CharacterController) GetComponent( typeof( CharacterController ) );
     }
     
     void FaceMovementDirection()
     {
             Vector3 horizontalVelocity = character.velocity;
             horizontalVelocity.y = 0.0f;
             if( horizontalVelocity.magnitude > 0.1f )
             {
                     thisTransform.forward = horizontalVelocity.normalized;  
             }
     }
     
     void OnEndGame()
     {
             moveJoystick.Disable();
             rotateJoystick.Disable();
             this.enabled = false;
     }
     void Update ()
     {
             Vector3 movement = cameraTransform.TransformDirection( new Vector3( moveJoystick.position.x,
                                                                                                                                             0.0f,
                                                                                                                                             moveJoystick.position.y ) );
             movement.y = 0.0f;
             movement.Normalize();
             
             Vector2 absJoyPos = new Vector2( Mathf.Abs( moveJoystick.position.x ), 
                                                                                     Mathf.Abs( moveJoystick.position.y ) );
           //  movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
             
      if(movement.x > 0){
      SendMessage("RotateMe", AdvancedMovement.Turn.right);
          SendMessage("MoveMeForward", AdvancedMovement.Forward.forward);
     }//else{
             // SendMessage("MoveMeForward", AdvancedMovement.Forward.none);
         
    // }
     if(movement.x < 0){
   SendMessage("RotateMe", AdvancedMovement.Turn.left);
          SendMessage("MoveMeForward", AdvancedMovement.Forward.forward);
     }
    if(movement.z > 0){
     SendMessage("MoveMeForward", AdvancedMovement.Forward.forward);
    }
     if(movement.z < 0){
      print("backward");
     }
            
             
             Vector2 camRotation = rotateJoystick.position;
             
             camRotation.x *= rotationSpeed.x;
             camRotation.y *= rotationSpeed.y;
             camRotation *= Time.deltaTime;
             
             cameraPivot.Rotate( 0, camRotation.x, 0, Space.World );
             cameraPivot.Rotate( camRotation.y, 0, 0 );
     }
}
Movement (Finance)
     using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(CharacterController))]
 public class AdvancedMovement : MonoBehaviour {
     public enum State {
         Idle,
         Init,
         Setup,
         Run
     }
     
     public enum Turn {
         left = -1,
         none = 0,
         right = 1
     }
     public enum Forward {
         back = -1,
         none = 0,
         forward = 1
     }
     
     public string walkAnimName;
     public string runAnimName;
     public string jumpAnimName;
     public string idleAnimName;
     public string swimForwardAnimName;
     public string strafeAnimName;
     public string fallAnimName;
     public string swordAnimName;
     public string rangeAnimName;
     public string firstrunAnimName;
     public string backAnimName;
     public string pushAnimName;
     public string lootAnimName;
     
     public string combatidleAnimName;
     public string combatwalkAnimName;
     
     public string FireAnimName;
     public string HealAnimName;
     public string RoarAnimName;
     public string TimeAnimName;
     
     
     public float walkSpeed = 2;                    //the speed our character walks at
     public float runMultiplier = 2;                //how fast the player runs compared to the walk speed
     public float strafeSpeed = 2.5f;            //the speed our character strafes at
     public float rotateSpeed = 250;                //the speed our character turns at
     public float gravity = 20;                    //the setting for gravity
     public float airTime = 0;                    //how long have we been in the air since the last time we touched the ground
     public float fallTime = .5f;                //the length of time we have to be falling before the system knows its a fall
     public float jumpHeight = 8;                //the height we move when we are jumping
     public float jumpTime = 1.5f;
 
     private CollisionFlags _collisionFlags;        //the collisionFlags we have from the last frame.
     private Vector3 _moveDirection;                //This is the direction our character is moving
     private Transform _myTransform;                //our cached transform
     private CharacterController _controller;    //our cached CharacterController
     
     private Turn _turn;
     public Forward _forward;
     private Turn _strafe;
     public bool _run;
     private bool _jump;
     private bool _isSwimming;
     public bool _isAttacking;
     public bool _MeleeCombat;
     public bool _RangeCombat;
     public bool _MagicCombat;
     public bool _Combat;
     public bool _no_Combat;
     public bool _Pushed;
     
     public bool _Looting;
     
     public bool _usingFire;
     public bool _usingRoar;
     public bool _usingHeal;
     public bool _usingTime;
     
     public bool freetoattack;
     
     public float arrowlenght;
     public float swordlenght;
     
     public float pushlenght;
     
     public float whenpushlenght;
     
     public float lootlenght;
     
     public float Firelenght;
     public float Heallenght;
     public float Roarlenght;
     public float Timelenght;
     
     
     
     private State _state;
     
 
     //Called before the script starts
     public void Awake() {
         _myTransform = transform;                                //cache our transform
         _controller = GetComponent<CharacterController>();        //cache our charactercontroller
         _state = AdvancedMovement.State.Init;
     }
     
         private void OnEnable() {
         Messenger.AddListener("Player Move Back", Pushed);
         Messenger.AddListener("Looting", Looting);
     }
     private void OnDisable() {
         Messenger.RemoveListener("Player Move Back",Pushed);
         Messenger.RemoveListener("Looting",Looting);    
         
     }
 
     // Use this for initialization
     IEnumerator Start () {
         firstrunAnimName = runAnimName;
         while(true) {
             switch(_state) {
             case State.Init:
                 Init();
                 break;
             case State.Setup:
                 SetUp();
                 break;
             case State.Run:
                 ActionPicker();
                 break;
             }
 
             yield return null;
         }
     }
     
     private void Init() {
         if(!GetComponent<CharacterController>()) return;
         if(!GetComponent<Animation>()) return;
         
         _state = AdvancedMovement.State.Setup;
     }
     
     private void SetUp() {
         _moveDirection = Vector3.zero;                            //zero our the vector3 we will use for moving our player
         animation.Stop();                                        //stop any animations that might be set to play automatically
         animation.wrapMode = WrapMode.Loop;                        //set all animations to loop by default
         
         if(jumpAnimName != "") {
             animation[jumpAnimName].layer = -1;                        //move jump to a higher layer
             animation[jumpAnimName].wrapMode = WrapMode.Once;
             //animation[pushAnimName].wrapMode = WrapMode.PingPong;
         }
 
         animation.Play(idleAnimName);                            //start the idle animation when the script starts
         
         
         _turn = AdvancedMovement.Turn.none;
         _forward = AdvancedMovement.Forward.none;
         _strafe = Turn.none;
         _run = true;
         _jump = false;
         _isSwimming = false;
         _isAttacking = false;
         _Looting = false;
         
         
         _state = AdvancedMovement.State.Run;
     }
     
     private void ActionPicker() {
         
         //allow the player to turn left and right
         _myTransform.Rotate(0, (int)_turn * Time.deltaTime * rotateSpeed, 0);
 
         
         //if we are on the ground, let us move
         if(_controller.isGrounded || _isSwimming) {
             //reset the air timer if we are on the ground
             airTime = 0;
             
             //get the user input if we should be moving forward or sideways
             //we will calculate a new vector3 for where the player needs to be
             _moveDirection = new Vector3((int)_strafe, 0, (int)_forward);
             _moveDirection = _myTransform.TransformDirection(_moveDirection).normalized;
             _moveDirection *= walkSpeed;
             
                 if(_forward <0){
                 runAnimName = "back";
             }else{
                 runAnimName = "run";
             }
             
             
             
                 
             
             if(_forward != Forward.none) {//if user is pressing forward
                 if(_isSwimming) {
                     Swim();
                 }
                 else {
                     if(_run) {                                    //and pressing the run key
                         _moveDirection *= runMultiplier;        //move user at run speed
                         Run();                                    //play run animation
                     }
                     else {
                         Walk();                                    //play walk animation
                     }
                 }
             }
             else if(_strafe != AdvancedMovement.Turn.none) {    //if user is pressing strafe
                 Strafe();//play strafe animation
             }
             else {
                 if(_isSwimming) {
                 }
                 else {
                     Idle();                                        //play idle animation
                 }
             }
             
             if(_jump) {                                        //if the user presses the jump key
                 if(airTime < jumpTime) {                    //if we have not already been in the air to long
                     _moveDirection.y += jumpHeight;            //move them upwards
                     Jump();                                    //play jump animation
                     _jump = false;
                 }
             }
             
         }
         else {
             //if we have a collisionFlag and it is CollideBelow
             if((_collisionFlags & CollisionFlags.CollidedBelow) == 0) {
                 airTime += Time.deltaTime;                    //increase the airTime
                 
                 if(airTime > fallTime) {                    //if we have been in the air to long
                     Fall();                                    //play the fall animation
                 }
             }
         }
         
         if(!_isSwimming){
             _moveDirection.y -= gravity * Time.deltaTime;        //apply gravity
         
         //move the character and store any new CollisionFlags we get
         _collisionFlags = _controller.Move(_moveDirection * Time.deltaTime);
         }
         if (_MeleeCombat == true){
             meleeAttack();
         
         
         
         }
     if (_isAttacking == false){
             freetoattack = true;
             swordlenght = animation[swordAnimName].length;
             arrowlenght = animation[rangeAnimName].length;
             
             lootlenght = animation[lootAnimName].length;
             Firelenght = animation[FireAnimName].length - .05f;
             Heallenght = animation[HealAnimName].length;
             Roarlenght = animation[RoarAnimName].length - 2;
             
             pushlenght = animation[pushAnimName].length - 1.1f;
         //    Timelenght = animation[TimeAnimName].length;
         }
         if (_RangeCombat == true)
             RangeAttack();
         
         //if (_MagicCombat == true)
         //    magicAttack();
         
     //    Attacksystem th = gameObject.GetComponent<Attacksystem>();
     //     th.canAttack = freetoattack;
         
         if (_usingFire == true)
         FireAttack();
         
         if (_usingHeal == true)
         HealAttack();
         
         if (_usingRoar == true)
         RoarAttack();
         
         if (_usingTime== true)
         TimeAttack();
         
         if(_Looting == true)
             LootAnim();
         
         
             if (whenpushlenght == 0){
                 _Pushed = true;
                 if ( _Pushed == true){
                     if(Vector3.Distance(transform.position, transform.position) <= 8){
             transform.position -= transform.forward * 5 * Time.deltaTime;
             }
                 Pushing();
                 freetoattack = false;
             }
         }
         
         if (_Combat == true){
     runAnimName = combatwalkAnimName;
     idleAnimName = combatidleAnimName;
         }
         
     }
 
     public void MoveMeForward(Forward z) {
         _forward = z;
     }
     
     public void Attacking() {
         _isAttacking = true;
         freetoattack = false;
         
     }
     public void Looting(){
         _Looting = true;
     }
     
     public void Pushed() {
         whenpushlenght --;
         
         
         
     }
     
     public void usingFire (){
         if (_isAttacking == false)
         _usingFire = true;
     }
     public void usingHeal (){
         if (_isAttacking == false)
         _usingHeal = true;
     }
     public void usingRoar (){
         if (_isAttacking == false)
         _usingRoar = true;
     }
     public void usingTime (){
         if (_isAttacking == false)
         _usingTime = true;
     }
     
     
     public void MeleeCombat (){
         //_MeleeCombat = true
         if (_MagicCombat == false)
         _MeleeCombat = true;
     }
     
     
     //public void MagicCombat (){
         //_MeleeCombat = true
         
     ///    if (_isAttacking == false)
     //        _MagicCombat = true;
     //}
     
     public void RangeCombat (){
         //_MeleeCombat = true
     if (_MagicCombat == false)
         _RangeCombat = true;
     }
     
     public void ToggleRun() {
         _run = !_run;
     }
     
     public void RotateMe(Turn y) {
         _turn = y;
     }
     
     public void Strafe(Turn x) {
         _strafe = x;
     }
     
     public void JumpUp() {
         _jump = true;
     }
     
     public void IsSwimming(bool swim) {
         _isSwimming = swim;
     }
     
     
     
 /**
  * Below is a list of all of the animations that every character in the game can perform along with any parameters needed for them to work right
 **/
     //Idle animation
     public void Combat(){
     _Combat = true;    
     }
     
     
     public void no_Combat(){
         _Combat = false;
         runAnimName = firstrunAnimName;
         idleAnimName = "idle1";
         
     }
     
     public void Combat_Animations(){
     //walkAnimName = combatwalkAnimName;
     //idleAnimName = combatidleAnimName;
         
     }
     public void LootAnim(){
         _isAttacking = true;
         if(lootAnimName == "")
             return;
         animation[ lootAnimName].speed = animation [lootAnimName].length;
         animation.CrossFade(lootAnimName);
         if (lootlenght > 0)
             lootlenght -= Time.deltaTime;
         if (lootlenght < 0)
             lootlenght = 0;
         
             if (lootlenght == 0){
             _Looting = false;
             _isAttacking = false;
         }
     }
     public void    Pushing(){
         _isAttacking = true;
             if(pushAnimName == "")
             return;
         animation[ pushAnimName].speed = animation [pushAnimName].length;
         animation.CrossFade(pushAnimName, .3f);
         if (pushlenght > 0)
             pushlenght -= Time.deltaTime;
         if (pushlenght < 0)
             pushlenght = 0;
         
             if (pushlenght == 0){
             _Pushed = false;
             _isAttacking = false;
             whenpushlenght = 2;
             
         
             
             
         }
     }
         
     public void    FireAttack(){
         _isAttacking = true;
             if(FireAnimName == "")
             return;
         animation[ FireAnimName].speed = animation [FireAnimName].length * 2;
         animation.CrossFade(FireAnimName);
         if (Firelenght > 0)
             Firelenght -= Time.deltaTime;
         if (Firelenght < 0)
             Firelenght = 0;
         
             if (Firelenght == 0){
             _isAttacking = false;
             _usingFire = false;
             
         }
         
     }
     
         public void    HealAttack(){
         _isAttacking = true;
             if(HealAnimName == "")
             return;
         animation[ HealAnimName].speed = animation [HealAnimName].length / 1f;
         animation.CrossFade(HealAnimName);
         if (Heallenght > 0)
             Heallenght -= Time.deltaTime;
         if (Heallenght < 0)
             Heallenght = 0;
         
             if (Heallenght == 0){
             _isAttacking = false;
             _usingHeal = false;
             
         }
         
     }
         public void    RoarAttack(){
             _isAttacking = true;
             if(RoarAnimName == "")
             return;
         animation[ RoarAnimName].speed = animation [RoarAnimName].length / 1f;
         animation.CrossFade(RoarAnimName);
         if (Roarlenght > 0)
             Roarlenght -= Time.deltaTime;
         if (Roarlenght < 0)
             Roarlenght = 0;
         
             if (Roarlenght == 0){
             _isAttacking = false;
             _usingRoar = false;
             
         }
         
     }
         public void    TimeAttack(){
             _isAttacking = true;
             if(TimeAnimName == "")
             return;
         animation[ TimeAnimName].speed = animation [TimeAnimName].length /1f;
         animation.CrossFade(TimeAnimName);
         if (Timelenght > 0)
             Timelenght -= Time.deltaTime;
         if (Timelenght < 0)
             Timelenght = 0;
         
             if (Timelenght == 0){
             _isAttacking = false;
             _usingTime = false;
             
         }
         
     }
     
     public void    meleeAttack(){
         if(swordAnimName == "")
             return;
         animation[ swordAnimName].speed = animation [swordAnimName].length / 1f;
         animation.CrossFade(swordAnimName);
         if (swordlenght > 0)
             swordlenght -= Time.deltaTime;
         if (swordlenght < 0)
             swordlenght = 0;
         
             if (swordlenght == 0){
             _isAttacking = false;
             _MeleeCombat = false;
             
         }
     }
         
         
     public void    RangeAttack(){
         if(rangeAnimName == "")
             return;
         animation[ rangeAnimName].speed = animation [rangeAnimName].length /1f;
         animation.CrossFade(rangeAnimName);
         if (arrowlenght > 0)
             arrowlenght -= Time.deltaTime;
         if (arrowlenght < 0)
             arrowlenght = 0;
         
             if (arrowlenght == 0){
             _isAttacking = false;
             _RangeCombat = false;
         }
     
         
     }
     public void Idle() {
         if(idleAnimName == "")
             return;
 
         animation.CrossFade(idleAnimName);
     }
     
     //walk animation
     public void Walk() {
         if(walkAnimName == "")
             return;
         
         animation.CrossFade(walkAnimName);
     }
     
     //run animation
     public void Run() {
         if(runAnimName == "run"){
             animation[runAnimName].speed = 1.5f;
         animation.CrossFade(runAnimName);
     
             return;
         }
         
         if(runAnimName == "back"){
         animation[backAnimName].speed = 2f;
         animation.CrossFade(backAnimName);
             return;
         }
 
     }
     
     //strafe animation
     public void Strafe() {
         if(strafeAnimName == "")
             return;
 
         animation.CrossFade(strafeAnimName);
     }
     
     //jump animation
     public void Jump() {
         if(jumpAnimName == "")
             return;
         
         animation.CrossFade(jumpAnimName);
     }
     
     //fall animation
     public void Fall() {
         if(fallAnimName == "")
             return;
 
         animation.CrossFade(fallAnimName);
     }
     
     public void Swim() {
         if(swimForwardAnimName == "")
             return;
 
         animation.CrossFade(swimForwardAnimName);
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                