mercredi 2 septembre 2015

Unity 3D création d'un arc avec Line Renderer/ Unity 3D create Arc with Line Renderer

J'ai téléchargé le projet fait par l'usager UVRadiation sur le forum;

http://forum.unity3d.com/threads/projection-trajectory-arc.12588/

Le code était en Javascript, je l'ai donc converti en C# et fait la modification pour que l'arc soit créé à partir de 3 point (début, milieu et fin).  Le mouvement de la souris modifie la position de l'objet fin ce qui agrandit et diminue l'arc.

J'ai aussi pris les fonctions du script Bezier que j'ai inclus directement dans le script ArcBehavior.cs

Eng/
I downloaded the project done by UVRadiation user forum ;

http://forum.unity3d.com/threads/projection-trajectory-arc.12588/

The code was in Javascript, so I converted to C # and made ​​the change to the arc is created from three points (start, middle and end) . The movement of the mouse changes the position of the end  object which enlarges and reduces the arc.

I also took the functions of Bezier script I included directly in the script ArcBehavior.cs
1:  using UnityEngine;  
2:  using System.Collections;  
3:  public class ArcBehaviourC1: MonoBehaviour {  
4:    public Transform start;  
5:    public Transform middle;  
6:    public Transform end;  
7:    public float maxHeight = 200;  
8:    public float maxWidth = 200 ;  
9:    public int sections = 10;  
10:    int floorMask;  
11:    Ray shootRay;  
12:    LineRenderer GrenadeLine;  
13:    float camRayLength = 50f;  
14:    bool raycastok;  
15:    // Use this for initialization  
16:    void Start () {  
17:       GrenadeLine = GetComponent <LineRenderer> ();  
18:      floorMask = LayerMask.GetMask ("Floor");  
19:      GrenadeLine.SetVertexCount(sections);  
20:    }  
21:    // Update is called once per frame  
22:    void Update () {  
23:      Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);  
24:      RaycastHit floorHit;  
25:      shootRay.origin = transform.position;  
26:      shootRay.direction = transform.forward;  
27:      raycastok = Physics.Raycast (camRay, out floorHit, camRayLength, floorMask);  
28:      Vector3 objectToMouse = floorHit.point - transform.position;  
29:      objectToMouse.y = 0f;  
30:      if (Mathf.Abs(objectToMouse.z) < 20 && Mathf.Abs(objectToMouse.x) < 20 )  
31:      {  
32:        if (raycastok)  
33:        {  
34:          GrenadeLine.enabled = true;  
35:           end.transform.position = floorHit.point;  
36:          Debug.DrawLine (start.position, middle.position, Color.red);  
37:          Debug.DrawLine (middle.position, end.position, Color.red);  
38:          Debug.DrawLine (start.position, end.position, Color.red);  
39:          Plot (start.position, middle.position, end.position);  
40:        }  
41:        else  
42:        {  
43:          GrenadeLine.enabled = false;     
44:        }  
45:      }  
46:      else  
47:      {  
48:        GrenadeLine.enabled = false;  
49:      }  
50:    }  
51:    Vector3 GetQuadraticCoordinates(float t , Vector3 p0, Vector3 c0, Vector3 p1)  
52:    {  
53:      Vector3 Result;  
54:      Result = Mathf.Pow(1-t,2)*p0 + 2*t*(1-t)*c0 + Mathf.Pow(t,2)*p1 ;  
55:      return Result;  
56:    }  
57:    void Plot(Vector3 p0 , Vector3 c0,Vector3 p1 )  
58:    {  
59:      float t;  
60:      for (int i = 0 ; i < sections ; i++ )  
61:      {  
62:        t = (float)i/(sections-1);  
63:        GrenadeLine.SetPosition (i ,GetQuadraticCoordinates(t , p0 , c0 , p1 ));  
64:      }  
65:    }  
66:  }  





Aucun commentaire:

Publier un commentaire