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 nathan · Jan 14, 2013 at 04:23 PM · movementcube

Move a cube around a center point

Hi,

Asalt text you can see from the picture, i want to use a sphere to push move the cube and the original position of cube is the center. When i move sphere close to the center , the cube will move relatively until the sphere pass the center. And the cube will move to the reverse position. How can i detect if the sphere pass the center or not? Any thought on that?

img_20130115_130426.jpg (171.5 kB)
Comment
Add comment · Show 4
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 robertbu · Jan 14, 2013 at 05:59 PM 0
Share

The picture was helpful, but even with it, I have difficulity understanding how you want everything to move. What do you mean by "push"...are you going to use the physics engine and collisions or are you going to move them in code? What does "past" mean in 3D space...moving across the x, y, or z axis or something else? Does the center point move over time? When you say "reverse position" are you saying the object bounces so that its velocity is reversed?

avatar image Lovrenc · Jan 14, 2013 at 06:08 PM 0
Share

Pic is 2 small.

avatar image nathan · Jan 14, 2013 at 06:10 PM 0
Share

Thanks for your response. I know i might not be clear. What i want to do is that when i use finger to move sphere close to some level, the cube will move accordingly. I am using physics engine and there is trigger on the sphere to detect if any thing is at its radius area. Like this cube here, when i move the sphere close to the cube, the cube will enter sphere's trigger. At this point, the cube will be moved away to keep certain distance with sphere. That what i wanna do.

avatar image nathan · Jan 14, 2013 at 06:13 PM 0
Share

the center point is the initial position of cube here. And when i want sphere to pass this center, the cube's position will be at opposite direction refers to sphere. The center point will never move over time, its kind of fixed point.

1 Reply

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

Answer by robertbu · Jan 15, 2013 at 05:22 AM

I’m still fuzzy on the details but let me take a shot. I assuming there are multiple cubes (one for each level), and that they move on the X axis, but not the Y or Z axis. When the finger slides into a new level you have a trigger that identifies the cube at that level. At that point you give the cube the transform of the sphere (set a public variable) and call a script method on the cube to start maintaining the distance. Based on position, you decide if the cube is to be to the left or right of the sphere. Assuming the sphere always starts in the center, the side the finger lands on will determine which way the cube moves. At each frame you do a calculation something like:

 public class CubeMover : MonoBehaviour
 {
         public bool bLeft = true;
         public Transform transSphere;
         
         float fSepDist = 3.0f;  
         float fMaxMovePerFrame = 0.1f;
         
         void Update ()
         {
                 float fPos;
                         
                 if (bLeft)
                         fPos = transSphere.position.x - fSepDist;  
                 else
                         fPos = transSphere.position.x + fSepDist;  
                         
                 // This will cause the block to slide rather than immediately move
                 fPos = Mathf.MoveTowards (transform.position.x, fPos, fMaxMovePerFrame);  
                 
                 Vector3 v3NewPos = transform.position;
                 v3NewPos.x = fPos;
                 transform.position = v3NewPos;
         }
 }

You will need to disable the movement when the sphere is in a different level.

Comment
Add comment · Show 7 · 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 nathan · Jan 15, 2013 at 05:16 PM 0
Share

Hey, thank you very much for your answer man. But i might not clear enough. I updated my picture and want it to be more clear. See, The cube and sphere can move around x, y and z axis. $$anonymous$$y picture here only demonstrate the along x axis case to make it simple. And the sphere, cube and center point should be always at a line and the distance between cube and sphere should always keep the same. It should feel like that when i move the sphere toward to cube with my finger, the cube will move accordingly. When the sphere pass through the center point(the initial position of cube), the cube will bounce back to the reversed direction. And when i move sphere away center, the cube will eventually move back to center which is its original position.

avatar image nathan · Jan 15, 2013 at 05:23 PM 0
Share

There is trigger around the sphere, as long as the cube is in the range of this trigger, it will move like the picture. When the sphere move away, the cube won't be affected by trigger and will back to its original position

avatar image robertbu · Jan 15, 2013 at 06:27 PM 0
Share

You mention the cube is free to move in the x, y and z axis. So if I'm within range, and the sphere is on the left size of the center point moves down, does the cube rise on the right? What causes z movement?

avatar image nathan · Jan 15, 2013 at 06:38 PM 0
Share

Yes, exactly. When the sphere is down , the cube will rise because the center,cube and sphere should always at a line. I will deal with z movement late, now i only want to fix this problem at x,y

avatar image robertbu · Jan 15, 2013 at 07:24 PM 0
Share

One way to do this movement is to project a ray from your sphere through the center and then place you cube a certain distance away along that ray.

 using UnityEngine;
 using System.Collections;
 
 public class GoTo : $$anonymous$$onoBehaviour
 {
     public Vector3 v3Pos = Vector3.zero;
     public Vector3 v3Center = new Vector3(0.0f, 1.0f, 0.0f);
 
     void Update ()
     {
     transform.position = Vector3.$$anonymous$$oveTowards(transform.position, v3Pos, 0.2f);    
     }
 
 }

The above script should be attached to the cube. v3Center can be changed and is specific to that cube.

 using UnityEngine;
 using System.Collections;
 
 public class Influence : $$anonymous$$onoBehaviour
 {
     public GameObject goTest;
     
     private GameObject goInfluence = null;
     GoTo gotoObj = null;
     Vector3 v3T = Vector3.zero;
     
     private float fSepDist = 3.0f;
 
     void Start ()
     {
         StartInfluence (goTest);
     }
     
     void Update ()
     {
         Ray ray = new Ray(transform.position, gotoObj.v3Center - transform.position);
         gotoObj.v3Pos = ray.GetPoint (fSepDist);
     }
     
     public void StartInfluence(GameObject go)
     {
         if (gotoObj != null)         // Sends last object to center
             gotoObj.v3Pos = gotoObj.v3Center;
         goInfluence = go;
         if (go != null)
             gotoObj = goInfluence.GetComponent<GoTo>();
     }
 }

Attach to the sphere. StartInfluence() connects the sphere to a specific cube. Any previous cube should return to its center (untested).

Show more comments

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

10 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

Related Questions

Objects cloning on movement? 1 Answer

A node in a childnode? 1 Answer

Change int value for 30 sec... 1 Answer

Sphere move only on ground 0 Answers

Unity Always Moving Right 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