Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mortar Script - align GameObjects between start and end?

Discussion in 'Scripting' started by mcunha98, Mar 22, 2015.

  1. mcunha98

    mcunha98

    Joined:
    Jun 13, 2010
    Posts:
    261
    I have a script that performs mortar launching a given object and use it to control the height and curvature of the emptys 4 path, the first being the origin and the final destination.

    The other two are emptys ' guides to the trajectory, just that I'm not able to align the waypoints as the target point moves and the line that should be aligned between the two points form a totally unwanted curvature.



    The green line is the desired trajectory, but the real trajectory is the yellow path.


    The mortar script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Morteiro : MonoBehaviour
    6. {
    7.     private float t = 0f;
    8.     private Bezier myBezier;
    9.  
    10.     private Vector3 v0;
    11.     private Vector3 v1;
    12.     private Vector3 v2;
    13.     private Vector3 v3;
    14.  
    15.     public GameObject projetil;
    16.     public GameObject alvo;
    17.     public GameObject p0;
    18.     public GameObject p1;
    19.     public GameObject p2;
    20.     public GameObject p3;
    21.  
    22.     public bool seguirAlvo = true;
    23.     public float velocidade = 0.009f;
    24.  
    25.     private Vector3 p2Original;
    26.     private Vector3 p3Original;
    27.  
    28.  
    29.     void Start()
    30.     {
    31.         Refresh();
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         if (alvo == null) Destroy(gameObject);
    37.         if (projetil == null) return;
    38.         if (seguirAlvo && alvo != null) p3.transform.position = alvo.transform.position;
    39.         //if (v0 != p0.transform.position || v1 != p1.transform.position || v2 != p2.transform.position || v3 != p3.transform.position) Refresh();
    40.         Refresh();
    41.         Vector3 vec = myBezier.GetPointAtTime(t);
    42.         projetil.transform.position = vec;
    43.         projetil.transform.LookAt(vec);
    44.         t += velocidade;
    45.         if (t > 1f) t = 0f;
    46.     }
    47.  
    48.     private void Refresh()
    49.     {
    50.         v0 = p0.transform.position;
    51.         v1 = p1.transform.position;
    52.         v2 = p2.transform.position;
    53.         v3 = p3.transform.position;
    54.         myBezier = new Bezier(p0.transform.position, p1.transform.position, p2.transform.position, p3.transform.position);
    55.     }
    56.  
    57.  
    58.     void OnDrawGizmos()
    59.     {
    60.         Refresh();
    61.  
    62.         Gizmos.color = Color.red;
    63.         float s = 0.2f;
    64.         if (p0 != null) Gizmos.DrawCube(p0.transform.position, new Vector3(s, s, s));
    65.         if (p1 != null) Gizmos.DrawCube(p1.transform.position, new Vector3(s, s, s));
    66.         if (p2 != null) Gizmos.DrawCube(p2.transform.position, new Vector3(s, s, s));
    67.         if (p3 != null) Gizmos.DrawCube(p3.transform.position, new Vector3(s, s, s));
    68.  
    69.         t = 0f;
    70.         Gizmos.color = Color.yellow;
    71.         float z = 0.02f;
    72.         while (t <= 1f)
    73.         {
    74.             t += 0.009f;
    75.             Gizmos.DrawSphere(myBezier.GetPointAtTime(t), z);
    76.         }
    77.         t = 0f;      
    78.     }
    79. }
    80.  

    And the Bezier component:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [System.Serializable]
    6. public class Bezier : System.Object
    7. {
    8.   private const float param = 1.5f;
    9.  
    10.   public Vector3 p0;
    11.   public Vector3 p1;
    12.   public Vector3 p2;
    13.   public Vector3 p3;
    14.  
    15.   public float ti = 0f;
    16.  
    17.   private Vector3 b0 = Vector3.zero;
    18.   private Vector3 b1 = Vector3.zero;
    19.   private Vector3 b2 = Vector3.zero;
    20.   private Vector3 b3 = Vector3.zero;
    21.  
    22.   private float Ax;
    23.   private float Ay;
    24.   private float Az;
    25.  
    26.   private float Bx;
    27.   private float By;
    28.   private float Bz;
    29.  
    30.   private float Cx;
    31.   private float Cy;
    32.   private float Cz;
    33.  
    34.   public Bezier(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3)
    35.   {
    36.   this.p0 = v0;
    37.   this.p1 = v1;
    38.   this.p2 = v2;
    39.   this.p3 = v3;
    40.   }
    41.  
    42.   public Vector3 GetPointAtTime(float t)
    43.   {
    44.   this.CheckConstant();
    45.   float t2 = t * t;
    46.   float t3 = t * t * t;
    47.   float x = this.Ax * t3 + this.Bx * t2 + this.Cx * t + p0.x;
    48.   float y = this.Ay * t3 + this.By * t2 + this.Cy * t + p0.y;
    49.   float z = this.Az * t3 + this.Bz * t2 + this.Cz * t + p0.z;
    50.   return new Vector3(x, y, z);
    51.   }
    52.  
    53.   private void CheckConstant()
    54.   {
    55.   if (this.p0 != this.b0 || this.p1 != this.b1 || this.p2 != this.b2 || this.p3 != this.b3)
    56.   {
    57.   this.SetConstant();
    58.   this.b0 = this.p0;
    59.   this.b1 = this.p1;
    60.   this.b2 = this.p2;
    61.   this.b3 = this.p3;
    62.   }
    63.   }
    64.  
    65.   private void SetConstant()
    66.   {
    67.   this.Cx = param * ((this.p0.x + this.p1.x) - this.p0.x);
    68.   this.Bx = param * ((this.p3.x + this.p2.x) - (this.p0.x + this.p1.x)) - this.Cx;
    69.   this.Ax = this.p3.x - this.p0.x - this.Cx - this.Bx;
    70.  
    71.   this.Cy = param * ((this.p0.y + this.p1.y) - this.p0.y);
    72.   this.By = param * ((this.p3.y + this.p2.y) - (this.p0.y + this.p1.y)) - this.Cy;
    73.   this.Ay = this.p3.y - this.p0.y - this.Cy - this.By;
    74.  
    75.   this.Cz = param * ((this.p0.z + this.p1.z) - this.p0.z);
    76.   this.Bz = param * ((this.p3.z + this.p2.z) - (this.p0.z + this.p1.z)) - this.Cz;
    77.   this.Az = this.p3.z - this.p0.z - this.Cz - this.Bz;
    78.   }
    79. }
    Someone can help me ?
     
  2. mcunha98

    mcunha98

    Joined:
    Jun 13, 2010
    Posts:
    261
    Nobody can help me, please ?
     
  3. GdeCarpentier

    GdeCarpentier

    Joined:
    Jun 18, 2013
    Posts:
    5
    Hi Mauricio. I think the constants in your Bezier code might be off, assuming you want it to be following an actual Bezier curve, that is. I think it should be something like: A = -3 p0 + 3 p1, B = 3 p0 - 6 p1 + 3 p2 and C = -p0 + 3 p1 - 3p2 + p3. Hope it works! If you're interested in doing projectiles in a slightly more physically correct way but still use a form of curves instead of doing a simulation, you may be interested in an open-source project of mine. Good luck!
     
    Last edited: Mar 31, 2015
  4. mcunha98

    mcunha98

    Joined:
    Jun 13, 2010
    Posts:
    261
    Problem solved, thanks for reply