Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Doubt in drag and shoot code with sprite.

Discussion in 'Scripting' started by Lostwanderer1, Jul 15, 2019.

  1. Lostwanderer1

    Lostwanderer1

    Joined:
    Mar 11, 2019
    Posts:
    9
    Hi, I am working on drag and shoot mechanics. I have created the mechanics, but i am not able to figure out the code for line renderer which rotates in opposite direction when dragged in 3D. (ex. buca, available on ios). I need the line renderer to rotate in direction of launch as i move my finger.

    Here the code for drag and shoot-

    Vector3 startPos, endPos;
    public Vector3 direction;
    Rigidbody myRigidbody;
    public float Power = 4f;
    public int hits=0;
    private LineRenderer rend;
    //public Sprite dir;

    Vector3 cam = new Vector3(0,0,180);

    void Start()
    {
    myRigidbody = GetComponent<Rigidbody> ();
    //rend=GetComponent<LineRenderer>();

    if(rend==null){

    rend=gameObject.AddComponent<LineRenderer>();
    }
    rend.positionCount=2;

    }

    void OnMouseDown (){
    if (Input.GetMouseButtonDown (0)) {
    startPos = Input.mousePosition;
    //startPos=Quaternion.Euler(0f, 0.0f, 90f);
    myRigidbody.isKinematic=true;
    rend.SetPosition(0,startPos);
    rend.SetWidth(0.25f,0.45f);
    rend.useWorldSpace=true;

    }
    }
    void OnMouseUp (){
    if (Input.GetMouseButtonUp (0)) {
    hits = hits + 1;
    myRigidbody.isKinematic = false;
    endPos = Input.mousePosition;
    direction = startPos - endPos;
    direction = Quaternion.Euler(90.0f, 0.0f, 0.0f) * direction;
    myRigidbody.AddForce (direction * Power);
    }
    }
    }


    Can you help with that?