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
0
Question by Robindoo · Nov 12, 2010 at 05:00 AM · cameraguilinerenderer

LineRenderer with moving camera

Still in progress of my game. I'm faced with a problem. The game that I'm working on is similar to Cave Run. The player is constantly running and the camera moves with the player (correct me if this should not be the case). For my game, as the player is running, they are able to draw on the screen to activate skills. I've attached the script of the LineRenderer to the camera, and when I draw, the first point of the line renderer moves out of view from the iPhone as the camera is still moving.

What I want to achieve is to have the drawing symbol remain on screen like how GUI does. A solution that I have is to move the platform instead. But this might cause some problems when adding obstacles into the stage.

EDIT: Here's my codes

class Stroke extends MonoBehaviour { public var currentCamera : Camera; private var player : Transform; private var distance = 1; private var drawArray : ArrayList = new ArrayList(); private var lineMat : Material;

 function Stroke()
 {

 }

 function Awake()
 {
     currentCamera = Camera.main;
     player = GameObject.FindWithTag("Player").transform;


 }

 function Update()
 {


     ProcessInput();

     var strokeRenderer : LineRenderer = GetComponent(LineRenderer);
     strokeRenderer.SetVertexCount(drawArray.Count);
     strokeRenderer.SetColors(Color(0,0,1,1), Color(0,0,1,0));
     strokeRenderer.material = new Material (Shader.Find("Particles/Additive"));


     strokeRenderer.SetWidth(0.1,0.1);

     for (var i : int = 0; i < drawArray.Count; i++)
     {
         strokeRenderer.SetPosition(i, drawArray[i]);
     }
 }


 function ProcessInput()
 {

     for(var p : int = 0; p < Input.touchCount; p++)
     {
         var touchPoint : Vector3 = Input.GetTouch(p).position;
         var screenPoint : Vector3 = new Vector3(touchPoint.x, touchPoint.y, distance);
         var worldPoint : Vector3 = currentCamera.ScreenToWorldPoint(screenPoint);

         if(Input.touchCount == 1)
         {
             if(Input.GetTouch(p).phase == TouchPhase.Began)
             {
                 drawArray.Add(worldPoint);
             }

             if(Input.GetTouch(p).phase == TouchPhase.Moved)
             {
                 drawArray.Add(worldPoint);
                 MovePlayer();

             }


             if(Input.GetTouch(p).phase == TouchPhase.Ended)
             {
                 drawArray.Clear();
             }

         }
     }

 }


 function MovePlayer()
 {
     var startPos : Vector3 = drawArray[0];
     var endPos : Vector3 = drawArray[drawArray.Count -1];
     direction = (endPos - startPos).normalized;
     player.transform.position.x += direction.x * 0.4;       
 }


}

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

1 Reply

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

Answer by Herman-Tulleken · Nov 12, 2010 at 07:19 AM

You simply have to update the points in Update to new world positions each update (and not just when you create the curve).

Something along:

int i = 0;

foreach screenPosition of my curve Vector3 worldPosition = camera.ScreenToWorldPoint(screenPosition);
lineRenderer.SetPosition(i, worldPosition); i++;

The tricky part will be to update if you interpolate screenpositions (or worldPositions), or sample them. In either case, the slow but easy method will be to re-interpolate and resample each Update.

Once you have got this going, you can optimise by storing the the right number of points in screen space. Then you only have to convert to world space - no need to resample or reinterpolate.

Edit: Here is how it looks in your code:

class Stroke extends MonoBehaviour { public var currentCamera : Camera; private var player : Transform; private var distance = 1; private var drawArray : ArrayList = new ArrayList(); private var lineMat : Material;

 //...

 function Update()
 {
     ProcessInput();

     var strokeRenderer : LineRenderer = GetComponent(LineRenderer);
     strokeRenderer.SetVertexCount(drawArray.Count);
     strokeRenderer.SetColors(Color(0,0,1,1), Color(0,0,1,0));
     strokeRenderer.material = new Material (Shader.Find("Particles/Additive"));    
     strokeRenderer.SetWidth(0.1,0.1);

     for (var i : int = 0; i < drawArray.Count; i++)
     {
         //Do the conversion here:
         var worldPoint : Vector3 = currentCamera.ScreenToWorldPoint( drawArray[i]);
         strokeRenderer.SetPosition(i, worldPoint);
     }
 }

 function ProcessInput()
 {
     for(var p : int = 0; p < Input.touchCount; p++)
     {
         var touchPoint : Vector3 = Input.GetTouch(p).position;
         var screenPoint : Vector3 = new Vector3(touchPoint.x, touchPoint.y, distance);

         if(Input.touchCount == 1)
         {
             if(Input.GetTouch(p).phase == TouchPhase.Began)
             {
                 //Add screen points here
                 drawArray.Add(screenPoint );
             }

             if(Input.GetTouch(p).phase == TouchPhase.Moved)
             {
                 //Add screen points here
                 drawArray.Add(screenPoint );
                 MovePlayer();
             }


             if(Input.GetTouch(p).phase == TouchPhase.Ended)
             {
                 drawArray.Clear();
             }

         }
     }    
 }    

 function MovePlayer()
 {
     var startPos : Vector3 = drawArray[0];
     var endPos : Vector3 = drawArray[drawArray.Count -1];
     direction = (endPos - startPos).normalized;
     player.transform.position.x += direction.x * 0.4;       
 }

}

Comment
Add comment · Show 3 · 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 Robindoo · Nov 15, 2010 at 01:47 AM 0
Share

I understand the logic, but i'm a little stuck at how i should put it in.

avatar image Herman-Tulleken · Nov 16, 2010 at 06:59 AM 0
Share

@Robindoo I adapted your code to implement the scheme.

avatar image Robindoo · Nov 19, 2010 at 01:30 AM 0
Share

Thanks once again. So far the controls seem to be working fine. Only when put together with some of my friends file it experience some lag. Haha. But in the mean time we are still working to fix things out.

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

No one has followed this question yet.

Related Questions

How do I set up a GUI around multiple cameras? 1 Answer

how can I disable all camera movements trough GUI? 2 Answers

Webplayer bug after coming back from sleep mode 0 Answers

Help with cameras and enabling/disabling GUItextures? 1 Answer

New Unity GUI has messed up the scene camera 2 Answers


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