playMaker

Author Topic: Draw Line 1.1 [last update: 18.05.2015]  (Read 10780 times)

Andrew_Raphael_Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Draw Line 1.1 [last update: 18.05.2015]
« on: May 20, 2013, 09:01:05 AM »
DrawLine.cs

 Draws line between two objects or points using LineRenderer.

Quote from: version history
1.01 - released
1.02 - <Align In Space Mode Self> now also works with only one target object at hand.
1.021 - bugfix
1.1 - Proper maintenance update. No features added but expect this action to be more performance friendly and a lot more robust.




Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

// DrawLine.cs version 1.1
// http://hutonggames.com/playmakerforum/index.php?topic=3943.0
// custom action by andrew r lukasik gmail com

using UnityEngine;

namespace HutongGames.PlayMaker.Actions {
[ActionCategory( ActionCategory.Effects )]
[Tooltip( "Draws line between two objects or points using LineRenderer." )]
public class DrawLine : FsmStateAction {

[Tooltip( "Line start." )]
public FsmGameObject startObject;
[Tooltip( "Line end." )]
public FsmGameObject endObject;
[Tooltip( "Start offset or world position." )]
public FsmVector3 startOffset;
[Tooltip( "End offset or world position." )]
public FsmVector3 endOffset;
[Tooltip( "Space." )]
public Space spaceMode;
[Tooltip( "When Space Mode Self is selected Start Object and End Object will execute LookAt each other to align their Z axes" )]
public FsmBool alignInSpaceModeSelf;

[Tooltip( "Material" )]
public FsmMaterial material;
[Tooltip( "Shadow Casting Mode" )]
public UnityEngine.Rendering.ShadowCastingMode shadowCastingMode;
[Tooltip( "Receive Shadows" )]
public FsmBool receiveShadows;

[Tooltip( "start width" )]
public FsmFloat startWidth;
[Tooltip( "end width" )]
public FsmFloat endWidth;

[Tooltip( "Start Color" )]
public FsmColor startColor;
[Tooltip( "End Color" )]
public FsmColor endColor;

[UIHint( UIHint.Variable )]
[Tooltip( "Store GameObject with LineRenderer." )]
public FsmGameObject storeGameobject;

[Tooltip( "Updates positions every frame" )]
public FsmBool everyFrame;
[Tooltip( "Destroy drawn line on exit" )]
public FsmBool destroyOnExit;

//
private FsmGameObject goLineRenderer;
private LineRenderer _lineRenderer;
private Transform _lineRendererTransform;
//
private FsmGameObject localStart;
private Transform _localStartTransform;
//
private FsmGameObject localEnd;
private Transform _localEndTransform;
//
private FsmGameObject startObjectPosition;
private Transform _startObjectPositionTransform;
//
private FsmGameObject endObjectPosition;
private Transform _endObjectPositionTransform;
//
private Transform _startObjectTransform;
private Transform _endObjectTransform;
//

// RESET //
public override void Reset () {
//
startObject = null;
_startObjectTransform = null;
//
endObject = null;
_endObjectTransform = null;
//
alignInSpaceModeSelf = false;
startOffset = new FsmVector3 { UseVariable = true };
endOffset = new FsmVector3 { UseVariable = true };
spaceMode = Space.World;
material = null;
shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
receiveShadows = false;
startWidth = 1.0F;
endWidth = 1.0F;
startColor = Color.white;
endColor = Color.white;
everyFrame = true;
destroyOnExit = false;
//
goLineRenderer = null;
storeGameobject = null;
_lineRenderer = null;
_lineRendererTransform = null;
//
_localStartTransform = null;
_localEndTransform = null;
_startObjectPositionTransform = null;
_endObjectPositionTransform = null;

}

// ON ENTER //
public override void OnEnter () {
if( goLineRenderer!=null ) {
Object.Destroy( goLineRenderer.Value );
}
//
goLineRenderer = new GameObject( "FSM draw line ("+this.Name+")" );
storeGameobject.Value = goLineRenderer.Value;
_lineRendererTransform = goLineRenderer.Value.transform;
//
_lineRenderer = goLineRenderer.Value.AddComponent<LineRenderer>();
_lineRenderer.material = material.Value;
//
_lineRenderer.shadowCastingMode = shadowCastingMode;
_lineRenderer.receiveShadows = receiveShadows.Value;
_lineRenderer.SetVertexCount( 2 );
_lineRenderer.SetWidth( startWidth.Value , endWidth.Value );
DoUpdatePositions();
if( !everyFrame.Value ) {
Finish();
}
}

// ON UPDATE //
public override void OnUpdate () {
if( everyFrame.Value ) {
DoUpdatePositions();
_lineRenderer.SetWidth( startWidth.Value , endWidth.Value );
_lineRenderer.SetColors( startColor.Value , endColor.Value );
_lineRenderer.shadowCastingMode = shadowCastingMode;
_lineRenderer.receiveShadows = receiveShadows.Value;
_lineRenderer.material = material.Value;
}
}

// ON EXIT //
public override void OnExit () {
if( destroyOnExit.Value && goLineRenderer!=null ) {
Object.Destroy( goLineRenderer.Value );
}
}

// DO UPDATE POSITIONS //
void DoUpdatePositions () {
//
if( spaceMode==Space.Self ) MakeSureSpaceSelfHelpersAreOk();
//calculate Start pos:
if( startObject.Value!=null ) {
_startObjectTransform = startObject.Value.transform;
if( spaceMode==Space.World ) {
_lineRenderer.SetPosition( 0 , (_startObjectTransform.position+startOffset.Value) );
}
else {
if( startObject.Value!=null ) {
_startObjectPositionTransform.position = _startObjectTransform.position;
if( alignInSpaceModeSelf.Value ) {
_startObjectPositionTransform.LookAt( _endObjectPositionTransform );
_localStartTransform.localPosition = startOffset.Value;
}
else {
_startObjectPositionTransform.rotation = _startObjectTransform.rotation;
_localStartTransform.localPosition = startOffset.Value;
}
_lineRenderer.SetPosition( 0 , _localStartTransform.position );
}
else {
_startObjectPositionTransform.position = startOffset.Value;
if( alignInSpaceModeSelf.Value ) {
_startObjectPositionTransform.LookAt( _endObjectPositionTransform );
_localStartTransform.localPosition = startOffset.Value;
}
else {
_localStartTransform.localPosition = startOffset.Value;
}
_lineRenderer.SetPosition( 0 , _localStartTransform.position );
}
}
}
else {
_lineRenderer.SetPosition( 0 , startOffset.Value );
}

//calculate End pos:
if( endObject.Value!=null ) {
_endObjectTransform = endObject.Value.transform;
if( spaceMode==Space.World ) {
_lineRenderer.SetPosition( 1 , (_endObjectTransform.position+endOffset.Value) );
}
else {
if( endObject.Value!=null ) {
_endObjectPositionTransform.position = _endObjectTransform.position;
if( alignInSpaceModeSelf.Value ) {
_endObjectPositionTransform.LookAt( _startObjectPositionTransform );
_localEndTransform.localPosition = endOffset.Value;
}
else {
_endObjectPositionTransform.rotation = _endObjectTransform.rotation;
_localEndTransform.localPosition = endOffset.Value;
}
_lineRenderer.SetPosition( 1 , _localEndTransform.position );
}
else {
_endObjectPositionTransform.position = endOffset.Value;
if( alignInSpaceModeSelf.Value ) {
_endObjectPositionTransform.LookAt( _startObjectPositionTransform );
_localEndTransform.localPosition = endOffset.Value;
}
else {
_localEndTransform.localPosition = endOffset.Value;
}
_lineRenderer.SetPosition( 1 , _localEndTransform.position );
}
}
}
else {
_lineRenderer.SetPosition( 1 , endOffset.Value );
}
}

//
private void MakeSureSpaceSelfHelpersAreOk () {
if( startObjectPosition==null ) {
startObjectPosition = new GameObject( "tracking startObject" );
_startObjectPositionTransform = startObjectPosition.Value.transform;
_startObjectPositionTransform.parent = _lineRendererTransform;
}
else {
_startObjectPositionTransform.parent = _lineRendererTransform;
}
//
if( localStart==null ) {
localStart = new GameObject( "local start" );
_localStartTransform = localStart.Value.transform;
_localStartTransform.parent = _startObjectPositionTransform;
_localStartTransform.localEulerAngles = new Vector3( 0 , 0 , 0 );
}
else {
_localStartTransform.parent = _startObjectPositionTransform;
}
//
if( endObjectPosition==null ) {
endObjectPosition = new GameObject( "tracking endObject" );
_endObjectPositionTransform = endObjectPosition.Value.transform;
_endObjectPositionTransform.parent = _lineRendererTransform;
}
else {
_endObjectPositionTransform.parent = _lineRendererTransform;
}
//
if( localEnd==null ) {
localEnd = new GameObject( "local end" );
_localEndTransform = localEnd.Value.transform;
_localEndTransform.parent = _endObjectPositionTransform;
_localEndTransform.localEulerAngles = new Vector3( 0 , 0 , 0 );
}
else {
_localEndTransform.parent = _endObjectPositionTransform;
}
//
}

}
}
« Last Edit: May 19, 2015, 01:22:02 AM by Andrew_Raphael_Lukasik »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2484
  • Yup.
    • View Profile
    • Cleverous
