Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

need some help to change a player script

Discussion in 'Scripting' started by grka, Sep 1, 2015.

  1. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    I'm using an asset which has a locomotion script to move the player. Unfortunately this script works the way that the player is walking in the direction where the mouse is pointing. I would like to change it so that I can use it without the mouse. The left and right arrow keys shall give the direction instead. But for some reason I don't manage that.

    This is the original script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3.  
    4. [RequireComponent(typeof(Animator))]
    5.  
    6. //Name of class must be name of file as well
    7.  
    8. public class LocomotionPlayer : MonoBehaviour
    9. {
    10.  
    11.     private Transform mountPoint;
    12.     private Vector3 moveDirection = Vector3.zero;//player's move direction
    13.     protected Animator _animator;
    14.     private CharacterController _charCtrl;
    15.  
    16.     private bool _jump;//jump button press detection
    17.  
    18.     public bool _run;
    19.     public bool canRotate = true;
    20.     public bool holding;
    21.     public bool alive = true;
    22.  
    23.     public float _speed;
    24.     public float _strafe;
    25.     public float gravity;//gravity force
    26.     public float health;
    27.  
    28.     private float _mouseX;
    29.     private float tapTimeWindow;
    30.  
    31.     public int tapCount;
    32.  
    33.     // Use this for initialization
    34.     void Start ()
    35.     {
    36.         _animator = GetComponent<Animator>();
    37.         _charCtrl = GetComponent<CharacterController>();
    38.         m_AudioSource = GetComponent<AudioSource>();
    39.     }
    40.  
    41.     void Update ()
    42.     {
    43.         _mouseX = Input.GetAxis("Mouse X");
    44.         _speed = Input.GetAxis("Vertical");//reading vertical axis input
    45.         _strafe = Input.GetAxis("Horizontal");//reading horizontal axis input
    46.         _run = Input.GetKey(KeyCode.LeftShift) ? true : false;//check if run button was pressed
    47.         //PROCESSING ROTATION
    48.         Vector3 aimPoint =  Camera.main.transform.forward * 10f;
    49.         if (canRotate)
    50.         {
    51.             Quaternion targetRotation = Quaternion.LookRotation(aimPoint);
    52.             this.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 4 * Time.deltaTime);
    53.             this.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
    54.         }
    55.  
    56.  
    57.      
    58.         if ((_charCtrl != null) && (!swimming))
    59.         { //is character controller enabled
    60.             if (_charCtrl.isGrounded)
    61.             {
    62.                 moveDirection.y = 0;
    63.             }
    64.  
    65.         }
    66.      
    67.     }
    68.     void FixedUpdate ()
    69.     {
    70.         _animator.SetFloat("mouseX", _mouseX, 0.3f, Time.deltaTime);
    71.         _animator.SetFloat("Speed", _speed);
    72.         _animator.SetFloat("Strafe", _strafe);
    73.         _animator.SetBool("Run", _run);
    74.         _animator.SetBool("JumpOverObstcle", _jumpOverObstcle);
    75.         _animator.SetBool("holding", holding);
    76.         _animator.SetInteger("SwingCount", tapCount);
    77.      
    78.     }
    79.  
    80. }
    81.  
    I changed it to:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. [RequireComponent(typeof(Animator))]
    8.  
    9. //Name of class must be name of file as well
    10.  
    11. public class LocomotionPlayer : MonoBehaviour
    12. {
    13.  
    14.     private Transform mountPoint;
    15.     private Vector3 moveDirection = Vector3.zero;
    16.     protected Animator _animator;
    17.     private CharacterController _charCtrl;
    18.  
    19.     private bool _jump;
    20.  
    21.     public bool _run;
    22.     public bool canRotate = true;
    23.     public bool holding;
    24.     public bool alive = true;
    25.  
    26.     public float _speed;
    27.     public float _strafe;
    28.     public float gravity;
    29.     public float health;
    30.  
    31.     private float _mouseX;
    32.     private float tapTimeWindow;
    33.  
    34.     public int tapCount;
    35.  
    36.     // Use this for initialization
    37.     void Start ()
    38.     {
    39.         _animator = GetComponent<Animator>();
    40.         _charCtrl = GetComponent<CharacterController>();
    41.         m_AudioSource = GetComponent<AudioSource>();
    42.     }
    43.  
    44.     void Update ()
    45.     {
    46.       _mouseX = Input.GetAxis("Horizontal"); // -->> Changed here
    47.         _speed = Input.GetAxis("Vertical");
    48.         _strafe = Input.GetAxis("Horizontal");
    49.         _run = Input.GetKey(KeyCode.LeftShift) ? true : false;
    50.         //PROCESSING ROTATION
    51.         Vector3 aimPoint =  transform.forward * 10f; // -->> Changed here and removed if statement
    52.  
    53.      
    54.  
    55.         Quaternion targetRotation = Quaternion.LookRotation(aimPoint);
    56.  
    57.         this.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 4 * Time.deltaTime);
    58.  
    59.         this.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
    60.  
    61.  
    62.  
    63.  
    64.      
    65.         if ((_charCtrl != null) && (!swimming))
    66.         { //is character controller enabled
    67.             if (_charCtrl.isGrounded)
    68.             {
    69.                 moveDirection.y = 0;
    70.             }
    71.  
    72.         }
    73.      
    74.     }
    75.     void FixedUpdate ()
    76.     {
    77.         _animator.SetFloat("mouseX", _mouseX, 0.3f, Time.deltaTime);
    78.         _animator.SetFloat("Speed", _speed);
    79.         _animator.SetFloat("Strafe", _strafe);
    80.         _animator.SetBool("Run", _run);
    81.         _animator.SetBool("JumpOverObstcle", _jumpOverObstcle);
    82.         _animator.SetBool("holding", holding);
    83.         _animator.SetInteger("SwingCount", tapCount);
    84.      
    85.     }
    86.  
    87. }
    88.  
    Now my player is walking left and right when I press the left and right arrow keys but he isn't doing it while he is walking forward, he only does it while he stands still. What do I do wrong?
     
  2. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    Ok I tried some more today. Now my player ist walking to the left and right while he is walking forward but he doesn't turn the body to the left or right during the walking. I have no idea what's wrong and hope someone can help me:
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (Input.GetKey (KeyCode.LeftArrow))
    4.             _mouseX = -1f;
    5.  
    6.         else if (Input.GetKey(KeyCode.RightArrow))
    7.             _mouseX = 1f;
    8.         else
    9.             _mouseX = 0f;
    10.  
    11.        
    12.         _speed = Input.GetAxis("Vertical");//reading vertical axis input
    13.         _strafe = Input.GetAxis("Horizontal");//reading horizontal axis input
    14.         _run = Input.GetKey(KeyCode.LeftShift) ? true : false;//check if run button was pressed
    15.         //PROCESSING ROTATION
    16.         Vector3 aimPoint =  transform.forward*10f;
    17.        
    18.  
    19.          Quaternion targetRotation = Quaternion.LookRotation(aimPoint);
    20.          this.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 4* Time.deltaTime);
    21.          this.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
    22.        
    23. }