Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by II_Spoon_II · Aug 06, 2018 at 03:12 PM · 2d3dscript.platformer2.5d

Is there a way I can add drawing mechanic like in "line rider" for a 3D game?

So, I am making a 3D game (it's actually a 2D platformer with 3D components), but the drawing script I am using works for 2D scenes only, any ideas how to implement it in 3D? This is the line prefab scirpt.

 using System.Linq;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Line : MonoBehaviour
 {
 
     public LineRenderer lineRenderer;
     public EdgeCollider2D edgeCol;
 
     List<Vector2> points;
 
     public void UpdateLine(Vector2 mousePos)
     {
         if (points == null)
         {
             points = new List<Vector2>();
             SetPoint(mousePos);
             return;
         }
 
         if (Vector2.Distance(points.Last(), mousePos) > .1f)
             SetPoint(mousePos);
     }
 
     void SetPoint(Vector2 point)
     {
         points.Add(point);
 
         lineRenderer.positionCount = points.Count;
         lineRenderer.SetPosition(points.Count - 1, point);
 
         if (points.Count > 1)
             edgeCol.points = points.ToArray();
     }
 
 }

and this is the line generator script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LineCreator : MonoBehaviour
 {
 
     public GameObject linePrefab;
 
     Line activeLine;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             GameObject lineGO = Instantiate(linePrefab);
             activeLine = lineGO.GetComponent<Line>();
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             activeLine = null;
         }
 
         if (activeLine != null)
         {
             Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             activeLine.UpdateLine(mousePos);
         }
 
     }
 
 }

Ps: The scripts are from Brackeys channel.

Thank you

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Aug 06, 2018 at 03:24 PM 1
Share

First of all, change Vector2 everywhere to Vector3 and the line will be in 3D. However, it you will also have to specify the third coordinate, z. I recommend to either set it to a multiple of Camera.main.forward so that you always draw in front of the camera, or use raycasting from the camera to get a more precise world point, and pass that:

 RaycastHit hit;
 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
     Vector3 pos = hit.point - Camera.main.forward * 0.01f; // moving point towards camera to avoid z-fighting
     activeLine.UpdateLine(pos);
 }
avatar image II_Spoon_II Harinezumi · Aug 12, 2018 at 11:51 AM 0
Share

Thank you for your reply, I tried what you said, I have 2 problems now:

First I don't know what to replace the edge collider 2D with.

Second, the Camra.$$anonymous$$ain.Forward, won't work, forwward works only with transform, should I add another library or did you mean camera.main.transform.forward?

Thanks again :)

avatar image IvovdMarel II_Spoon_II · Aug 12, 2018 at 12:57 PM 0
Share

Yes he means Camera.main.transform.forward. The other question is tricky. Depending on your game, but most likely you should use box colliders for the different lines. If there are a lot of curves in your line or your geometry is more complex, you'd have to look into changing the mesh using the mesh api. https://docs.unity3d.com/ScriptReference/$$anonymous$$esh.html

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

195 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Free Look Camera Pivot 1 Answer

Locking the Z-axis based on direction 0 Answers

Should 2.5D use 3D or 2D settings ? 3 Answers

2D or 3D settings for a 2.5D game? 2 Answers

2.5D weapon aiming with cross hairs help needed 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges