Calculate an isosceles triangle given a base width and two vectors
I am trying to create a mobile game where you can draw an isosceles triangle by touching and dragging. I need to know what colliders intersect this isosceles triangle.
I was thinking about setting up a line renderer with a start width of some value and an end width of 0. but I cannot get a collider on this shape. I thought of calculating the shape of the triangle manually and use a polygon collider, but I can't get the maths correct.
Here is what I have so far:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RayCreator : MonoBehaviour
{
private Vector3 startPosition;
private TouchPhase previousTouchPhase = TouchPhase.Began;
private GameObject line;
private float lastTime = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touchCount == 1)
{
var touch = Input.GetTouch(0);
var camera = Camera.main;
if (touch.phase == TouchPhase.Moved)
{
Vector3 position = touch.position;
if (line != null)
{
var lineRenderer = line.GetComponent<LineRenderer>();
lineRenderer.SetPosition(0, (Vector2) camera.ScreenToWorldPoint(startPosition));
lineRenderer.SetPosition(1, (Vector2) camera.ScreenToWorldPoint(position));
lineRenderer.startWidth = 0.5f;
lineRenderer.endWidth = 0.0f;
lineRenderer.sortingOrder = 1;
line.SetActive(true);
}
}
if (touch.phase == TouchPhase.Began)
{
startPosition = touch.position;
line = new GameObject("ForceLine");
LineRenderer lineRenderer = line.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
lineRenderer.material.color = Color.red;
lineRenderer.positionCount = 2;
lineRenderer.useWorldSpace = true;
line.SetActive(false);
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
// Destroy(line);
line = null;
}
previousTouchPhase = touch.phase;
}
}
}
Your answer
Follow this Question
Related Questions
A line between two objects with conditions 0 Answers
Using LineRenderer, how can I draw a single path passing through more than two points? 1 Answer
How do you calculate the points for a predictive trajectory line? 0 Answers
How to make a surface glow when its hit by a laser. 0 Answers
UNITY 2D linerender wont show in 2d 0 Answers