Advertisement

Bow Mechanics Line Renderer Problem

Started by February 13, 2017 04:01 PM
1 comment, last by LiziPizi 7 years, 10 months ago


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

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));
My current game project Platform RPG
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));

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

This topic is closed to new replies.

Advertisement