Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to define a motion trajectory?

Discussion in 'Getting Started' started by tiho_bg, Dec 12, 2016.

  1. tiho_bg

    tiho_bg

    Joined:
    Jun 24, 2016
    Posts:
    39
    Hi

    I would like to ask how to define the trajectory of motion using unity 3D. I have object 1 and object 2. When the distance is less than a defined threshold, the object 1 has to move to the object 2 along the motion-trajectory from the sketch. How to define this motion trajectory using unity 3D? The motion is realized after pushing a key from the PC keyboard.

    Could you please help me to resolve the problem?
     

    Attached Files:

    • Pic1.jpg
      Pic1.jpg
      File size:
      31.5 KB
      Views:
      853
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    That trajectory appears to be a straight line. You can do this trivially by calling MoveTowards in an Update method. (In fact the sample script in the docs there is pretty much exactly what you want.)
     
    tiho_bg likes this.
  3. tiho_bg

    tiho_bg

    Joined:
    Jun 24, 2016
    Posts:
    39
    Hi,

    I tried to use MoveTowards but without success. Could you please help me to resolve the problem?
    Here is my code:

    publicclassNeedleMove : MonoBehaviour {


    publicGameObject Point_Sphere;
    publicGameObject Cell;

    publicTransform target;

    publicfloat speed;


    // Update is called once per frame

    void Update () {


    if (Input.GetKey(KeyCode.P))

    {
    Cell.transform.position = Vector3.MoveTowards(Cell.transform.position, target.position, 10.00f * Time.deltaTime);
    }

    }
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    You haven't described what the problem is. Your code looks correct (assuming you want Cell to move towards target at 10 units/second while the P key is held down).
     
    tiho_bg likes this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
    Is that a direct copy and paste? Because there are spaces missing in some very key locations and one bracket is missing.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NeedleMove : MonoBehaviour
    6. {
    7.     public GameObject Point_Sphere;
    8.     public GameObject Cell;
    9.  
    10.     public Transform target;
    11.  
    12.     public float speed;
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (Input.GetKey(KeyCode.P))
    18.         {
    19.             Cell.transform.position = Vector3.MoveTowards(Cell.transform.position, target.position, 10.00f * Time.deltaTime);
    20.         }
    21.     }
    22. }
     
    tiho_bg likes this.
  6. tiho_bg

    tiho_bg

    Joined:
    Jun 24, 2016
    Posts:
    39
    Hi
    The problem is that the Cell object doesn't move to the target position after pressing the key button "P".
     
  7. tiho_bg

    tiho_bg

    Joined:
    Jun 24, 2016
    Posts:
    39
    Here are screeshots of my application.
     

    Attached Files:

  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Do you mean while pressing "P"? Because the way you've written it, it will only move while the key is held down.
     
    tiho_bg likes this.
  9. tiho_bg

    tiho_bg

    Joined:
    Jun 24, 2016
    Posts:
    39
    Yes
    Something is wrong. It doesn't work while the key is held down.
     
  10. smacbride

    smacbride

    Joined:
    Jan 10, 2015
    Posts:
    50
    Is the script attached to anything in the scene?
     
    tiho_bg likes this.
  11. tiho_bg

    tiho_bg

    Joined:
    Jun 24, 2016
    Posts:
    39
    Yes it was. I deselected it but it is still not working. The script is not attached to the cell object. Is it an error?
     
  12. smacbride

    smacbride

    Joined:
    Jan 10, 2015
    Posts:
    50
    Put a Debug.Log() statement inside the if condition to make sure it's getting called.
     
    tiho_bg likes this.
  13. tiho_bg

    tiho_bg

    Joined:
    Jun 24, 2016
    Posts:
    39
    Everything is OK. I wrote the script again.
    Thank you for the help.