• 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 Flaymes · Dec 08, 2020 at 07:35 PM · transformvector2movetowards

How to store variables in start method

In my game I am trying to make it so the boss shoots objects at you and as soon as those objects spawn in I want them to get your position from that start frame and go towards it, so I don't want it to follow the player but if the player doesn't move then it will hit them. I had trouble with this as the object would continuously follow the player as the variable target kept changing event though I set it in the start method. Here is the script, please try to help me here :

public Transform target; public GameObject player; public float speed; public float deathTime;

 public void Start()
 {
     player = FindObjectOfType<Player>().gameObject;
     target = player.transform;

     Invoke("EntityClear", deathTime);
 }

 public void Update()
 {
     transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
 }

 public void EntityClear()
 {
     Destroy(gameObject);
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if(collision.gameObject.tag == "Player")
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
     if(collision.gameObject.tag == "Floor")
     {
         Destroy(gameObject);
     }
 }
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 unity_ek98vnTRplGj8Q · Dec 08, 2020 at 07:50 PM

You are grabbing the player's Transform which is a class that contains data about the player's position. As the player moves around the scene, the data inside the class changes, so when you call target.position in your update method you will get the new, updated position values. Instead, grab the position out of the transform in the start method. The position is a Vector, which is a not a class but a struct, which is pass by value and therefore won't change when you look at it again in your update, even if the position associated with the transform has changed. That is because pass by value types make a copy of the data when they read it, so that copy is preserved even when the original changes.


So instead, do something like this:

 Vector3 playerPos;
 
 Start(){
     playerPos = Player.transform.position;
 }
 
 Update(){
     //Move towards player pos
 }

One more thing to note is that you probably don't actually want the player position, you want the direction to the player. If you use the MoveTowards(playerPos), the projectile will stop at the position where the player was when it was fired. Instead I imagine you want the projectile to continue traveling in the same direction. So you can instead do something like

 Vector3 direction;
 
 Start(){
     direction = (Player.transform.position - this.transform.position).normalized;
 }
 
 Update(){
     transform.position += direction * speed * Time.deltaTime;
 }



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.

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

184 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Set speed by distance from object 1 Answer

How Vectors work and how I should use them? 1 Answer

how to find a point(vector2) between two points(Vector2) of a line (line renderer) 2 Answers

why does the position of my destinationGameobject doesnt change? 0 Answers

Reposition to minimum distance from target 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