Currently I am creating a line using linerenderer This is based on user input via mouse or touch. However, I want to add collision physics to this line. Basically, I want the user to be able to draw a line with their finger and for the player game object to be able to walk on this line.
My idea was to use ray-cast however, I don’t know how to implement the code.
Thanks for the help!
Here is my current code: (in C#)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawLineMouse : MonoBehaviour
{
public Material newMaterialRef;
private LineRenderer line;
private bool isMousePressed;
private List<Vector3> pointsList;
private Vector3 mousePos;
public GameObject player;
public int lineNumber; // set in inspector, used to keep track of multiple gameObjects using same C# file
int counter = 1; // Keep track of how many times you're touching the screen and building a line.
// Structure for line points
struct myLine //I don't think I need this if I'm not using the intersect code. (but whatever)
{
public Vector3 StartPoint;
public Vector3 EndPoint;
};
// -----------------------------------
void Awake()
{
// Create line renderer component and set its property
line = gameObject.AddComponent<LineRenderer>();
line.material = new Material(newMaterialRef); // Shader.Find("Particles/Additive"
line.SetVertexCount(0);
line.SetWidth(0.5f,0.5f);
line.SetColors(Color.white, Color.white);
line.useWorldSpace = true;
isMousePressed = false;
pointsList = new List<Vector3>();
}
// -----------------------------------
void Update ()
{
//if (Input.touchCount > 0) {
// var touch = Input.GetTouch (0);
if (Input.GetKey (KeyCode.Z)) //delete all lines with button press
{
line.SetVertexCount (0);
pointsList.RemoveRange (0, pointsList.Count);
line.SetColors (Color.white, Color.white);
}
if (Input.GetMouseButtonUp (0)) { //every time the user touches screen (mouse) and releases counter goes up (user just create another line)
counter++;
}
if (counter == 11) { //if user makes so many lines, we go back to the original game object, this line (up to 10) will be redrawn.
counter = 1;
}
// If mouse button down, remove old line and set its color to green
if (counter == lineNumber) {
if (Input.GetMouseButtonDown (0) ) {
isMousePressed = true;
line.SetVertexCount (0);
pointsList.RemoveRange (0, pointsList.Count);
line.SetColors (Color.white, Color.white);
} else if (Input.GetMouseButtonUp (0) ) { //touch.phase == TouchPhase.Ended
isMousePressed = false;
}
// Drawing line when mouse is moving(presses)
if (isMousePressed) {
mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition); //changed from Input.mousePosition to touch.position
mousePos.z = 0;
if (!pointsList.Contains (mousePos)) {
pointsList.Add (mousePos);
line.SetVertexCount (pointsList.Count);
line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
//Instantiate (drawBox, mousePos, rotation.rotation);
//Instantiate (drawEdge, mousePos, rotation.rotation);
//if (isLineCollide () || isLineCollidedWithOtherObject()) { //don't end up using this.
//if( OnCollisionEnter() ){
if (isLineCollidedWithOtherObject()) {
isMousePressed = false;
line.SetColors (Color.red, Color.red);
}
}
}
}
} //end update
// -----------------------------------
// Following method checks is currentLine(line drawn by last two points) collided with line
// -----------------------------------
private bool isLineCollide()
{
return false; //temp to disable this part of the code
if (pointsList.Count < 2)
return false;
int TotalLines = pointsList.Count - 1;
myLine[] lines = new myLine[TotalLines];
if (TotalLines > 1)
{
for (int i=0; i<TotalLines; i++)
{
lines <em>.StartPoint = (Vector3)pointsList _; lines *.EndPoint = (Vector3)pointsList [i + 1]; }*_</em>
* }*
* for (int i=0; i<TotalLines-1; i++)*
* {*
* myLine currentLine;*
* currentLine.StartPoint = (Vector3)pointsList [pointsList.Count - 2];*
* currentLine.EndPoint = (Vector3)pointsList [pointsList.Count - 1];*
_ if (isLinesIntersect (lines , currentLine))
* return true;
}
return false;
}*_
* // -----------------------------------*
* // Following method checks whether given two points are same or not*
* // -----------------------------------*
* private bool checkPoints (Vector3 pointA, Vector3 pointB)*
* {*
* return (pointA.x == pointB.x && pointA.y == pointB.y);*
* }*
* // -----------------------------------*
* // Following method checks whether given two line intersect or not*
* // -----------------------------------*
* private bool isLinesIntersect (myLine L1, myLine L2)*
* {*
* if (checkPoints (L1.StartPoint, L2.StartPoint) || checkPoints (L1.StartPoint, L2.EndPoint) || checkPoints (L1.EndPoint, L2.StartPoint) || checkPoints (L1.EndPoint, L2.EndPoint))*
* return false;*
* return((Mathf.Max (L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min (L2.StartPoint.x, L2.EndPoint.x)) &&(Mathf.Max (L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min (L1.StartPoint.x, L1.EndPoint.x)) && (Mathf.Max (L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min (L2.StartPoint.y, L2.EndPoint.y)) &&(Mathf.Max (L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min (L1.StartPoint.y, L1.EndPoint.y)));*
* }*
* private bool isLineCollidedWithOtherObject()*
* {*
* RaycastHit hit;*
* Ray ray = Camera.main.ScreenPointToRay(Camera.main.WorldToScreenPoint(pointsList[pointsList.Count-1]));*
* if(Physics.Raycast(ray,out hit))*
* {*
* if(hit.collider)*
* return true;*
* }*
* return false;*
* }*
}//end public class
/*
if (Input.touchCount > 0) {
* var touch = Input.GetTouch (0);*
* Vector3 touchDeltaPosition = new Vector3 (Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0);*
* if (Input.GetTouch (0).phase == TouchPhase.Began) {*
* mousePos = Camera.main.ScreenToWorldPoint (touchDeltaPosition);*
*/