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 sqil · Feb 11, 2015 at 10:29 AM · physicsobjecttravel

When an object travels a certain amount, it comes back to it's origin position

I have a shot prefab, I want it to travel some distance after it was created and if it reaches a certain distance it comes back to it's original position, I have played around with the code but I couldn't get it to work. The stuff in start should determine that when the object is created it travels at that speed in the direction, but the code in the update function stops it from doing that and at object creation it starts to move it to the player position. How to fix that?

 using UnityEngine;
 using System.Collections;
 
 public class movement : MonoBehaviour {
     public float boostSpeed;
     private GameObject player;
     public float speed;
     // Use this for initialization
     void Start () {
         player = GameObject.FindGameObjectWithTag("Player");
 
         if(player.transform.localScale.x == -1){
             rigidbody2D.AddForce(transform.right * (boostSpeed * -1f) );
         }
         else{
             rigidbody2D.AddForce(transform.right * boostSpeed);
 
         }
     }
     
     // Update is called once per frame
     void Update () {
         boostSpeed = 1f * 5f;
         float distance = Vector3.Distance(player.transform.position, transform.position);
         float step = speed * Time.deltaTime;
         if(transform.position.x - player.transform.position.x != 0f){
             transform.position = Vector3.MoveTowards(transform.position, player.transform.position, step);
         }
         
 }

Comment
Add comment · Show 1
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 Joyrider · Feb 11, 2015 at 10:32 AM 1
Share

Well yes, in the startfunction you're using physics (which computes displacement and changes the transform position), where as in the update you manually set the transform's position... So in the end... you just get your manual setting of the transform's position at every frame...

I guess I just don't get what that user position code is doing there... that or I didn't get what you were trying to do.. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Feb 11, 2015 at 06:15 PM

Your if in Update() will most likely always be true:

 if (transform.position.x - player.transform.position.x != 0f)

Instead you could save the projectiles starting position in Start() and do a "Vector3.Distance > something" in Update. If "that's" true, either start a Coroutine to move back, or toggle a boolean that distinguishes between still being physically on the way as it was or traveling back with your MoveTowards. Set the rigidbody to isKinematic for this if you want to reliably change its position. Otherwise you're screwing with the physics engine.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

swapping out an object on collision 2 Answers

2.5D Side-scroller Mouse Drag Objects 1 Answer

Dense joid joining! Help 0 Answers

Collision with a coin 0 Answers

Object movement impacted by another 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