Re: Draw Line
« Reply #1 on: May 20, 2013, 09:38:55 AM »
Wow, that's one seriously elaborate line renderer!

Very cool, thanks. I'm going to use this a lot for testing and debugging.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12342
  • Official Playmaker Support
    • View Profile
Re: Draw Line
« Reply #2 on: May 20, 2013, 09:42:38 AM »
Hi,

 Why not using Vectrosity? maybe I am missing something with line renderers...? I guess it implements better as "Physical" object in the scene than vectrosity right?

where is your struggle to expand this to a poly? I think you could inspire from Vectrosity here too cause It features an editor script that parse a poly and extracts all the edges to build the mesh as a wireframe.

 Bye,

 Jean

Andrew_Raphael_Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Re: Draw Line
« Reply #3 on: May 20, 2013, 10:03:19 AM »
@Jeanfabre
I never used Vectrosity and I believe it's great for complex things. But when I want just to experiment and draw a line between things I would like to have something built-in and unity-native.

Well this was just intense improvisation, trail and error till it worked. I know that it probably should use some kind of array but I'm not yet sure if I will be able to implement it. In any case I will definitely check your Vectrosity's actions to research that.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2484
  • Yup.
    • View Profile
    • Cleverous
Re: Draw Line
« Reply #4 on: May 20, 2013, 10:09:15 AM »
Personally, I just wanted to be able to draw some lines for debugging and experimenting. Vectrosity is a paid asset and more elaborate than I need.

