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 areeyaodsup · Jan 30, 2019 at 03:52 PM · 2d gamerigidbody2donline gamelaser beam

Hi,How do I make a 2D laser beam with stable speed ?

I create an olinegame about guys with laser beams in the room ,so i decide to make my laser beam by rigidbody2d that reflect with everything in the room , but when i test it .Sometime my laser has lower speed and stop moving.

what should i do to improve my laser better ?

-laser beam is 2d circle collider with physics2d bounce 0.5


my laser beam code :

 public class LaserBehavior : MonoBehaviour {
 
     // Use this for initialization
     Rigidbody2D _rbLaser;
     [SerializeField]
     GameObject _laser = null;
     public float _speedLaser;
 /*
     void Awake()
     {
         _rbLaser = gameObject.GetComponent<Rigidbody2D>();
         //Invoke("LaserControll",0.1f);
     }
 */
     
     void Start () {
         _rbLaser = gameObject.GetComponent<Rigidbody2D>();
         Invoke("LaserControll",0.1f);
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void LaserControll()
     {
         Vector2 direction = new Vector2(Random.Range(-2, 2), Random.Range(-3, 2)+1);
         _rbLaser.AddRelativeForce(direction*_speedLaser,ForceMode2D.Impulse);
        
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "Player")
         {
             other.gameObject.GetComponent<SpriteRenderer>().color = Color.black;
             other.gameObject.GetComponent<SpriteRenderer>().color = Color.green;
             Debug.Log("destroy");
             Destroy(this);
         }
 
         if (other.gameObject.tag == "Shield")
         {
             Vector2 inDirect = GetComponent<Rigidbody2D>().velocity;
             Vector2 inNormal = other.contacts[0].normal;
             Vector2 reflect = Vector2.Reflect(inDirect, inNormal);
             reflect.Normalize();
 
             _rbLaser.AddRelativeForce(reflect * _speedLaser, ForceMode2D.Impulse);
         }
 
         if (other.gameObject.tag == "ReflectObject")
         {
             //Debug.Log("REFLECT " +other.gameObject.name);
             //Invoke("LaserControll", 0.1f);
             Vector2 inDirect = GetComponent<Rigidbody2D>().velocity;
             Vector2 inNormal = other.contacts[0].normal;
             Vector2 reflect = Vector2.Reflect(inDirect,inNormal);
             reflect.Normalize();
 
             //Debug.Log("inDirection " + inDirect);
             // Debug.Log("REFLECT " + other.gameObject.name+" inNormal" + inNormal);
             //Debug.Log("reflect" + reflect);
 
            // _rbLaser.AddRelativeForce(reflect *(_speedLaser*10), ForceMode2D.Force);
             _rbLaser.AddRelativeForce(reflect * _speedLaser, ForceMode2D.Impulse);
 
             if (_laser != null)
             {
                 GameObject go = Instantiate(_laser, transform.position, transform.rotation);
                 go.name = "Laser";
                 go.GetComponent<SpriteRenderer>().color = Color.white;
             }
         }
     }
 }

alt text

capture.png (5.3 kB)
Comment
Add comment · Show 2
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 $$anonymous$$ · Jan 30, 2019 at 10:08 PM 1
Share

What happens when you replace AddRelativeForce with just AddForce?

avatar image areeyaodsup · Jan 31, 2019 at 09:42 AM 0
Share

@HomebrewAI I want to use my laser local position to control direction ,so i choose addRelativeForce .

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by xxmariofer · Jan 31, 2019 at 09:46 AM

hello, if the beam is a object that is losing speed, try going into the laser rigidbody propertys and set drag and angular drag to 0, also the walls (and i think the rigidbdy too) must have a Physical Material attach to it with the bounciness set to max.

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 areeyaodsup · Jan 31, 2019 at 10:05 AM 0
Share

thank! I will try this.

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

116 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

Related Questions

Why does this piece of code work with gravity and this one dosent 2 Answers

How to make a 2D Player move a box properly ? 1 Answer

Aiming Direction of Player altered After collision 1 Answer

Teleport Player in 2D game? 4 Answers

2D Game: Best way to set player's Z? 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