- Home /
How to translate an empty?
I've created a weapon recoil script where when you hold down left click, a variable number gradually increases by 2.5 per second. While this number increases i want an empty to translate upwards by the variable number. Once you release left click the variable number slowly decreases by 3 per second, and the empty moves back down gradually. The max height is 4, and the minimum is 0. I dont want the empty object to snap from position a to b, i want it to slowly go up by float values.
public float RecoilPerSecond = 1.25f;
public float RecoilRecovery = 0.30f;
public int MaxRecoil = 4;
public float MinRecoil = 0.0f;
public bool IsFiring = false;
public float CurrentRecoil = 1f;
void Update () {
//When holding left click call recoil multiplier and cancel recoil reducer
if(Input.GetMouseButtonDown (0)){
IsFiring = true;
if(IsFiring == true){
recoilRepeater();
CancelInvoke("minRecoil");
}
}
//When releasing left click call recoil reducer script and cancel recoil multiplier
if(Input.GetMouseButtonUp(0)){
IsFiring = false;
CancelInvoke("recoilMultiplier");
minCheck();
}
}
//Function to increase recoil every 0.1 seconds by 0.75
void recoilMultiplier (){
RecoilPerSecond *= 1;
CurrentRecoil += RecoilPerSecond;
if (CurrentRecoil >= 4) {
CurrentRecoil = 4;
}
}
//Constantly repeating recoil multiplier
void recoilRepeater (){
InvokeRepeating ("recoilMultiplier", 1, 0.10f);
}
//Function to reduce recoil every 0.1 seconds by 0.3
void minRecoil (){
CurrentRecoil -= RecoilRecovery;
if (CurrentRecoil <= MinRecoil) {
CurrentRecoil = MinRecoil;
}
}
//Constantly repeating recoil reducer
void minCheck (){
InvokeRepeating ("minRecoil",1,0.10f);
}
}
Just looking at the title of the Q: empties move the same way as any other gameObject. Translate them the same way as anything else.
It might help to make a a marker for it. $$anonymous$$aybe a tiny, bright colliderless ball, childed at 000 to the empty (so it's always exactly where the empty is.)
Answer by rhbrr5hrfgdfgw · Jun 18, 2015 at 03:55 PM
Something like:
public GameObject empty;//put in editor
public float MaxRecoil = 4.0f;
public float sumY = MaxRecoil + empty.transform.localPosition.y;
public float minRecoil = ?;//Find the value you want in theeditor
public float increaseValue = 2.5;
public float decreaseValue = 3;
void Update(){
if(IsFiring && empty.transform.localPosition.y <= sumY){
empty.transform.localPosition.y += increaseValue *Time.deltaTime;
}else if(!isFiring && empty.transform.localPosition.y > minRecoil){
empty.transform.localPosition.y -= decreaseValue *Time.deltaTime;
}
}
Its untested code
Your answer
![](https://koobas.hobune.stream/wayback/20220613201123im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vector3.Lerp moves in the wrong direction 1 Answer
How to get this object to move 2 Answers
Multiple Cars not working 1 Answer
Move GameObject along a line according to the players look direction 3 Answers
Distribute terrain in zones 3 Answers