Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Path drawing
#1
Hello everyone,

I'm a new curvy user and I need some help.
I need to implement a path drawing system pretty much identical to the one shown in the demo "04_PaintSpline", but I need it to work in 3D space (I noticed that one is currently working within UI), also I need the path to be followed by many gameObjects, not just one.
I was wondering if anyone has some kind of solution to this problem, I should add that I am not a programmer.
Thanks in advance!
Reply
#2
Hi,
The PaintSpline script used in that scene does two things:
- Painting the spline
- Modifying the settings of the given controller, mainly setting the controller at the spline's start when a new spline is painted.
You can find bellow a modified version of that script. In this version, the script does only the painting. No controller is involved. This way you can use the script in your scene and set the controllers the way you like.
Code:
// =====================================================================
// Copyright 2013-2018 ToolBuddy
// All rights reserved
//
// http://www.toolbuddy.net
// =====================================================================

using UnityEngine;
using System.Collections;
using FluffyUnderware.Curvy;
using FluffyUnderware.DevTools.Extensions;
using FluffyUnderware.Curvy.Controllers;
using UnityEngine.UI;

/*
* In this example we let the user draw a spline on screen!
*
*/
namespace FluffyUnderware.Curvy.Examples
{
   public class PaintSpline : MonoBehaviour
   {
       public float StepDistance = 30;
       public Text InfoText;

       CurvySpline mSpline;
       Vector2 mLastControlPointPos;
       bool mResetSpline = true;

       void Awake()
       {
           // for this example we assume the component is attached to a GameObject holding a spline
           mSpline = GetComponent<CurvySpline>();
       }

       void OnGUI()
       {
           // before using the spline, ensure it's initialized and the Controller is available
           if (mSpline == null || !mSpline.IsInitialized)
               return;

           var e = Event.current;
           switch (e.type)
           {
               case EventType.MouseDrag:
                   // Start a new line?
                   if (mResetSpline)
                   {
                       mSpline.Clear(); // delete all Control Points
                       addCP(e.mousePosition); // add the first Control Point
                       mLastControlPointPos = e.mousePosition; // Store current mouse position
                       mResetSpline = false;
                   }
                   else
                   {
                       // only create a new Control Point if the minimum distance is reached
                       float dist = (e.mousePosition - mLastControlPointPos).magnitude;
                       if (dist >= StepDistance)
                       {
                           mLastControlPointPos = e.mousePosition;
                           addCP(e.mousePosition);
                       }
                   }

                   break;
               case EventType.MouseUp:
                   mResetSpline = true;

                   break;
           }
       }

       // Add a Control Point and set it's position
       CurvySplineSegment addCP(Vector3 mousePos)
       {
           Vector3 p = Camera.main.ScreenToWorldPoint(mousePos);
           p.y *= -1; // flip Y to get the correct world position
           p.z += 100; //To move further than camera's plane. The value 100 comes from the Canvas' plane distance
           var cp = mSpline.InsertAfter(null, p, false);

           InfoText.text = "Control Points: " + mSpline.ControlPointCount.ToString(); // set info text

           return cp;
       }
   }
}

Now about setting the controllers, you can do that though the inspector of course, but if you need some logic without coding, then you can use PlayMaker (which is an asset you should consider anyways if you want to make game logic without coding), it has a Curvy addon that allows you to setup controllers.

Now about painting a spline in 3D: the example scene's painting spline basically detects the position of mouse cursor in screen space, and creates the spline's control points accordingly. To do 3D painting is not just a coding problem, it's a game design one: how will you translate a mouse movement (2D) to 3D space? This isn't some easy question I can just send you a simple script to do, this is real feature coding that needs back and forth discussion between coder and game designer. I don't really see a solution to this problem that would not require a coder to be in your team.

Have a nice day
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#3
(04-30-2020, 04:14 PM)_Aka_ Wrote: Hi,
The PaintSpline script used in that scene does two things:
- Painting the spline
- Modifying the settings of the given controller, mainly setting the controller at the spline's start when a new spline is painted.
You can find bellow a modified version of that script. In this version, the script does only the painting. No controller is involved. This way you can use the script in your scene and set the controllers the way you like.
Code:
// =====================================================================
// Copyright 2013-2018 ToolBuddy
// All rights reserved
//
// http://www.toolbuddy.net
// =====================================================================

