How to move a cube 3 second in one direction, and next 3 seconds in oppposite direction, alternatively

Viewed 164

I am new to unity, and trying something like below, but I can either move only in one direction, or not moving at all.

My cube is a trigger, and is not using gravity. I have checked the Kitematic box. I am trying to make the cube move to and fro, so that player have difficuly collecting it.

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class movedanger : MonoBehaviour
{
    private int mytime = 0;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        MyMover(mytime);
    }

    void MyMover(int mytime)
    {
      if (mytime <= 3)
        {
            transform.Translate(Vector3.forward * Time.deltaTime);
            mytime++;
        }

        else
        {
            transform.Translate(-Vector3.forward * Time.deltaTime);
            mytime = 1;
        }
    }
        
}
3 Answers

What you are looking for is to and fro movement of an object. You can achieve this with Mathf.PingPong() function instead of using translate. I have tested it with a cube, you can set the minimum and maximum distance it should move to and the speed at which it travels. Since you want the cube to move 3 seconds in one direction at a time. You can calculate the speed as distance/time so the max distance it should travel to from the current distance and the time (3 seconds) it takes. Hope this helps.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCube : MonoBehaviour {
    public float min = 2f;
    public float max = 8f;
    public float SpeedOfMovement = 2f;
    // Start is called before the first frame update
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        transform.position = new Vector3 (Mathf.PingPong (Time.time * SpeedOfMovement, max - min) + min, transform.position.y, transform.position.z);
    }


}

With InvokeRepeating you will call the same MoveCube method every 3 seconds.

using UnityEngine;

public class MoveDanger: MonoBehaviour
{
    public bool isForward = false;

    private void Start()
    {
      
        InvokeRepeating("MoveCube", 0f, 3f);
    }

    private void MoveCube()
    {
        if (isForward)
        {
            transform.Translate(Vector3.back);
            isForward = false;
        }
        else
        {
            transform.Translate(Vector3.forward);
            isForward = true;
        }
    }
}

Honestly the best and easiest way to do something like this, once you get used to it, is just to

Use Unity's incredibly simple animator system:

enter image description here

(Essentially just click "new animation" and then drag the object around as you want it animated.)

There are 100s of tutorials online explaining how to use it.

It's one of those things where once you use it and see how easy it is, you will do a "facepalm" and never bother again with other ways.

It's really "the Unity way" to achieve the goal here, dead easy and flexible.

Related