Search Unity

Change the input of this script

Discussion in 'Scripting' started by Treasureman, Mar 1, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a code with the input Aim set up to it. Here's the script...
    Code (JavaScript):
    1. #pragma strict
    2. @SerializeField
    3. var aimSpeed : float;
    4. function Update () {
    5.     if (Input.GetButton("Aim"))
    6.         Aim();
    7. }
    8. function Aim() {
    9.     var aimRotation : Vector3 = transform.rotation.eulerAngles;
    10.     aimRotation.y = Camera.main.transform.rotation.eulerAngles.y;
    11.     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(aimRotation), Time.deltaTime * aimSpeed);
    12. }
    But I want it to work when you move and not when you hold the Aim input. What would I put instead of...
    Code (JavaScript):
    1. (GetButton("Aim"))
    ...if I want the input to be movement?
     
  2. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,025
    Can you explain a bit more about what you want to do? What object is this code attached to? I see that you want to rotate a transform to align with the camera on the y-axis but I don't know in which cases you do/don't want this alignment to happen. What exactly do you mean by movement, any movement?

    William
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Oh sorry, I forgot to tell what the script did. It's attached to a FPS Controller and it makes it smooth align with the camera when the input Aim is held. I just want it changed so that instead of the input aim being held, it activates when the player moves
     
  4. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,025
    You could try replacing:

    Code (JavaScript):
    1.  if (Input.GetButton("Aim"))
    with

    Code (JavaScript):
    1.  if (Input.GetAxis("Vertical") != 0f)
    Since the Input "Vertical" axis is normally set up with the up and down arrow keys, then pressing either up or down would activate the script (you can change the axis keys in the Player Settings -> Input).

    Note that the script is pretty much only active for as long as you hold the key down (however there is an on/off lag on the input axis), so depending on your lerp coefficient you would have to hold the key down for a certain period of time to get it to align properly. Otherwise you might try a setup where once you activate the script, it continues until the alignment is made to within a certain margin.