Andrew_Raphael_Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Re: Draw Line
« Reply #5 on: May 20, 2013, 10:29:57 AM »
@Lane I agree totally. I don't want to import/buy Vectrosity when I just want to draw single raycast (shot) trail for example.

Andrew_Raphael_Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Re: Draw Line
« Reply #6 on: May 21, 2013, 11:43:30 AM »
Quote from: update note
1.02 - <Align In Space Mode Self> now also works with only one target object at hand.

Drakon

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Re: Draw Line
« Reply #7 on: May 23, 2013, 07:31:57 AM »
Super! Works really well!

Andrew_Raphael_Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Draw Line 1.021 (bugfix)
« Reply #8 on: July 08, 2013, 11:44:15 AM »
Quote from: update note
1.021 - Bugfix in OnExit(), "if (destroyOnExit.Value)" changed to "if (destroyOnExit.Value && goLineRenderer != null)".
Line 137 was giving me errors lately so this simple addition tests if Line Renderer object exist when destroying it OnExit.

aLDime

  • Playmaker Newbie
  • *
  • Posts: 24
    • View Profile
    • http://aldimegame.com/
Re: Draw Line
« Reply #9 on: October 26, 2013, 07:14:28 AM »
Thank you very much, Andrew!
Quickly and simply made ​​an electric discharge between 2 objects!

henrygordon

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: Draw Line
« Reply #10 on: December 20, 2014, 12:08:37 AM »
This has been an old post, yet this resolves my concern, thanks, still working well. Anyway, soon I will try to use this on upcoming game that I am doing, you can soon check it at http://www.iphysicsgames.com/top-rated. Thanks,

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • View Profile
    • Pixel Life - portfolio
Re: Draw Line
« Reply #11 on: February 02, 2015, 04:22:55 PM »
This is exactly what I needed and still works ^^ thanks! just had to remember to set the material, otherwise it is invisible!

Great work, thanks :)
Remember, you don't fail unless you give up trying to succeed!

FritsLyn

  • Full Member
  • ***
  • Posts: 191
    • View Profile
Re: Draw Line
« Reply #12 on: April 03, 2015, 02:05:06 AM »
Jean: Bump: This should IMO be in the Ecosystem (Or straight up native PlayMaker Package)

Andrew: Very, very nice work, works extremely well!!

Thanks!

mitchelq

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: Draw Line
« Reply #13 on: May 17, 2015, 04:28:25 AM »
I was looking for this!

Thanks, well done!

Works in playmaker 1.8

Andrew_Raphael_Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Re: Draw Line 1.1 [last update: 18.05.2015]
« Reply #14 on: May 18, 2015, 05:54:23 AM »
Quote from: version history
1.1 - Proper maintenance update. No features added but expect this action to be more performance friendly and a lot more robust.

Hi!
 I noticed that you guys are still using this action so I made a proper maintenance update 1.1 today! :)

ps: post bugs reports here when encountered any