- Home /
Line Direction
I am doing a minigolf game in 2D, and to control the force I have made a line with the LineRenderer, and when you hold down it points in which direction you are going to throw, and the force depending on how much you stretch the line, I have managed to do the mechanics of the impulse, but I don't get the ball to drive in the direction of the drag power line, can someone help me?
Answer by Ossi101 · Jul 24, 2019 at 07:22 PM
To get the proper direction for shooting the ball you need to subtract the end position of the line from the starting point of the line and normalize the vector to get the x and y directions. You then keep updating this value as you drag your mouse around. I made a prototype of a golf shooting mechanic so you can see the entire thing put together.
This script is placed on the game object that has your LineRenderer:
using UnityEngine;
public class ShotLine : MonoBehaviour
{
public Rigidbody2D ball = null;
public float force = 100.0f;
public bool isStartAtMouse = false;
public bool isLineVisible = false;
private LineRenderer line = null;
private Vector2 startPosition = Vector2.zero;
private Vector2 endPosition = Vector2.zero;
private Vector2 direction = Vector2.zero;
private float power = 0.0f;
private bool isShot = false;
private void Awake()
{
line = GetComponent<LineRenderer>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (isStartAtMouse == true)
{
startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
line.SetPosition(0, startPosition);
}
else
{
startPosition = ball.position;
line.SetPosition(0, startPosition);
}
}
if (Input.GetMouseButton(0))
{
endPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
line.SetPosition(1, endPosition);
direction = (endPosition - startPosition).normalized;
power = Vector2.Distance(startPosition, endPosition);
}
if (Input.GetMouseButtonUp(0))
{
if (isLineVisible == false)
{
line.SetPosition(0, Vector2.zero);
line.SetPosition(1, Vector2.zero);
}
isShot = true;
}
}
private void FixedUpdate()
{
if (isShot == true)
{
ball.AddForce(direction * (power * force));
isShot = false;
}
}
}
There's some extra stuff in there, like if you want to toggle the visualization of the shot staying on screen or not. Also if you want to start the shot from anywhere on screen and not have it locked to the ball while maintaining the proper shooting direction. This demonstrates that it doesn't matter where you click and drag on the screen so long as you normalize the two vectors.
As for the ball game object itself it just has a Rigidbody2D with gravity scale set to 0 and a Circle Collider2D.
And here's the result of this code: This is with the IsStartAtMouse option set to true:
I hope this helps! Good luck!
Your answer
![](https://koobas.hobune.stream/wayback/20220612205402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add random force/Rotation to bulletEject. 1 Answer
How do I mimic dragging and dropping a FBX to the hierarchy window with an editor script? C# 1 Answer
How do you spawn objects from UI ScrollView via drag? 0 Answers
LineRenderer not instantiating on iOS or Xcode, Works fine in editor and Unity Remote 0 Answers
Trajectory on mouse up 0 Answers