0
\$\begingroup\$

Sorry if my title was not clear. My script needs to fix the method of movement object. In my script, the Z axis angel of main object moves with the target object on Y axis. If target goes under zero value, main object goes back smoothly. But, if target goes up from "part 2 to part 1"...it just be there.I don't know how to invert or reverse the movement. Here is my script

public Transform target;
public float dis, zz;

// Update is called once per frame
void Update()
{
    dis = transform.position.y - target.transform.position.y;
    transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, zz);
        // part 1
    if (dis < 0.0f && dis > -0.3f)
    {
        zz -= Time.deltaTime * 0.2f;
        if (zz <= -0.3f)
        {
            zz = -0.3f;
        }
    }
        // part 2
    if (dis < 0.31f && dis > -0.6f)
    {
        zz -= Time.deltaTime * 0.2f;
        if (zz <= -0.6f)
        {
            zz = -0.6f;
        }
    }
        // part 3
    if (dis < 0.61f && dis > -0.9f)
    {
        zz -= Time.deltaTime * 0.2f;
        if (zz <= -0.9f)
        {
            zz = -0.9f;
        }
    }

}
\$\endgroup\$
1
\$\begingroup\$

Maybe you meant to write else if instead of just else in part 2 and part 3. It’s hard to follow what it’s doing because it’s entering all 3 if statements in some cases, but it feels to me like you need those cases to be exclusive of each other.

If this is wrong please describe the exact behavior you’re trying to achieve.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.