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 SandorChickane · Nov 02, 2014 at 01:15 AM · raycastupdatescaledynamic

gameObject not dynamically scaling via the update method

Hello there!

I am currently working in 2D and trying to make a 'lazer-beam' sort of thing appear between the player and where a raycast hits. So far, I have got the beam to instantiate well enough at the right scale and position.

However, I want the beam to scale dynamically between the player and the end of the raycast, so that if the player moves further away from where it is hitting, the beam stretches so that it is still connected to the player.

I attempted to do this by putting the scaling code in the update function and assuming that it would update the scale every frame. Instead, the beam stays the length it was instantiated at and is seemingly unaffected by the code in the update function.

Here is the code I am using:

 public float fireRate = 0;
     public float damage = 0;
     public LayerMask whatToHit;
     public GameObject lazer;
 
     float timeToFire = 0;
     Transform firePoint;
     float distance = 0;
     Vector2 lazerInstantiate;
     Vector2 lazerPosition;
     Vector2 firePointPosition;
     RaycastHit2D hit;
 
     // Use this for initialization
     void Start () {
         firePoint = transform.FindChild ("FireLazerPoint");
         if (firePoint == null)
         {
             Debug.LogError ("NO FIREPOINT LULZ!??!!??!!");
         }
     }
     
     // Update is called once per frame
     void Update () {
                 if (Input.GetButtonDown ("Fire1") && Time.time > timeToFire) {
                         timeToFire = Time.time + 1 / fireRate;
                         Shoot ();
                 }
         }
 
     void Shoot() 
     {
 
         lazerPosition = new Vector2(firePoint.position.x + 100, firePoint.position.y);
         firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
         hit = Physics2D.Raycast (firePointPosition, lazerPosition - firePointPosition, 100, whatToHit);
         Debug.DrawLine (firePointPosition, (lazerPosition - firePointPosition)*100, Color.cyan);
         Instantiate (lazer, Vector2.Lerp (firePointPosition, hit.point, 0.5f), Quaternion.identity);
         distance = Vector2.Distance (firePointPosition, hit.point);
         lazer.transform.localScale = new Vector2 (distance, 1);
 
 
         Debug.Log (distance);
 
         if (hit.collider != null) 
         {
             Debug.DrawLine(firePointPosition, hit.point, Color.red);
 
         }
 
     }

From my meager knowledge of unity and c#, I have no clue as to why this isn't working! Any advice on the probably obvious blunder I've made would be much appreciated :)

Cheers!

P.S. the debug drawlines are just there for testing purposes, I know they don't show up in game.

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 Nebukam · Nov 02, 2014 at 05:20 AM

Your starting point is never updated, since the Start function is only called once (it act like a constructor for your object)

Thus, your update function needs to read :

      void Update () {
              if (Input.GetButtonDown ("Fire1") && Time.time > timeToFire) {
                      timeToFire = Time.time + 1 / fireRate;

                      //Just add this line so you always get an up-to-date firePoint :
                      firePoint = transform.FindChild ("FireLazerPoint");

                      Shoot ();
              }
      }
Comment
Add comment · Show 1 · 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 SandorChickane · Nov 02, 2014 at 12:13 PM 0
Share

That's awesome, thank you! Completely forgot that the original position had to be updated in the actual update method :)

Cheers!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Check if player object is right above this object 1 Answer

rollover scale script problem 1 Answer

How should i add raycast to multiple game objects??? 0 Answers

Scaling Animation for a Instantiated Object 1 Answer

What do I need to know when caching the results of a raycast every frame? 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