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 shamblinwithshotguns · Dec 22, 2017 at 03:12 AM · instantiatepositionrounding

How do I instantiate an object to a rounded position?

This is a GIF illustrating the problem. I am trying to make it so that the bullets are instantiating on a rounded position (to .0 or .5, preferably) At the moment, they spawn based on a rate of time which is okay because it gets it pretty close but it eventually falls out of sync. The meat and potatoes of the turret code is this block that operates in a Coroutine that cycles once every second:

             //Instantiates the prefab at the current position and rotation. I assume I will have to do something here.
             go = Instantiate(_bullet, (transform.position + new Vector3(0, 0, 0)), transform.rotation);
             //This grabs the script component of the bullet and assigns a damage value.
             Scr_Bullet sn = go.GetComponent<Scr_Bullet>();
             sn.SetDamage(Damage);
             //Gets the rigidbody of the instantiated prefab and sends it flying in the direction the turret is facing.
             rb = go.GetComponent<Rigidbody2D>();
             rb.AddForce(transform.rotation * new Vector2(100, 0));


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

2 Replies

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

Answer by Buckslice · Dec 22, 2017 at 03:31 AM

Rounding won't help the root cause of the problem I don't think. Eventually it would still get out of sync when based on time and you would end up just skipping a whole shot or something.

Idea 1: You could make a variable called like nextXSpawnPos or something start it at like 1.0. Once your current transform.position.x is past that, spawn a shot. Then add one to nextSpawnPos and continue. So next time youre past 2.0 then spawn another one etc. Have that checked in Update(). You would have to change once you reverse directions to the opposite of that code basically.

Better Idea 2: You could alternatively just set up little triggers below each platform and in the OnTriggerEnter2D() check if you hit them then spawn a shot like that. Prob easiest solution.

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 shamblinwithshotguns · Dec 22, 2017 at 04:05 PM 0
Share

I think you're absolutely right about the turret eventually falling out of sync. I didn't want to string a bunch of triggers along the path of the turrets, though. $$anonymous$$y new code looks like this:

 public float Distance = 0.0f;
 public Vector3 lastPos;
 
 // Sets lastPos as its point of origin.
 void Start()
     {
         lastPos = transform.position;
     }
 
 // Distance increases as the position of the turret changes.
     void Update()
     {
         Distance += Vector3.Distance(transform.position, lastPos);
         lastPos = transform.position;
         Fire();
     }
 
 // Whenever the Distance the turret has traveled becomes at least one game unit, the turret fires and sets Distance back down by one.
     void Fire()
     {
         if (Distance >= 1)
         {
             Distance--;
             // And then my code for firing the projectiles goes here!
         }

It's not exact, but it's close enough. It also works backwards!

avatar image
0

Answer by Kishotta · Dec 22, 2017 at 04:42 AM

If you double any floating point number, round it to an int, and then divide it by two, you will get a rounding to the nearest half.

For example:

  • 1.2→2.4→2→1

  • 3.14→6.28→6→3

  • 5.6→11.2→11→5.5

You could make this easy with a helper function:

  float RoundToHalf (float n) {
     n = Mathf.Round (n * 2);
     return n / 2f;
 }


Your usage might be:

 float rx = RoundToHalf(transform.position.x);
 float ry = RoundToHalf(transform.position.y);
 float rz = RoundToHalf(transform.position.z);
 Vector3 instantiatedPosition = new Vector3 (rx, ry,  rz);


You could probably simplify this with an extension method:

 Vector3 RoundToHalf (this Vector3 v) {
     return new Vector3 (RoundToHalf(v.x), RoundToHalf (v.y), RoundToHalf(v.z));
 }


Usage:

 Vector3 instantiatedPosition = transform.position.RoundToHalf();
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

103 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

Related Questions

Fire Projectile based on Model rotation 1 Answer

give instantiated objects positions from from objects in list 0 Answers

Instantiating objects at different locations 0 Answers

How to save/load the Y-Coordinates of Instantiated Objects 1 Answer

my object isnt instantiating in the right place 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