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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Move object with

Discussion in 'Scripting' started by Sylon87, Sep 20, 2018.

  1. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    Hi everyone

    I have my ball that will get throwed to an exact point, the starting piint is


    Y = 2
    Z = 0

    Target position is

    Y= 0
    Z = 10

    The point is, ihow do it, im totally confused

    Any help?
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    In a straight line or with an arc? Straight line is easy:
    Code (csharp):
    1.  
    2. public Vector3 start = new Vector3(0.0f, 2.0f, 0.0f);
    3. public Vector3 end = new Vector3(0.0f, 0.0f, 10.0f);
    4. public float lerp = 0.0f;
    5.  
    6. private void Update()
    7. {
    8.     transform.position = Vector3.Lerp(start, end, lerp);
    9. }
    10.  
    Just adjust the lerp value between 0 and 1 while running.
     
  3. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    Its an arc :)
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Ok, still doable:
    Code (csharp):
    1.  
    2. public Vector3 start = new Vector3(0.0f, 2.0f, 0.0f);
    3. public Vector3 end = new Vector3(0.0f, 0.0f, 10.0f);
    4. public float arc = 10.0f; // Height of the arc in the middle point
    5. public float lerp = 0.0f;
    6.  
    7. private void Update()
    8. {
    9.     Vector3 position = Vector3.Lerp(start, end, lerp);
    10.     float arcing = arc * 4.0f * (lerp - lerp * lerp);
    11.     position.y += arcing;
    12.     transform.position = position;
    13. }
    14.  
     
  5. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    i'm trying to use your code, but it's not working
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Compile error (which error) or does it do something other (what) than expected?
     
  7. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    i'm using this script now, that's works fine

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class ParabolaScript : MonoBehaviour
    4. {
    5.     public Vector3 Target = new Vector3(10.0f,0,0);
    6.     public float firingAngle = 45.0f;
    7.     public float gravity = 9.8f;
    8.     public Transform Projectile;    
    9.     private Transform myTransform;
    10.  
    11.     void Awake()
    12.     {
    13.         myTransform = transform;    
    14.     }
    15.     void Start()
    16.     {        
    17.         StartCoroutine(SimulateProjectile());
    18.     }
    19.     IEnumerator SimulateProjectile()
    20.     {
    21.      
    22.        /* // Move projectile to the position of throwing object + add some offset if needed.
    23.         transform.position = myTransform.position + new Vector3(0, 0.0f, 0);*/
    24.      
    25.         // Calculate distance to target
    26.         float target_Distance = Vector3.Distance(transform.position, Target);
    27.         // Calculate the velocity needed to throw the object to the target at specified angle.
    28.         float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);
    29.         // Extract the X  Y componenent of the velocity
    30.         float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
    31.         float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);
    32.         // Calculate flight time.
    33.         float flightDuration = target_Distance / Vx;
    34.  
    35.         // Rotate projectile to face the target.
    36.         transform.rotation = Quaternion.LookRotation(Target - transform.position);
    37.      
    38.         float elapse_time = 0;
    39.         while (elapse_time < flightDuration)
    40.         {
    41.             transform.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
    42.          
    43.             elapse_time += Time.deltaTime;
    44.             yield return null;
    45.         }
    46.     }
    47. }