Jump to content
  • Advertisement
Sign in to follow this  
LiziPizi

Bow Mechanics Line Renderer Problem

This topic is 584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


public class Bow : MonoBehaviour {
 
    public GameObject line;
    public float maxStretchDistance;
    float currentStretchDistance;
    Vector3 mousePosition;
    Vector3 bowDirection;
    Vector3 mousePositionInRelationToLineCenter;
 
    void Update() {
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) + Vector3.forward * 10;
        mousePositionInRelationToLineCenter = mousePosition - line.transform.position;
    }
 
    void OnMouseDrag()
    {
        bowDirection = -(mousePosition - this.transform.position);
        bowDirection.Normalize();
        transform.right = bowDirection;
        line.GetComponent<LineRenderer>().SetPosition(1, mousePositionInRelationToLineCenter);
    }
 
    void OnMouseUp()
    {
        line.GetComponent<LineRenderer>().SetPosition(1, Vector3.zero);
    }
 
}
 

 

 

[attachment=34889:Bow.gif]
 

As you can see the problem is with the line renderer.
I have a variable that save the mouse position in relation to the line center and I am trying to use this variable to move the line center.

I don't know what is wrong in my code, help me please.

 

Thank you in advance

Edited by Elgauly

Share this post


Link to post
Share on other sites
Advertisement
How is your hierarchy setup? It looks like the line renderer is a child game object of the bow. If that is the case the rotation of the bow seems to be the problem. If that is the case, try setting the line renderer point in local coordinates
line.GetComponent().SetPosition(1, new Vector3(-mousePositionInRelationToLineCenter.magnitude, 0.0f, 0.0f));

Share this post


Link to post
Share on other sites

How is your hierarchy setup? It looks like the line renderer is a child game object of the bow. If that is the case the rotation of the bow seems to be the problem. If that is the case, try setting the line renderer point in local coordinates

line.GetComponent().SetPosition(1, new Vector3(-mousePositionInRelationToLineCenter.magnitude, 0.0f, 0.0f));

 

YESSS!!!! That worked! Thank you so much!! I totally forgot about the magnitude value

Edited by Elgauly

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!