• 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
Question by BitWarrior11 · Jun 23, 2018 at 09:08 AM · c#variableupdateshootinggun script

Stop checking for values in Update function?

So, I'm trying to make a shooting mechanism for my 2D platformer, and the way I have it going in the right direction is by using the facingRight boolean (line 25-29). The problem is (and you can probably see it) is that when the player changes directions, the bullet does too. Does anyone know how to stop from checking values (EDIT: or keeping values the same) WHILE letting the player shoot still? Any suggestion is welcome. Thanks! BTW here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectile : MonoBehaviour {
 
     private Vector2 target;
     public float speed;
     public GameObject player;
     public PlayerController pController;
     public bool facingRight;
     private Transform playerTransform;
 
     void Start () {
         player = GameObject.Find("Player");
         pController = player.GetComponent<PlayerController>();
         playerTransform = player.GetComponent<Transform>();
     }
 
     void Update () {
 
         facingRight = pController.facingRight;
         Debug.Log(facingRight);
 
         if (facingRight == true) {
             target = new Vector2(playerTransform.position.x + 30, playerTransform.position.y);
         } else {
             target = new Vector2(playerTransform.position.x - 30, playerTransform.position.y);
         }
         
         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
         if (Vector2.Distance(transform.position, target) < 0.2f) {
             Destroy(gameObject);
         }
 
     }
 
 }
 




Comment

People who like this

0 Show 0
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

Answer by JDelekto · Jun 23, 2018 at 09:56 AM

Since I imagine that you are creating the projectile when the user fires, you will probably want to save the initial state of the direction the player was facing and the target position in your Start() method. Then, only do the movement of the projectile using these initial values, so your new Start() and Update() methods would look something like this:

     void Start()
     {
         player = GameObject.Find("Player");
         pController = player.GetComponent<PlayerController>();
         playerTransform = player.GetComponent<Transform>();
 
         target = new Vector2(playerTransform.position.x + (pController.facingRight ? 30 : -30), playerTransform.position.y);
     }
 
     void Update()
     {
         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
         if (Vector2.Distance(transform.position, target) < 0.2f)
         {
             Destroy(gameObject);
         }
     }

Comment
BitWarrior11

People who like this

1 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 BitWarrior11 · Jun 23, 2018 at 10:04 AM 0
Share

Thank you! This worked perfectly!

avatar image

Answer by belwar · Jun 23, 2018 at 10:00 AM

If the bullet changes direction when the player does, I suppose you spawn the bullets as childs of the player object.

Try to spawn them at the root (or as childs of another object but not the player).

Comment

People who like this

0 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 BitWarrior11 · Jun 23, 2018 at 10:05 AM 0
Share

They already are as root. I think it's cause they are constantly updating the target based on the player's direction.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

86 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

Related Questions

summing up items price in runtime 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

GUI Variables doesn't update C# 0 Answers

How to make this code fire projectile once during the Update function? I know Update means 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