- Home /
Need Help To Instantiate A Line
Hey guys, I'm working on drawing a straight line with the mouse, it's working to a degree with a cylinder, but I need to be able to create multiple's of that object, not just have the one that disappears every time a new line is drawn.
So obviously the best solution is to Instantiate a Prefab of this cylinder, but I'm not sure how to go about it, I worked with instantiating things before, but this one is different and I've looked for similar problems but I haven't been able to solve this myself.
And help is greatly appreciated!
using UnityEngine;
using System.Collections;
public class Lines: MonoBehaviour
{
Vector3 pos1;
Vector3 pos2;
float objectHeight= 2.0f;
void Update ()
{
Time.timeScale = 0.2f;
if (Input.GetMouseButtonDown(0))
{
pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
pos1 = Camera.main.ScreenToWorldPoint(pos1);
pos2 = pos1;
}
if (Input.GetMouseButton(0))
{
pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
pos2 = Camera.main.ScreenToWorldPoint(pos2);
}
if (pos2 != pos1)
{
Vector3 v3= pos2 - pos1;
transform.position = pos1 + (v3) / 2.0f;
transform.localScale = new Vector3(transform.localScale.x, v3.magnitude/objectHeight, transform.localScale.z);
transform.rotation = Quaternion.FromToRotation(Vector2.up, v3);
}
}
}
This script is attached to a cylinder in the scene scaled at 0.08-1-0.08. Thanks in advanced!
Also, I'm trying to do this in a 2D environment, does anyone know how I can do this without the z direction being used? In otherwords, I don't want anything to be able to move in the z direction, just x and y across the screen.
Why not just use some of the built in line drawing tools, like Gizmos or GL?
@$$anonymous$$evLoughrey - Gizmos is Editor only. GL will only draw a one pixel line. LineRenderers could be used. Or you could dynamically build a mesh of quads for each line as a more efficient solution than this one.
@$$anonymous$$evLoughrey - robertbu is right, and I've researched LineRenders, but I'm pretty sure that's for graphical operations, like making a drawing game, but I need to use my lines for physics based applications, thanks for the input though, I appreciate it!
Answer by robertbu · May 22, 2014 at 03:58 PM
I believe the code above is based on code I posted awhile back. Here is a changed version that uses a prefab. I tested it with a cylinder prefab with perspective camera and a scale of (0.01, 0.0, 0.01) and a orthographic camera with a prefab cylinder having scale (0.1, 0.0, 0.1).
using UnityEngine;
using System.Collections;
public class Lines: MonoBehaviour
{
public GameObject prefab;
Vector3 pos1;
Vector3 pos2;
float objectHeight= 2.0f;
Transform current = null;
void Update ()
{
Time.timeScale = 0.2f;
if (Input.GetMouseButtonDown(0))
{
pos1 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
pos1 = Camera.main.ScreenToWorldPoint(pos1);
pos2 = pos1;
current = (Instantiate(prefab, pos1, Quaternion.identity) as GameObject).transform;
}
if (Input.GetMouseButton(0))
{
pos2 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5f);
pos2 = Camera.main.ScreenToWorldPoint(pos2);
}
if (current != null && pos2 != pos1)
{
Vector3 v3 = pos2 - pos1;
current.position = pos1 + (v3) / 2.0f;
current.localScale = new Vector3(current.localScale.x, v3.magnitude/objectHeight, current.localScale.z);
current.rotation = Quaternion.FromToRotation(Vector2.up, v3);
}
if (Input.GetMouseButtonUp (0)) {
current = null;
}
}
}
Note I do not understand this line in your question:
how I can do this without the z direction being used?
Awesome, thank you Robert, yes you're absolutely right, I did some looking around for lines and multiples of your posts about lines showed up, so I borrowed it and I had been trying to work out how to instantiate it for a >= 1 week but to no avail.
about the z direction, I want to do this as a 2D based game, and I want to lock everything from entering the z position, so that the cylinder can collide with objects that fall and bounce them, but keep them from falling behind or in front of the cylinders.
Hey Robert, I just tested it, it works great thanks! But I was wondering if there was a way around the cylinder being vertical up the screen when it instantiates?
If you are seeing the cylinder, then you need to set the the localScale.y to 0.0 in the prefab. If you are talking about orientation, then for a cylinder you need vertical since that is the natural orientation of the cylinder. For objects that have a different natural orientation, these two lines would need to change:
current.localScale = new Vector3(current.localScale.x, v3.magnitude/objectHeight, current.localScale.z);
current.rotation = Quaternion.FromToRotation(Vector2.up, v3);
For example, if the game object you were using had a natural horizontal orientation, the lines would be:
current.localScale = new Vector3(v3.magnitude/objectHeight, current.localScale.y, current.localScale.z);
current.rotation = Quaternion.FromToRotation(Vector2.right, v3);
Ok thanks, is that to fix the prefab appearing vertically up the screen or the z thing? Because at the moment, it's actually working ok, the objects aren't falling off the cylinder.
Oh but one more thing, I've noticed that with the current method of instantiating the prefab, I've had to place the cylinder in the game first, and attaching this script ends up with the cylinder cloning clones, so if I have 6 lines in the scene, I end up with 6 x 2 x 6 cloned clones, which is really bad for performance obviously.
I've just spend the last hour trying to put your script into two scripts and try to instantiate from an empty game object, but I ended up with thousands of errors..
The above script should be attached to an empty game object. No need to attach it to a cylinder. You will end up with one cylinder per line. And the cylinder should be a prefab, so it will not exist in the scene at the start.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Collision between Cylinder and Cube 1 Answer
How to Physics.CheckCylinder? 2 Answers
Flip over an object (smooth transition) 3 Answers