drawing a line following an object in unity c# - c#

Hello I am using this code to draw a line that follows an object
Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z),
new Vector3(prevPosition_x, prevPosition_y, prevPosition_z), Color.red,1.0f);
prevPosition_x = gameObject.transform.position.x;
prevPosition_y = gameObject.transform.position.y;
prevPosition_z = gameObject.transform.position.z;
However this doesnt draw in a build. Does anyone know how to do this without using the built in line or trail renderers. I know it can be done with gl but dont have a clue how to do this.
Any help is appreciated

I do believe Debug.DrawLine only works in the Scene Editor.
There are several ways you can draw lines, GL is definitely one of them. There is also Vectrosity.
Alas, you can do it yourself with some effort by generating your own line mesh and moving the vertices around.
Hope it helps.

As Xerosigma said, Debug.DrawLine only works in Editor, because it's purpose is to debug scripts, not provide visualization for users. Another problem is that this way, you'll only create lines that will render one frame, and previous to current position will only differ by one frame. You, on the other hand, probably want the trail not to be dependent on framerate.
I think that you'll find TrailRenderer component in Unity fitting for your situation. I've used it in one of my hobbie projects, and it works just fine.

I think that's not the best way, but should work
http://rockonflash.wordpress.com/2010/04/17/how-to-do-lasers-in-unity3d/
Debug.DrawLine only works on Scene Window

Related

Unity 2D OnTriggerEnter2D() will not repeat in an entirely different script

I am creating a game where there are 9 chunks in 1 2D map and you must "unlock" every chunk by triggering a collider on the edge in a specific order to beat the game. I have managed to switch the scene from 0 to 1 using this code on a rectangle with a trigger collider:
void OnTriggerStay2D(Collider2D other)
{
SceneManager.LoadScene(1);
Destroy(this.gameObject);
}
The code above works fine, but recently I created the third chunk and copied the code to a new script, changed the name, and switched SceneManager.LoadScene(1) to SceneManager.LoadScene(2), then added it to a new rectangle with a trigger collider. The second chunk loads in when triggered, but the third does not. Is there any way to fix this?
I don't have enough reputation yet to comment, so I'll propose a few possible problems/solutions. I assume your chunks are scenes, so you're loading in several scenes at once? Some things I can think of that might be causing the second chunk not to load in:
Did you add the scene to the build? If not, you can do so in the build settings.
You can try to debug OnTriggerStay2D() in the second chunk trigger. Is it being triggered? If not, you might not have added the trigger to the rectangle correctly: did you set up the Rigidbody2D?
If the above points do not fix your issue, I'd suggest adding more of your code (of the chunk loading scripts) and some screenshots (of the inspector in both chunk loaders and of the colliders you made) to your question. Hope this helps.

Objects having same distance/radius from the center (camera) in Unity 3D

It's a 360 Video application on Unity 3D.
I want to place several objects around the camera (which has a fixed position), but I need this objects to have the same distance (same radius) from the Camera (which is the center). How can I do this? Either on Editor or by code.
I have been manually displacing objects around the camera, by dragging them by arrow tool. But it's as inaccurate as a pain to do. :)
Any light on this would help me a lot! Not only me, but anyone working with 360 videos in Unity.
Thank you all in advance!
To solve your problem, an easy solution would be to add a child "child_of_camera" to your camera and then add a child "child_of_child" to the "child_of_camera".
Now that you've done this, move the "child_of_child" away to however far from the camera you'd like it to be. After this, apply the random rotation you'd like to "child_of_camera".
Duplicate "child_of_camera" to however many objects you'd like on screen and then rotate them all to your preference.
Now, when you're moving around the camera, these children will follow along with the camera.
If you're looking so that the rotation of the camera doesn't affect the objects there are two ways you can handle this:
Place camera and "child_of_camera" (this name would now be misleading and should be renamed) under an empty GameObject and move "empty_GO" on the X,Y,Z axis instead of the camera.
or
Create a quick script to attach onto "child_of_camera" so that it always sets the "child_of_camera"s world space rotation to Vector3.zero.
As I stated in the comments, this solution is most likely not the optimal way to fix your problem but it is definitely a very simple solution that is easy to understand and implement. I hope it helps.

How can I draw lines on the edges of objects?

