Multiple object animation script for click & point

Hello i am working with click & point game. I need to animate multiple object like cabinet doors, drawers etc.

I am at this point right now.
sentimentalmeagerindusriverdolphin

Its working with gameobject with given transform and they are simply movetowards between them.

My question is how can i do this for whole objects. What is the efficient way for movetowards multiple object?

Here is my script:

public float speed = 10f;
    public Transform startMarker;
    public Transform endMarker;
    private Vector3 startPos;
    private Vector3 currentTargetPos;
    private Quaternion currentTargetRot;
    private Quaternion startRot;
    private bool isMovingObject = false;

    void Start()
    {
        currentTargetPos = endMarker.position;
        startPos = startMarker.position;
        currentTargetRot = endMarker.rotation;
        startRot = startMarker.rotation;
    }
    void Update()
    {
        if (isMovingObject)
        {
            this.transform.position = Vector3.MoveTowards(this.transform.position, currentTargetPos, speed * Time.deltaTime);
            this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, currentTargetRot, speed * Time.deltaTime * 20);
            if (this.transform.position == currentTargetPos && this.transform.rotation == currentTargetRot)
            {
                currentTargetPos = startPos;
                currentTargetRot = startRot;
                isMovingObject = false;
            }

            if (this.transform.position == startPos && this.transform.rotation == startRot)
            {
                currentTargetPos = endMarker.position;
                currentTargetRot = endMarker.rotation;
                isMovingObject = false;
            }
        }
    }

    public void Move()
    {
        isMovingObject = true;
    }

It seems like you should be doing proper animations.

Im very guilty of doing things the hard way too, btw.

but as an FYI manually moving things is crazy expensive compared to doing an animation.

also, I’m sure you’ll want have more dramatic motions for more complicated things like curtains.

Firstly thanks for reply,

I consider your opinion, i was in this dilemma too.(Animator animation vs scripting).
You have a good point for more dramatic motions. But i was afraid for create bunch of animation clip for every moving object. Do we have a better solution?

I had a repeatable script for movable platforms that worked really well.

I would make a platform, then copy it and place it where I want it to go, and placed it as a child.

then put a script on the parent that at startup it saved the child’s transform then disabled the child.

Then I made floats for speed, delay, and whatnot.

When it triggered I moved the platform to the child’s location.

I was able to use that for elevators, doors, and the like.

it made setting them up a breeze.

I also made it so it was on entering a trigger or on use with some simple bools.

seems like that may be a better solution than manually entering a bunch of numbers every time.

EDIT: oh, and of course this makes it easy to slap in some sound effects.

Actualy i am a little bit confused about platform system. Do we have multiple gameobjects(platforms) and how it is shifting
Can you write simple repeatable script ?
EDIT: i guess elevators have same spacing between floors. How can we use it for my case?

In my example I used that script for platforms, doors, and elevators.

you can do the same thing with doors in a point and click.

click on a door and it opens.

there’s no reason to make a new script for every door. Just make one that fits multiple instances.

Oh you simplfy it for me thanks. So to be clear i have to attach move script for every game object i want to move.
and every moving gameobject has to child for target transform. (and delete after)

So we are back to scripting instead of animations.

Scripting animated motions has a place, but don’t waste too much time reinventing the wheel. You can get a lot of benefit from learning how to use a tweening assets such as DOTween or ITween or LeanTween. Spend the time to see what all it can do for you because combining it with your conditional user inputs can really go a long way to being interesting for the type of game you describe.

2 Likes

I totaly forget about tweening, saw them 4-5 month ago. Actualy im new in unity its great reminder for me. I should research more before start a project. Thanks alot :slight_smile:

1 Like

Tweening is pretty powerful, splitting that line between “dumb” animations and laboriously coding tons of different moving routines yourself.