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 ConsciousCoder · Jan 07, 2013 at 06:47 AM · c#transformpositionmovey

C# move y position of object not working

Hi im having a problem getting this script to properly reset the y position of the object after the condition is met. When the condition is met it only changes the value but not the actual y position of the object. So im wondering why is this happening? Thank You.

 public GameObject sceneManager;
 public Transform collisionExplosion;
 
 public Vector3 position;
 public float posY;
     
     void Awake ()
     {
         posY = transform.position.y;
         
     }
     // Update is called once per frame
     void Update () 
     {
         posY = transform.position.y;
         
 
         move();
         reset();
         
     }
 
     void move ()
     {
         float shipSpeed = 7.0f;
         float totalSpeed = shipSpeed * Time.deltaTime;
         transform.Translate(Vector3.down * totalSpeed);
     }
 
     void reset ()
     {
              
 
         if(posY <= -6)
         {
             position = new Vector3(Random.Range (-6.5f,6.5f),8,0);
             
         }
 
     } 
 
     void OnTriggerEnter(Collider other)
     {
                 
 
         if(other.gameObject.tag == "Player")
         {
             
     
         //Create explosion on ship collision
             Instantiate(collisionExplosion, other.transform.position, other.transform.rotation);
     
     
         //Reset the position of the enemy
             position = new Vector3(Random.Range (-6.5f,6.5f),8,0);
         }
     }
 

     
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Maulik2208 · Jan 07, 2013 at 07:07 AM

 public GameObject sceneManager;
 public Transform collisionExplosion;
 
 public Vector3 position;
 public float posY;
 
     void Awake ()
     {
        posY = transform.position.y;
 
     }
     // Update is called once per frame
     void Update () 
     {
       // posY = transform.position.y;
       // Update is called after every frame so posY is getting transform.position.y after each frame so it's better to use this in start function if you want to pass the y position just once.....
 
 
        move();
        reset();
 
     }
 
     void move ()
     {
        float shipSpeed = 7.0f;
        float totalSpeed = shipSpeed * Time.deltaTime;
        transform.Translate(Vector3.down * totalSpeed);
     }
 
     void reset ()
     {
 
 
        if(posY <= -6)
        {
          transform.position /*position*/ = new Vector3(Random.Range (-6.5f,6.5f),8,0);
 
        }
 
     } 
 
     void OnTriggerEnter(Collider other)
     {
 
 
        if(other.gameObject.tag == "Player")
        {
 
 
        //Create explosion on ship collision
          Instantiate(collisionExplosion, other.transform.position, other.transform.rotation);
 
 
        //Reset the position of the enemy
         transform.position/* position*/ = new Vector3(Random.Range (-6.5f,6.5f),8,0);
        }
     }
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 Maulik2208 · Jan 07, 2013 at 07:16 AM 0
Share

Tried to fix the issue have a look at above script....if Still your problem not solved then please provide some more info regarding script.....Enjoy and Don't forget to mark the answer if found useful

avatar image
0

Answer by ConsciousCoder · Jan 07, 2013 at 08:15 AM

Thank you so much for your quick response. Appreciate it.

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 Maulik2208 · Jan 07, 2013 at 08:21 AM 0
Share

Glad that you found it useful......Cheers...Enjoy.....here all the people are trying to solve the problems as soon as possible....So, have a Happy Coding........

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move Object A's Y position towards Object B's Y position 1 Answer

Guided missile help c# 1 Answer

Keep a min distance away from object while moving towards it 0 Answers

Issue's With Positioning 0 Answers

An Efficient Way To Follow Transform's Position? 3 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