1
\$\begingroup\$

I'm trying to move a Third-Person character by looking at random objects in the scene using the googleVR Rectile, what I had in mind is lerping the character to the collision point position. Any Help how to do this ?

\$\endgroup\$
  • \$\begingroup\$ Can you clarify whether you're trying to move the player's viewing position, or the position of a character the player is looking at from a third-person perspective? \$\endgroup\$ – DMGregory Jul 31 '17 at 21:32
  • \$\begingroup\$ its a third-person character actually \$\endgroup\$ – Saifiction Jul 31 '17 at 23:27
  • \$\begingroup\$ This kind of detail is important to include in your question. ;) \$\endgroup\$ – DMGregory Jul 31 '17 at 23:27
1
\$\begingroup\$

In first person mode, where the user is moving themselves by looking around, the reticle is always in the center of the screen. So you can just as easily move forward to achieve the same effect.

Either with a straight constant movement:

void Update() {
    this.transform.Translate(transform.forward * Time.deltaTime);
}

or do a Raycast to ensure the player is looking at one of your specified objects:

void Update() {
   if(Physics.Raycast(transform.position, transform.forward,
        Mathf.Infinity, LayerMask.GetMask("MyObjectLayer")))
    {
        this.transform.Translate(transform.forward * Time.deltaTime);
    }
}

Where all the objects that cause the player to move forward are in the layer 'MyObjectLayer".

In a third person scenario, or if you've moving something other than the player, you'll want to get the position of the hit, so you can move the target object toward that position.

For that, use this version of Raycast:

public GameObject targetObject;

void Update() {
    RaycastHit hit;
    if (Physics.Raycast(transform.position, transform.forward, out hit,
        Mathf.Infinity, LayerMask.GetMask("MyObjectLayer")))
    {
        Vector3 direction = MoveTowards(TargetObject.transform.position,
                                        hit.point, Time.deltaTime);
        targetObject.transform.Translate(direction);
    }
}

Where targetObject is the object you want to move toward the hit point.

\$\endgroup\$
  • \$\begingroup\$ Is there anyway that I can get the collision point ? \$\endgroup\$ – Saifiction Jul 31 '17 at 20:08
  • \$\begingroup\$ Yes, of course, but there's little point as far as the movement goes. I've updated the answer. \$\endgroup\$ – MichaelHouse Jul 31 '17 at 20:20
  • \$\begingroup\$ I think this assumes a first-person perspective — that the character you're moving is also your viewpoint. If it's a third-person perspective like Lucky's Tale, then moving the character pawn toward the gaze position is not the same as moving the viewpoint forward. \$\endgroup\$ – DMGregory Jul 31 '17 at 21:07
  • \$\begingroup\$ @DMGregory Yes, I suppose I hadn't considered that scenario. That would be rather ambitious with Google Cardboard, but certainly possible. \$\endgroup\$ – MichaelHouse Jul 31 '17 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.