Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how does "new" work in fixed update

Discussion in 'Getting Started' started by james_m_russell, Sep 18, 2015.

  1. james_m_russell

    james_m_russell

    Joined:
    Jun 2, 2015
    Posts:
    25
    So i have a gameobject that follows my players transform location, but not rotation. Originally I made it a child object, but that makes it follow the rotation as well. to get around this, I decided to write a small script to move this new gameobject in line with my player. (is there a better way?)

    Originally I was going to use transform.position.set. but that did nothing. I found this article on it:
    http://answers.unity3d.com/questions/225729/gameobject-positionset-not-working.html

    So my question: should you call "new" inside of update? would that continually create new variables in memory or does the garbage collector destroy the variable, each time Update() completes?

    So here is my sample code (which now works as i want it to).:
    Code (csharp):
    1.  
    2. public class PlayerShotSpawn : MonoBehaviour {
    3.  
    4.     public Transform PlayerLocation;
    5.  
    6.     Vector3 newPos;
    7.  
    8.     void FixedUpdate () {
    9.        
    10.         newPos = new Vector3(PlayerLocation.position.x+3.36f, PlayerLocation.position.y-0.3F, PlayerLocation.position.z);
    11.  
    12.         transform.position = newPos;
    13.  
    14.     }
    15. }
    16.  
    I've only been working in unity for a few months (off and on), so I figured getting started is the best place? If I'm wrong, please advise.

    for reference, here is the game i'm making from scratch. the gameobject in question is where I spawn my shots. it was originally a child of the player object, but when I added the "Spread" upgrade (grab two upgrade spheres), the up and down shots ended up going in front and behind of the enemy plane. I created the sound effects in Abelton Live. the models in Blender. (feedback welcome on the game as well, but not the point of this thread).
    http://www.vectorreactor.com/WebGames/Test6/Test6.html
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    FixedUpdate is only for physics; use Update. Vector3s are structs so "new" just allocates on the stack. You should remove the "newPos" variable unless it's being used in other functions; variables should always be local where possible. You can just do "transform.position = new Vector3(...".

    --Eric
     
    Kiwasi likes this.
  3. james_m_russell

    james_m_russell

    Joined:
    Jun 2, 2015
    Posts:
    25
    Perfect. I started with Update, but when "set" didn't work i swapped it to fixed update and didn't change it back. so Optimally, it should look more like this:

    Code (csharp):
    1.  
    2.  
    3. public class PlayerShotSpawn : MonoBehaviour {
    4.  
    5.  
    6. public Transform PlayerLocation;
    7.  
    8. void Update () {
    9.        
    10.      transform.position = new Vector3(PlayerLocation.position.x+3.36f, PlayerLocation.position.y-0.3F, PlayerLocation.position.z);
    11.  
    12.   }
    13. }
    14.  
    15.  
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Your code is correct and is a reasonable solution to the problem.

    Another way to do it would be to put your player and your shot spawn object both within a parent object (i.e., as siblings). Do movement on this parent object, and rotation only on the player object. But, depending on how you're handling movement, that may be more complex than the solution you have now — do whatever's easiest to write and maintain.
     
  5. james_m_russell

    james_m_russell

    Joined:
    Jun 2, 2015
    Posts:
    25
    awesome. thanks both of you.