using UnityEngine;
using System.Collections;
using FluffyUnderware.Curvy;
using FluffyUnderware.DevTools.Extensions;
using FluffyUnderware.Curvy.Controllers;
using UnityEngine.UI;

/*
* In this example we let the user draw a spline on screen!
*
*/
namespace FluffyUnderware.Curvy.Examples
{
   public class PaintSpline : MonoBehaviour
   {
       public float StepDistance = 30;
       public Text InfoText;

       CurvySpline mSpline;
       Vector2 mLastControlPointPos;
       bool mResetSpline = true;

       void Awake()
       {
           // for this example we assume the component is attached to a GameObject holding a spline
           mSpline = GetComponent<CurvySpline>();
       }

       void OnGUI()
       {
           // before using the spline, ensure it's initialized and the Controller is available
           if (mSpline == null || !mSpline.IsInitialized)
               return;

           var e = Event.current;
           switch (e.type)
           {
               case EventType.MouseDrag:
                   // Start a new line?
                   if (mResetSpline)
                   {
                       mSpline.Clear(); // delete all Control Points
                       addCP(e.mousePosition); // add the first Control Point
                       mLastControlPointPos = e.mousePosition; // Store current mouse position
                       mResetSpline = false;
                   }
                   else
                   {
                       // only create a new Control Point if the minimum distance is reached
                       float dist = (e.mousePosition - mLastControlPointPos).magnitude;
                       if (dist >= StepDistance)
                       {
                           mLastControlPointPos = e.mousePosition;
                           addCP(e.mousePosition);
                       }
                   }

                   break;
               case EventType.MouseUp:
                   mResetSpline = true;

                   break;
           }
       }

       // Add a Control Point and set it's position
       CurvySplineSegment addCP(Vector3 mousePos)
       {
           Vector3 p = Camera.main.ScreenToWorldPoint(mousePos);
           p.y *= -1; // flip Y to get the correct world position
           p.z += 100; //To move further than camera's plane. The value 100 comes from the Canvas' plane distance
           var cp = mSpline.InsertAfter(null, p, false);

           InfoText.text = "Control Points: " + mSpline.ControlPointCount.ToString(); // set info text

           return cp;
       }
   }
}

Now about setting the controllers, you can do that though the inspector of course, but if you need some logic without coding, then you can use PlayMaker (which is an asset you should consider anyways if you want to make game logic without coding), it has a Curvy addon that allows you to setup controllers.

Now about painting a spline in 3D: the example scene's painting spline basically detects the position of mouse cursor in screen space, and creates the spline's control points accordingly. To do 3D painting is not just a coding problem, it's a game design one: how will you translate a mouse movement (2D) to 3D space? This isn't some easy question I can just send you a simple script to do, this is real feature coding that needs back and forth discussion between coder and game designer. I don't really see a solution to this problem that would not require a coder to be in your team.

Have a nice day

Hi there, thank you very much for answering.
When I say I want to do it in a 3D Space, I mean that I want the tracing to take place on a "ground" of my selection, I add this image as an example, there I am placing a cube mesh which would work as the ground, and I would like the path tracing to occur on that mesh since there will be other things on there as well, things like obstacle or enemies, and I need my characters to walk a path traced on that mesh so that they can interact with things, however with the included example the path tracing happens inside de UI, away from the mesh.
Also when I try to use this code with a perspective camera, the path is not traced correctly, all the nodes get piled on top of each other regardless of what kind of trace I make.


Attached Files Thumbnail(s)
   
Reply
#4
Use a Curvy Generator to conform the created spline on a collider, then use a CG Path Controller to make your object follow that path.
Scene 20_CGPaths is an example of usage of a CG Path Controller
Scene 24_CGConformPath is an example of how to conform a path on a collider

About the mouse movement detection for a perspective camera, this isn't a Curvy problem. Here is the solution:
https://stackoverflow.com/questions/28125428/screentoworldpoint-with-perspective-cameras
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Wink Train carriage with 2 bogies following a path arcadeperfect 9 27 08-25-2023, 02:56 PM
Last Post: arcadeperfect
  Volume Spots not even when "drawing" spline too fast at runtime. _RicO 9 11 08-11-2023, 09:22 AM
Last Post: _Aka_
  Odd behavior when drawing spline ricke 11 29 05-15-2023, 12:28 PM
Last Post: _Aka_
  Generator does not exactly follow the path F.A.L. 3 4 04-24-2023, 03:49 PM
Last Post: _Aka_

Forum Jump: