Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by ratboy · Jul 16, 2012 at 12:44 PM · iostouchquaternionlinebox collider

Draw a line in game

Hi all, I have a 2D, sprite based physics game for IOS. Ive already implemented a script which allows the player to drag their finger on the screen and it spawns random ink splats, sort of an improvised version of that Line Rider game.

alt text

Each of these inks plats is an individual prefab with the sprite image, and a capsule collider, however due to the nature of the object that rolls over it, being a rock with a capsule collider on it, occasionally the rock gets stuck in between the dots, as if the player moves their finger too quickly the engine can't keep up and there will be gaps, as seen in the picture. (These can be a lot bigger though).

My question is if it is possible to find the position of the last dot spawned, and create a box collider between 'this' one, and the old one... well i know its possible, its a game engine, but how would i get the rotation ? i imagine using some sort of Quaternion.LookAt or RotateTowards or something similar, also how would i find the last spawned dot?

Cheers in advance. :)

screen shot 2012-07-16 at 13.36.35.png (118.9 kB)
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by ScroodgeM · Jul 22, 2012 at 02:56 PM

answer rev 3.0

draws a solid collider using box colliders folowing by mouse while pressing left button

draws in Y=0 plane independent of colliders.

example on the pic below

using UnityEngine;
using System.Collections;
public class UnbreakableCollider : MonoBehaviour
{
    public Transform DotPrefab;
    Vector3 lastDotPosition;
    bool lastPointExists;
    void Start()
    {
        lastPointExists = false;
    }
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            Vector3 newDotPosition = mouseRay.origin - mouseRay.direction / mouseRay.direction.y * mouseRay.origin.y;
            if (newDotPosition != lastDotPosition)
            {
                MakeADot(newDotPosition);
            }
        }
    }
    void MakeADot(Vector3 newDotPosition)
    {
        Transform dot =(Transform) Instantiate(DotPrefab, newDotPosition, Quaternion.identity); //use random identity to make dots looks more different
        if (lastPointExists)
        {
            GameObject colliderKeeper = new GameObject("collider");
            BoxCollider bc = colliderKeeper.AddComponent();
            colliderKeeper.transform.position = Vector3.Lerp(newDotPosition, lastDotPosition, 0.5f);
            colliderKeeper.transform.LookAt(newDotPosition);
            bc.size = new Vector3(0.1f, 0.1f, Vector3.Distance(newDotPosition, lastDotPosition));
        }
        lastDotPosition = newDotPosition;
        lastPointExists = true;
    }
}

alt text


1.png (310.0 kB)
Comment
Add comment · Show 7 · Share
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 fafase · Jul 22, 2012 at 03:16 PM 0
Share

I doubt this is not working so I transfer the +1 that was given to me. @Jacobmill you should +1 that answer too so that it shows on top for later consultation.

avatar image ratboy · Jul 22, 2012 at 03:27 PM 0
Share

this is exactly what i need, thank you very much for you time and effort. you too @fafsase, but newDotPosition is co$$anonymous$$g through as NaN (not a number), is that a bug my side or the math? thanks for being patient.

avatar image ratboy · Jul 22, 2012 at 03:54 PM 0
Share

its weird, have looked up the problem and tried everything (except reinstalling unity), guessing this doesnt occur with you @Scroodge?

avatar image ScroodgeM · Jul 22, 2012 at 03:56 PM 0
Share

you can got NaN if y-component of ray's direction is zero. this can be if your camera is orthogonal and looks toward x or z (then ray from camera will never intersect Y=0 plane

you can adept ray-to-position calculation as you need - this can be other axis-oriented or even a collider intersect

avatar image ScroodgeM · Jul 22, 2012 at 03:59 PM 0
Share

PS Debug.Log mouseRay.origin and mouseRay.direction - this can point to NaN source

Show more comments
avatar image
0

Answer by ScroodgeM · Jul 22, 2012 at 08:34 AM

using UnityEngine;
using System.Collections;
public class UnbreakableCollider : MonoBehaviour
{
    public Transform DotPrefab;
    Vector3 lastDotPosition;
    bool lastPointExists;
    void Start()
    {
        //testing script
        lastPointExists = false;
        MakeADot(new Vector3(10, 0, 0));
        MakeADot(new Vector3(5, 10, 0));
        MakeADot(new Vector3(7, 7, 7));
        MakeADot(new Vector3(0, 0, 0));
        MakeADot(new Vector3(0, 10, 10));
    }
    void MakeADot(Vector3 newDotPosition)
    {
        Transform dot =(Transform) Instantiate(DotPrefab, newDotPosition, Quaternion.identity); //use random identity to make dots looks more different
        if (lastPointExists)
        {
            GameObject colliderKeeper = new GameObject("collider");
            BoxCollider bc = colliderKeeper.AddComponent();
            colliderKeeper.transform.position = Vector3.Lerp(newDotPosition, lastDotPosition, 0.5f);
            colliderKeeper.transform.LookAt(newDotPosition);
            bc.size = new Vector3(0.1f, 0.1f, Vector3.Distance(newDotPosition, lastDotPosition));
        }
        lastDotPosition = newDotPosition;
        lastPointExists = true;
    }
}

alt text


1.png (21.6 kB)
Comment
Add comment · Show 7 · Share
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 ratboy · Jul 22, 2012 at 01:11 PM 0
Share

Hi Scrooodge, thanks for your post. Ive implemented this and this is pretty much what i want, although being the amateur that i am, i need to be able to draw it with the mouse/finger touch. so obviously i need to start the makeadot function in update if(Get$$anonymous$$ouseButton(0)){, as this current method makes predefined lines in the Start function. So i need to make the lines as the player is drawing them, between the touch origin, to the touch end. Could you please help a little more - thanks a lot for your input already though I'm grateful as it is :)

avatar image ScroodgeM · Jul 22, 2012 at 01:18 PM 0
Share

take your script that places point (image in your question) and ins$$anonymous$$d of place dots call $$anonymous$$akeADot method. it places dot and makes collider at once

avatar image ratboy · Jul 22, 2012 at 02:24 PM 0
Share

cheers scroodge, i am doing, but am getting the problem i stated in @fafase's answer. Its an edit of your script so feel free to feedback ;)

avatar image oldcollins · Jul 03, 2013 at 05:31 PM 0
Share

any chance you have a javascript version please

avatar image AlucardJay · Jul 04, 2013 at 05:02 AM 1
Share

It is easy to convert this to uJS.

Check the Unity Scripting Reference, at the above-right of every script example, there is a drop-down box. Click Javascript, then select C# to see the same example in each language.

Here's some links I found useful in converting between C# and JS :

  • http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

  • http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

  • http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/

If you are still having trouble, I will convert Scroodge$$anonymous$$s answer to uJS.

Show more comments
avatar image
0

Answer by ratboy · Jul 22, 2012 at 02:37 PM

var lastDotPosition : Vector3;

var lastPointExists : boolean = false;

var startPos : Vector3;

var endPos : Vector3;

var result : Vector3;

function Update(){

  if (Input.GetMouseButtonDown(0)){

      startPos = Input.mousePosition;

  }

  if (Input.GetMouseButtonUp(0)){

      endPos = Input.mousePosition;

      result = endPos - startPos;

      Debug.Log(result);

  }
  
  MakeADot(result);

}

function MakeADot(newDotPosition : Vector3){

  Debug.Log(newDotPosition);

  if (Input.GetMouseButton(0)){

      var dot : Transform = Instantiate(dotPrefab, newDotPosition, Quaternion.identity);

      dot.gameObject.layer = LayerMask.NameToLayer("Play Zone");

      if (lastPointExists){

          var colliderKeeper = new GameObject("collider");

          colliderKeeper.layer = LayerMask.NameToLayer("Play Zone");

          var bc : BoxCollider = colliderKeeper.AddComponent(BoxCollider);

          colliderKeeper.transform.position = Vector3.Lerp(newDotPosition, lastDotPosition, 0.5);

          colliderKeeper.transform.LookAt(newDotPosition);

          bc.size = new Vector3 (0.1, 0.1, Vector3.Distance(newDotPosition, lastDotPosition));

      }
      lastDotPosition = newDotPosition;
      lastPointExists = true;

 }

}

sorry for poor formatting, the code thing in the answers box is failing to work.

Comment
Add comment · Share
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

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

11 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

Related Questions

Drawing lines between two touch positions? 4 Answers

Rotate on drag for IOS? 1 Answer

Rotate model with ARKit 0 Answers

Detecting touch during OnGUI () 1 Answer

Recommendations on best way to begin implementing Swipe for iOS game? 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