Is there a function that according to the name of the object, its tag and the time taken since the beginning of the game(for example time+=Time.deltaTime), gives the position of this object?

if yes which one?


from a rectilinear trajectory unknown to a randomly translated object, I need to press a Q key to know its direction vector. For this, I get the position of the object to the object. Q support and if I get the position of the object in the lapse of time preceding the moment of this support, the difference of these 2 positions gives me the direction sought of the trajectory at this moment.

When I have perfectly resolved this script, I will create another very similar one that will give me the vector direction of the rectilinear trajectory of the object at each collision on another object

Here is my script:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class script5 : MonoBehaviour {

//public GameObject sphere;
public static float t;
//public float laps_time;
public static float temps;
//public float difftime;
//float temps_appui;

float x1,y1,j1;
float x2,y2,j2;
float x3,y3,j3;

// Vector3 cat = transform.position; struct vecteur { public float k1; public float k2; public float k3;


// Use this for initialization void Start () { temps+=Time.deltaTime;


//fonction retournant la position d'un objet à un instant précis
vecteur function_script6(float t)

    vecteur v;
    v.k1 = 0;
    v.k2 = 0;
    v.k3 = 0;

    Vector3 cat = transform.position;

    if (temps == t) {
        v.k1 = cat.x;
        v.k2 = cat.y;
        v.k3 = cat.z;


    return v;

// Update is called once per frame
void Update () {

    float temps_appui;
    public float difftime;
    public float laps_time;
    //t += Time.time;

    //donner les coordonnées de la sphère au temps où la touche Q est appuyée
    if (Input.GetKeyDown (KeyCode.Q)) {

        //donner les coordonnées de la sphère au temps où la touche Q est appuyée
        temps_appui = temps;

        function_script6 (temps_appui);

        //donner les coordonnées de la sphère au temps juste avant l'appui de la touche Q
        function_script6 (difftime);


        j1=function_script6 (temps_appui).k1;
        j2=function_script6 (temps_appui).k2;
        j3=function_script6 (temps_appui).k3;

        //donne le vecteur de la trajectoire de la sphère à l'appui de la touche Q
        y1=function_script6 (temps_appui).k1-function_script6 (difftime).k1;
        y2=function_script6 (temps_appui).k2-function_script6 (difftime).k2;
        y3=function_script6 (temps_appui).k3-function_script6 (difftime).k3;

        print ("(position appui:" + j1 + "," +j2 + "," + j3 + ")");
        print ("(position difftime:" + x1 + "," + x2 + "," + x3 + ")");
        print ("(vecteur trajectoire:" + y1 + "," + y2 + "," + y3 + ")");
        print ("-----------------------------");


} }

almost everything is good except for the position of difftime which is always null. Why?

  • \$\begingroup\$ Please don't completely change the subject of your question after it already has an answer. You've done this repeatedly now, and it's disrespectful to the users who have taken their time to answer you. Instead, make sure you put in the work up-front, to ask about the real problem you're having, not about your guessed-at solution, to avoid the XY Problem \$\endgroup\$ – DMGregory Apr 19 at 22:58
  • \$\begingroup\$ In this case, it looks like you're trying to intercept an object moving with constant velocity, is that accurate? Have you reviewed existing Q&A about solving this type of problem? Please take the time to edit your question and ensure it's formatted correctly, and explains all steps of what you need. \$\endgroup\$ – DMGregory Apr 19 at 23:03

Your Answer

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

Browse other questions tagged or ask your own question.