hi all,
i have a cube and i would like to move it horizantally x meters distance in t duration so what should be cube velocity to reach the target position in t duration? what is the formulate?
hi all,
i have a cube and i would like to move it horizantally x meters distance in t duration so what should be cube velocity to reach the target position in t duration? what is the formulate?
Velocity is defined as distance over time. You have your velocity already:
var x = 10f
var t = 3f
var velocity = x / t;
Add this script as a component of cube you want to move. Create an empty gameobject named “targetPoint” whose x value equals to x value of your target place.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCube : MonoBehaviour {
private Transform targetPoint;
private Rigidbody rb;
public float duration;
private void Awake()
{
rb = GetComponent<Rigidbody>();
targetPoint = GameObject.Find("targetPoint").GetComponent<Transform>();
}
private void Start()
{
float xDistance = targetPoint.position.x - transform.position.x;
float speedX = xDistance / duration;
rb.velocity = new Vector3(speedX,0,0);
}
}
it is on the ground and we should care of the gravity. but how should i implement when take account of gravity?