- Home /
Move a cube around a center point
Hi,
As 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?
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?
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.
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.
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.
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.
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
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?
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
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).
Your answer
Follow this Question
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