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 /
  • Help Room /
This question was closed May 10, 2021 at 11:55 AM by WizzardH for the following reason:

Other

avatar image
0
Question by WizzardH · Dec 22, 2016 at 06:56 PM · c#positionobjectvector3update

Saving old Position

Hey guys. My porblem is, that I have to spawn an Prefab-object everytime the Y Position of my Spawner changed around -1.8. But due to my Code it does not and I don not know why..

using UnityEngine; using System.Collections;

public class Spawner : MonoBehaviour {

 public Vector3 oldPos;
 public Vector3 Pos;
 public GameObject Block;
 void Start () {
     Vector3 oldPos = transform.position;
     Vector3 BlockPos = new Vector3 (Random.Range (1.5f, -1.5f), transform.position.y, transform.position.z);
     Instantiate (Block, BlockPos, transform.rotation); 
 }

 void Update () {
     Vector3 pos = transform.position;
             if (oldPos.y - 1.8f == Pos.y) { //everytime the current Position is equal to the Old Position.y - 1.8f, oldPos.y -1.8 > Pos.y did not work as well
         Vector3 BlockPos = new Vector3 (Random.Range (1.5f, -1.5f), transform.position.y, transform.position.z);
         Instantiate (Block, BlockPos, transform.rotation); 
         Pos = oldPos;
     }
 }

}

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

  • Sort: 
avatar image
0

Answer by Vicarian · Dec 22, 2016 at 07:16 PM

@WizzardH Your variable oldPos never gets updated, so Pos and oldPos will always be equivalent, thus the decision structure on line 12 will always evaluate to false.

 void Update () {
     Vector3 pos = transform.position;
     if (oldPos.y - 1.8f == pos.y) {
          Vector3 BlockPos = new Vector3 (Random.Range (1.5f, -1.5f), transform.position.y, transform.position.z);
          Instantiate (Block, BlockPos, transform.rotation); 
          oldPos = pos;
      }
  }

You may want to consider a distance calculation instead of a subtraction:

 if (Vector3.Distance(oldPos, pos) >= 1.8f)
     //Do stuff
Comment
Add comment · Show 2 · 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 WizzardH · Dec 23, 2016 at 02:52 PM 0
Share

Thank you for your awnser :) I realised that the Object isn´t changing it´s position because it is attached to the camera (which is moving downwards with the player), so I placed the code at the camera Update function.. void Start () { Vector3 oldPos = transform.position; thePlayer = FindObjectOfType (); lastPlayerPosition = thePlayer.transform.position; }

 void Update () {
     Vector3 Pos = transform.position;
     distanceTo$$anonymous$$ove = thePlayer.transform.position.y - lastPlayerPosition.y;
     transform.position = new Vector3 (transform.position.x, transform.position.y + distanceTo$$anonymous$$ove, transform.position.z);
     lastPlayerPosition = thePlayer.transform.position;
     if (Vector3.Distance (oldPos, Pos) > -1.8f) {
         Spawner.setBlock ();
     }
 
 }

At my spawner I now have the function setBlock ..

             public void setBlock(){
     Vector3 BlockPos = new Vector3 (Random.Range (1.5f, -1.5f), transform.position.y,                        transform.position.z);
     Instantiate (Block, BlockPos, transform.rotation); 
          }

The problem now is, that there are a lot of blocks generated all the time. $$anonymous$$aybe thats due to the Update function but I don´t know where else to place it.. Do you have an idea?

avatar image Vicarian WizzardH · Dec 27, 2016 at 02:27 PM 0
Share

When you take a distance, the value obtained from the function will always be positive, so

 if (Vector3.Distance (oldPos, Pos) > -1.8f)

will always evaluate true, which means that you're going to get a block spawned every frame.

Follow this Question

Answers Answers and Comments

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

How to know where my object is going to fall? 1 Answer

Rotating & fixing the position of an object to one spot 1 Answer

Quick question with a Camera Following script 1 Answer

Moving an array of Vector3 with the GameObject (Path-System) 0 Answers

Unique list of Vector3's 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