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 /
avatar image
0
Question by eyeofthetitan · Dec 10, 2016 at 07:19 AM · unity 5vector3lerptransform.positionvector3.position

How can I transform 3 objects to swap positions, but make sure they aren't moving to a position an object is already going to?

I have applied a code to 3 objects. So far this code switches three cups around so that they swap places. The code gets the start position of the cup, and then the end position of each cup. All 3 end positions are actually the start positions of the other cups, since I want them to swap to the same exact positions as the other cups. The update function makes it so that if the cup's end position (that is chosen at random) DOES NOT equal it's start position, it will move to one of the other 2 spots that were not it's start position, randomly.

I am having trouble with figuring out a couple of things and have tried a few things, but I don't think I'm getting the code right. I want to:

Make the cups randomly move to their new spot, but not move a cup to a spot that another cup is already moving to. (Currently when the animation plays, 2 cups potentially move to the same spot of the 3 spots).

I am just not sure what direction to go in. I also would like this animation to keep playing and for the cups to keep randomly swapping their spots. It would be helpful to get a point in the direction of figuring that out as well.

This is what I want the final outcome to resemble: http://i249.photobucket.com/albums/gg240/OldNewby4507/shell-game-animated.gif

Thanks in advance.

 using UnityEngine;
     using System.Collections;
     using UnityEngine;
     using System.Collections;
 
     public class NewBehaviourScript : MonoBehaviour
     {
         private Vector3 startPos;
         private float lerp = 0, duration = 1;
         public Vector3[] endPos = new Vector3[3];
         int index;
         Vector3 theNewPos;
 
         void Start ()
         {
             startPos = transform.position;
             endPos [0] = GameObject.Find ("object1").transform.position;
             endPos [1] = GameObject.Find ("object2").transform.position;
             endPos [2] = GameObject.Find ("object3").transform.position;
             index = Random.Range (0, endPos.Length);
             theNewPos = endPos[index];
 
         }
 
         void Update() {
 
             if (theNewPos != startPos) { 
                 lerp += Time.deltaTime / duration;
                 transform.position = Vector3.Lerp (startPos, theNewPos, lerp);
 
             }
 
         }
     }
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

· Add your reply
  • Sort: 
avatar image
0

Answer by AlwaysSunny · Dec 10, 2016 at 07:19 AM

A good question!

First thing I would do is put the target-selecting logic you're talking about in a manager class instead of on scripts for the individual objects. Think of it as centralizing control. Suddenly you have access to all three objects within the same context (the same script) and can leverage this to work with your scene information more efficiently. This will be a common theme in your programming experience. :)

Having a manager that helps you perform tasks on related objects is nearly always a good strategy for organization. In this case, your ShellManager could actually replace your Shell script altogether, or you could put only the target-picking logic in the manager.

With all three shells in the same scope, it becomes very simple to assign each one a guaranteed-unique target. Have your shells in a collection. When it's time to pick new targets, for each shell, pick a random element of the collection. If it's the same shell you're trying to assign a target for, or if you've already picked that element, pick another. Assign the randomized, guaranteed-unique shell you've just found as the target position.

Getting consistent, quality animations is too big a topic for this UA post, but feel free to make another when the time comes. Learning to repeat said animations is not really a valid UA question because that information is easily available.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to transform my character's local position on a GetKey. 0 Answers

Lerp between two direction vector 0 Answers

Using Raycast hit to set vector position then moving player to that position 2 Answers

Move a object along a vector 1 Answer

Object shakes on collide! 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