Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 MicDaRoc · Aug 11, 2015 at 05:27 PM · instantiatetransform.positionvector2time.time

GameObject moving left to right AND up

Hi! I've got a rather simple question (I guess), but can't get it to work. I've got a GameObject which moves left to right on a Loop. It's an instatiated Object. I found the script here at UnityAnswers:

 public class ghostMovement : MonoBehaviour {
 
     public float delta = 0.5f;  // Amount to move left and right from the start point
     public float speed = 2.0f; 
     private Vector2 startPos;
 
 
 
     void Start () {
         startPos = transform.position;
 
                     Invoke ("GhostDeath", 1f);
 
 
     }
     
      void Update () {
      Vector2 v = startPos;
         v.x += delta * Mathf.Sin (Time.time * speed);
 
         
         transform.position = v;
         
                         
                  }
         
 
 
     void GhostDeath(){
         Destroy (gameObject);
     }
 }

This works great. But I can't get this object to move up at the same time. Adding a negative gravity in the rigidbody2D doesn't work. Adding something like v.y += Time.time * 0.5f; doesn't work either, the sprite doesn't start to go up at the exact Location and the object starts to appear way too far away from where it should as time goes on.

Cand someone give me an advice on how to solve this Problem? Thanks in advance!

Comment
Add comment · Show 2
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 StaNov · Aug 11, 2015 at 05:46 PM 0
Share

I added v.y += Time.time * 0.5f right below v.x += delta ... and the object moves up (and from left to right ofcourse).

the object starts to appear way too far away from where it should as time goes on

I don't understand this. So the desired behavior is "move from left to right and vice versa and move up, destroy yourself after 1s". Right?

avatar image MicDaRoc · Aug 11, 2015 at 06:02 PM 0
Share

Sorry for not being specific enough. I'm working on a topdown shooter where your character can die and respawns after a short time. When the character dies I want this ghost prefab to appear on the location the character died. The ghost should move up and left to right. After 1 second the ghost disappears (the gameobject gets destroyed). Since you get respawned again I need the "new" ghost appearing again at the exact location where the player died again. With the solution you suggested the first ghost appears a bit too high above the players death location. The second ghost appears a bit more far away from the new death location and so on. I hope you understand what I mean.

1 Reply

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

Answer by StaNov · Aug 11, 2015 at 06:31 PM

The second ghost appears higher, because the time goes on. At the beginning, the time is zero, so your ghost moves up from y=0. But if you spawn another ghost later, the time is no more zero. It's something positive, like 20 (in seconds).

You can save time of the animation start when you spawn a ghost. And as time use difference between current time starting time.

 private float startTime;

 void Start () {
      startPos = transform.position;
      startTime = Time.time;
 }
  
 void Update () {
     Vector2 currentPos = startPos;
     float currentTime = Time.time - startTime;

     currentPos.x += delta * Mathf.Sin (currentTime * speed);
     currentPos.y += delta * currentTime * 0.5f; 
      
     transform.position = currentPos;
 }
Comment
Add comment · Show 3 · 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 MicDaRoc · Aug 11, 2015 at 06:42 PM 0
Share

Thx for your reply. Unfortunately I'm nit able to check this out now, but I will test it tomorrow. Thanks a lot.

avatar image MicDaRoc · Aug 12, 2015 at 02:55 PM 0
Share

Yes! Thx a lot! Works just like I imagined it!

avatar image StaNov · Aug 12, 2015 at 03:32 PM 0
Share

Glad to help :)

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

25 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

Related Questions

Change animation at runtime with Mecanim 0 Answers

Not able to fire projectiles at mouse position 2 Answers

Why time in instantiate object with this script doesn't work? 0 Answers

Bullets will instantiate but they won't move. 1 Answer

Instanitiate PreFabs and let them move to different locations 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