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 ConfirmCrit · Aug 18, 2014 at 10:55 PM · 2dobjectsspawningrange

Spawning an object attached to one object, drawn to another.

I have a game where the player score while getting next to an object. I want to create a line that shoots from the player to the enemy as the player has entered that range.

It seems like Distance Joint 2D does something SIMILAR to what I want, but not quite.

I don't see a clear way to do it, but I'll certainly hack at more of it tonight.alt text

I made a beautiful picture to further explain what I'm asking, 'cause I feel like I did a poor job. I want to use this as feedback to let the player know they've scored.

exampleofline.png (13.5 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

1 Reply

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

Answer by robertbu · Aug 19, 2014 at 05:04 AM

Here is a bit of example code. To test it

  • Start a new scene

  • Create a Quad and set the x scale to the width of the line you want.

  • Attach the script below to the Quad

  • Crate two game object that are no more that 'breakDistance' apart (default 5 units).

  • Drag and drop the two game objects onto the 'start' and 'end' variables.

  • Run the app and hit the space bar to connect the line.

  • Move one of the game objects so they are more than 'breakDistance' apart to break the line.


    using UnityEngine; using System.Collections;

    public class LinePulse : MonoBehaviour { public float pulseTime = 1.0f; public float breakDistance = 5.0f; public Transform start; public Transform end;

       private bool pulsing = false;;
     
         void Start() {
             renderer.enabled = false;
         }
     
         void Update () {
             if (Input.GetKeyDown(KeyCode.Space)) {
                 StartCoroutine(Pulse(pulseTime));
             }
         }
     
         IEnumerator Pulse(float time) {
             if (pulsing) yield break;
             pulsing = true;
             renderer.enabled = true;
             Vector3 scale = transform.localScale;
             bool broken = false;
     
             float timer = 0.0f;
             while (timer <= 1.0f) {
                 Vector3 dir = end.position - start.position;
                 float dist = dir.magnitude;
                 transform.rotation = Quaternion.FromToRotation (Vector3.up, dir);
                 float yScale = Mathf.PingPong(timer * 2.0f, 1.0f);
                 scale.y = yScale * dist;
                 transform.localScale = scale;
                 transform.position = Vector3.Lerp (start.position, end.position, timer);
     
                 if (!broken && timer > 0.5f && dist > breakDistance)
                     broken = true;
     
                 if (timer < 0.5 || broken)
                     timer += Time.deltaTime / time;
                 yield return null;
             }
             pulsing = false;
             renderer.enabled = false;
         }
     }
    
    
    

For your app, you'll likely have to set things up a bit differently, but this gives you some sample code that solves the line connecting and breaking.

Comment
Add comment · Show 2 · 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 ConfirmCrit · Aug 19, 2014 at 10:16 PM 0
Share

It seems to work, I swapped out the quad for my sprite and it seems to work fine in my game, awesome!

I'd upvote you but my rep is too low <3

avatar image robertbu · Aug 19, 2014 at 10:20 PM 0
Share

The code will work fine with any world object that has a height of 1 unit. The height is not 1 unit, then either the code needs to change just a bit or you need to use an empty game object parent with a child sized to a height of 1 unit.

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

2 People are following this question.

avatar image avatar image

Related Questions

Help How to stop spawning objects? 1 Answer

2D ball same bounce height on objects as on the ground 2 Answers

How to calculate projectile range 2 Answers

How to prevent 2d objects go over each other? 1 Answer

How to make objects appear with mouse click in a row? 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