0

So, here is what i have: SteamVR's hand gameobject 3D sphere.

what i want: The sphere to move to same direction/position as the hand does, but it to move further with a multiplier. E.g. i move the VR controller and the hand moves 1 unit. I want that the sphere moves to the same direction in a same amount of time but e.g. 2 units. how do i do this? i tried simple

sphere.transform.position = ControllerToFollow.position +2f;

but then the sphere is always offset.

jakkis
  • 73
  • 1
  • 9

2 Answers2

0

position is a Vector3, which is essentially 3 floats - you can't plus a Vector3 with a float unless you overload the + operator. Otherwise what you can do is the following:

Vector3 followPos = new Vector3(ControllerToFollow.position.x + 2f,
                                ControllerToFollow.position.y + 2f, 
                                ControllerToFollow.position.z + 2f);
sphere.transform.position = followPos;

If you only want it to follow on one axis, then you can do the following:

Vector3 followPos = new Vector3(ControllerToFollow.position.x + 2f, // Follow on x Axis
                                ControllerToFollow.position.y, // Y axis is the same
                                ControllerToFollow.position.z); // X Axis is the same
sphere.transform.position = followPos;

Edit: I think I understand your problem better now. Here's a better version.

if (Vector3.Distance(sphere.transform.position, ControllerToFollow.position) >= 2f)
{
    // Code that makes the sphere follow the controlling
}
  • This still does not solve the problem. That +2 was simply to demonstrate that adding values to coordinates does not work as the sphere is still constantl offset of the controller. I need a way to magnify the controllers movement. – jakkis Feb 05 '20 at 14:40
  • I edited my answer to include an example you can use in the code. – Schalk van der Merwe Feb 05 '20 at 14:43
  • Again, Making the sphere to follow the controller is not the problem. Making the sphere move further is the problem. All i can do with that is to offset the sphere compared to the controller. The amount of movement stays the same which is not what i am aiming to do here – jakkis Feb 05 '20 at 14:50
  • Then just multiply the ControllerToFollow.position by 2f. The `*` operator is overloaded by Unity already, so it acts as a scalar – Schalk van der Merwe Feb 05 '20 at 14:56
  • @SchalkvanderMerwe if you would do `ControllerToFollow.position * 2` it would return a position relative to the origin of the Unity coordinate system. Doesn't sound like that's the goal – derHugo Feb 05 '20 at 17:26
0

Just track the movement Delta of the hand and multiply it by a certain multiplier.

At the beginning of the manipulation store

private Vector3 lastControllerPosition;

...

lastControllerPosition = ControllerToFollow.position;

then in every frame compare

var delta = ControllerToFollow.position - lastHandPosition;
// Don't forget to update lastControllerPosition for the next frame
lastControllerPosition = ControllerToFollow.position;

Now in delta you have a movement of the controller since the last frame. So you can assign it to the sphere with a multiplier using Transform.Translate

sphere.transform.Translate(delta * multiplier, Space.World); 

or simply using

sphere.transform.position += delta * multiplier;
derHugo
  • 83,094
  • 9
  • 75
  • 115