I'm making a unity SteamVR game.
I want to achieve an unlit art style reminiscent of Antichamber.
I want to draw lines on the edges of objects, but I couldn't find an online solution that was not outdated and that worked.
Here is a picture from antichamber.
You can see that every object has a line across all of it's edges.
How can I achieve this effect?
Yo can do this with Unity's "Edge Detect Effect Normals" which is part of Unity's image effects and is included in their Standard Assets. You can get it here.
There is also a new image effect for Unity called "Post Processing Stack" which is really faster than the one I linked above and you can get that here. Edge Detect is not included in the new "Post Processing Stack" image effects but if you decide to use the new Post Processing Stack, you can get the ported version of "Edge Detect Effect Normals" here.
Any one of these effects can draw lines on edges of objects.
I want to draw lines on the edges of objects
This free asset promises to do just that.
"Supports outlines with variable thickness, color, that work in Editor, Ortho and Perspective cameras."
Source: https://assetstore.unity.com/packages/vfx/shaders/toon-shader-free-21288

C# - I am trying to create a grid lock game and I need your assistance

Since I don't have 10 reputation to post an image, I am going to leave a link of the game picture. http://thinkfun.com/mathcounts/play-rush-hour
This is what I want to create. A game where a rectangle must pass through other rectangles through the hole to win the game. The main rectangle can only move forward and other rectangles can move either right, left, up or down depending on their position. You must move the other rectangles in a way that they could make a way for the main rectangle to pass.
Here is my plan how I am going to create this game:
Create all the forms, import all the images and graphics to get ready to work with them.
Write code for 1 rectangle that would be able to move around.
Write an algorithm that wouldn't allow for rectangles to go through each other.
Make a winning hole.
Code other rectangles, make a full game.
Make fancy animations.
Import statistics : score, move counter and etc.
Here are my questions that I wanted to ask before trying to develop this game.
(Oh, and by the way, I am using Visual Studio C# form )
I assume that these rectangles are not going to be buttons, then what kind of a "button" (or whatever you call those extra features ) should I use?
How do I make a skin for those rectangles? So I can make it look like wood and make a nice skin for the main rectangle.
These are all my questions so far. I would like to know how to overcome those problems and I would like to hear some extra tips. Thank you very much for your time!
Use Graphics to draw your rectangles. Else make an object to hold info for it and you can import an Image as your "skin".
See number 1.
First off: Do you want to create a game or learn winForms? If the answer is create a game. Chose a Game Engine (Unity is nice, and uses c#) and work with that instead.
If you want to learn winForms you'd probably want a PictureBox, then add your code your code in OnMouseUp, OnMouseDown, etc. Good luck :)

splatmap textures not applying in build in unity

im procedurally creating my heightmaps and declaring my terrain in the scripts
in the unity editor, my splatmaps work perfectly, however when i build my terrain is just plain black.
is there a setting that i may have missed when building that is required to make the splats work ?.
editor
http://answers.unity3d.com/storage/temp/31838-untitle2d.png
build
http://answers.unity3d.com/storage/temp/31839-untitled.png
i load them in using code with
SplatPrototype[] terrainTexture = new SplatPrototype[4];
terrainTexture[0] = new SplatPrototype();
terrainTexture[0].texture = (Texture2D)Resources.Load("stone");
terrainTexture[1] = new SplatPrototype();
terrainTexture[1].texture = (Texture2D)Resources.Load("sand");
terrainTexture[2] = new SplatPrototype();
terrainTexture[2].texture = (Texture2D)Resources.Load("grass");
terrainTexture[3] = new SplatPrototype();
terrainTexture[3].texture = (Texture2D)Resources.Load("snow");
edit
i attempted adding the textures to the code as variables draged in rather than using resource.load, also didnt work
edit
i found a work around which makes it work, but its unfortunate that i coudnt find a proper solution.
can anyone see something that could be the cause of the issue
http://
answers.unity3d.com/questions/171150/terrain-basemap-texture-through-code.html
You need to include an extra shader in your build. Go to edit -> project settings -> graphics. In the inspector increase the size. In the new slot select Nature/Terrain/Diffuse.
The reason you need to do this is that Unity will search for any assets that are referenced in the scene to decide what to include in the build and what to leave out. Since your terrain is built procedurally there is no terrain object and no terrain material present, and the required shader is not included. By following the above steps you basically tell Unity to include it anyway because you know you are going to need it.
I would add in a few things to the above answer:
There is a prototypes refresh, called TerrainData.RefreshPrototypes() that is supposed to refresh all available prototypes, including the detail textures and trees. Since the ground textures are part of the prototypes listing, this will help unity update the new textures for the terrain. (And, it also works inside of the Editor.)
Also, others have found that when they change the textures' data (ie, changing the pixel data) that it causes unity to update the materials used for the terrain. They made a blank terrain, assigned blank base textures to it, and then at runtime loaded up the height map, splat maps, and overwrote the textures. This caused unity to display the newly created terrains.
I hope that this helps out some, as most of the terrain asset is a highly undocumented area (and rather dysfunctional, too)... Good Luck!

Categories

Resources