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 /
avatar image
0
Question by Neetru · Apr 15, 2017 at 12:08 PM · transformpositionvector3

Stop transform position moving objects together

I'm using transform position to make an object bob up and down, this works fine, however if there are two objects using the same script they move towards each other. How do I stop this?

Code: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Rotator : MonoBehaviour {
 
     private Vector3 startPos;
     private Vector3 endPos;
     private static Vector3 moveTo;
     public static float moveSpeed = 0.005f;
 
 
     private void Start()
     {
         startPos = this.transform.position;
         endPos = this.transform.position - (new Vector3(0, 0.3f, 0));
     }
     
     // Update is called once per frame
     void Update () {
         transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
 
         if(transform.position == startPos)
         {
             moveTo = endPos;
         }
 
         if(transform.position == endPos)
         {
             moveTo = startPos;
         }
 
         transform.position = Vector3.MoveTowards(this.transform.position, moveTo, moveSpeed);
     }
 }
 
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 Neetru · Apr 15, 2017 at 04:47 PM 0
Share

As a follow up, when the objects use two different scripts that are the same, they do not move together.

1 Reply

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

Answer by HammerCar · Apr 15, 2017 at 05:11 PM

I tested your code and found the problem. The moveTo variable is static. It was messing up the movement of the objects. Without the static modifier the code worked fine.

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 Neetru · Apr 15, 2017 at 06:54 PM 0
Share

Thank you! If it's not too much to ask, what's the difference between a static and a non-static variable? Sorry I'm very new to Unity and C#.

avatar image HammerCar Neetru · Apr 15, 2017 at 07:07 PM 0
Share

If a variable is static the value of that variable will be same on all instances of the script.

So in your original script first the object A set the static variable moveTo to its endPos. Next the object B overwrote the moveTo variable with its own endPos. After this both of the objects were going to the same endPos.

When you used two different scripts they weren't instanses of the same script so they didn't share the same value.

avatar image Neetru HammerCar · Apr 15, 2017 at 07:22 PM 0
Share

Ohh, I see, many thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Offset against Rotated Object,Applying Offset to a Rotated Transform 1 Answer

Vector3 sets to different coordinates than specified 1 Answer

Play animation as the value changes from 0 to 1 with changing GameObject's position in unity 0 Answers

Vector3 Transform.Position Not Working 2 Answers

Script to make Camera follow the player C# 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