- Home /
Increase when the Key is Down and Decrease when the Key is Up
I whant a script that when i press a keycode down the value increase and when leave the key the value decrease with a max and min value how i can do that :( i am so beginner.
Comment
Best Answer
Answer by itvarth · Jan 10, 2020 at 08:11 PM
I solved it with this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetFov : MonoBehaviour
{
public float maxValue;
public float currentValue; //Current value is the Min Value too
public float smooth;
bool isFoved;
void Update()
{
if (Input.GetKey(KeyCode.E))
{
isFoved = true;
}
else
{
if (Input.GetKeyUp(KeyCode.E))
{
isFoved = false;
}
}
if (isFoved)
{
GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, maxValue, Time.deltaTime * smooth);
}
else
{
GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, currentValue, Time.deltaTime * smooth);
}
}
}
Answer by Fariborzzn · Jan 10, 2020 at 08:14 PM
Hey Dude Try This:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
float maxvalue = 4;
float minvalue = 0;
public float myvalue=0;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButton( "Fire1" )) //change your button base on your need
{
myvalue+=Time.deltaTime;
myvalue=Mathf.Clamp( myvalue , minvalue , maxvalue );
}
else
{
myvalue-=Time.deltaTime;
myvalue=Mathf.Clamp( myvalue , minvalue , maxvalue );
}
}
}