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 Slateboard · Nov 16, 2014 at 05:35 PM · c#2dbeginner

I'm using an Object Pooler for the projectiles in my game. How do I reset their movement speed when they are made active again?

I'm basing it on this - http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

I have a pool of 20 bullets, and when they're shot, they will move in the direction the player is facing through a bool.

The problem arises when the bullets are reused, and I end up in a scenario where they maintain their movement speed. Basically, if I am facing left and fire 5 of those bullets, then fire the remaining 15 while facing right, once they recycle, they will travel in these same directions, regardless of what direction I'm facing.

Here's the script for my projectiles.

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : MonoBehaviour {
 
     // Use this for initialization
 
     public float speed;
     
     bool bulletDirection;
     
     
    
     
     
     void Start () 
     {
         bulletDirection = PlayerMovement.facingRight;
         
 
         
         
 
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         
 
         if (bulletDirection == false) 
         { 
             transform.Translate(-speed * Time.deltaTime, 0, 0);
             
 
         } 
         else 
         { 
             transform.Translate(speed * Time.deltaTime, 0, 0); 
             
         }
         
             //transform.Translate(-speed * Time.deltaTime, 0, 0);
         
 
         
         
         
     }
 }
 

This is the part of the Player Movement Script that handles left-right movement and the direction the player faces -

 void Movement()
     {
 
 
         if (Input.GetKey("left"))
         {
             //Add force to the players rigidbody allowing it to move upwards if the above if statement is true
 
             rigidbody2D.AddForce(Vector2.right * -speed);
             //rigidbody2D.velocity = new Vector2(-speed, 0);
 
             if (facingRight == true)
             {
                 Vector2 theScale = transform.localScale;
                 theScale.x *= -1;
                 transform.localScale = theScale;
                 
                 facingRight = false;
 
             }
             
            
             
         }
 
         if (Input.GetKey("right"))
         {
             //Add force to the players rigidbody allowing it to move upwards if the above if statement is true
 
             rigidbody2D.AddForce(Vector2.right * speed);
             //rigidbody2D.velocity = new Vector2(speed, 0);
 
             if (facingRight == false)
             {
                 Vector2 theScale = transform.localScale;
                 theScale.x *= -1;
                 transform.localScale = theScale;
                 
                 facingRight = true;
 
             }
 
             
         }


And here's what causes the projectiles to fire -

 void Fire()
     {
 
                 GameObject obj = ObjectPooler.current.GetPooledObject();
                 if (obj == null) return;
 
 // Resets the Position and Rotation of the projectiles when they become active again
                 obj.transform.position = bulletOrigin.position;
                 obj.transform.rotation = bulletOrigin.rotation;
                 
                 obj.SetActive(true);
                 
 
             
 
     }


And here's the game itself - http://howdoilookinthisshirt.com/Unity/2DGame.html

Arrow Keys to Move, Spacebar to Jump, and Left-CTRL to fire.

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 Kiwasi · Nov 16, 2014 at 06:25 PM

Start only gets called once in the lifetime of a GameObject. OnEnable gets called every time you activate it.

Change your Start method to OnEnable, and everything will work fine.

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 Slateboard · Nov 16, 2014 at 06:49 PM 0
Share

This fixed it. Thanks.

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

GetComponent().enabled = true; 1 Answer

Unusual error regarding movement in a top down 2d sidescroller 1 Answer

C# Procedural Tilemap Generation - Each instance of tilemap rendering the same 1 Answer

Help Me with simple respawn script please (c#) 2 Answers

What is wrong with my Counter Script? (C#) 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