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 Letters-User · Nov 27, 2016 at 02:25 PM · beginnervirtualreality

How to move forward after staring at one point then stop when looking elsewhere ?

Hello everybody,

My question is quite trikcy but it's because I'm trying some VR possibilities.

What I want is my player to move when looking somewhere, but he's moving only when staring at one point more than 2 seconds. Also I want him to stop moving if he's looking somewhere else, unless he looks for more than 2 seconds then he's walking there.

I don't know if it's clear, but what I want is not using a controller but to let the player move just by looking where he wants to go.

Thanks for your answers,

Comment
Add comment · Show 2
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 Anton-Korhonen · Nov 27, 2016 at 02:33 PM 0
Share

It's not tricky at all, but actually pretty simple. One question i have is are the positions predefined points in your game world or can the player move wherever he/she wants?

I will answer after you have answered my question :)

avatar image Letters-User Anton-Korhonen · Nov 27, 2016 at 02:39 PM 0
Share

Thanks for your answer! I want my player to move where he wants, by just looking :)

1 Reply

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

Answer by Anton-Korhonen · Nov 27, 2016 at 03:05 PM

Note: This is untested code, you can use this as a base to refine your own perfect movement system:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float maxRayDistance = 100f;
     public float waitTime = 2f;
     public float headMovementRange = 3f;
     public LayerMask layerMask;
     private bool waiting = false;
     private Vector3 currentRayPosition;
     private Vector3 initialRayPosition;
     public Transform player;
     private RaycastHit target;
 
     private void MoveToTargetPos(Vector3 position)
     {    
         player.transform.position = initialRayPosition;
     }
     
     private IEnumerator WaitForMovement()
     {
         yield return new WaitForSeconds(waitTime);
         MoveToTargetPos(initialRayPosition);
         waiting = false;
     }
     
     // Update is called once per frame
     void Update () {
         
         Vector3 rayDirection = transform.TransformDirection(Vector3.forward);
         Ray ray = new Ray(transform.position, rayDirection);
 
         // Fire a ray from current head position towards rayDirection with a range of maxRayDistance
         if (Physics.Raycast(ray, out target, maxRayDistance, layerMask))
         {
             if(!waiting)
             {
                 // This is the first time the ray hits the ground, so let's store that position
                 initialRayPosition = target.point;
                 StartCoroutine(WaitForMovement());
 
                 // Set waiting to true so we start the coroutine only once
                 waiting = true;
             }
             
             // Store the current position of the ray
             currentRayPosition = target.point;
             // Calculate distance between the first ray and the current ray
             if(Vector3.Distance(initialRayPosition, currentRayPosition) > headMovementRange)
             {
                 /* 
                     The distance is larger then headMovementRange, so stop the coroutine and let's start
                     from the beginning
                 */
                 StopCoroutine(WaitForMovement());
                 waiting = false;
             }
         }
     }
 }
 

Attach this script to your eye camera and assign the player transform reference and test it out. Basically what it does:

It fires a raycast forwards from the head position and stores the point it hits the ground. Then it starts a timer that waits for the time specified with waitTime The update loop continues to fire rays and calculates the distance between the first ray that hit the ground and the ray that is currently hitting the ground. If the distance exceeds headMovementRange, we start this process from the beginning.

Comment
Add comment · Show 6 · 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 Letters-User · Nov 27, 2016 at 03:07 PM 0
Share

You are such a hero! I owe you a couple of beers here, thanks a lot I will try and test it in my project :)

avatar image Anton-Korhonen Letters-User · Nov 27, 2016 at 03:09 PM 0
Share

No problem! I'm very glad to help :) If you're satisfied, make sure to mark my answer as correct!

avatar image Letters-User Anton-Korhonen · Nov 27, 2016 at 03:53 PM 0
Share

I still have a quick question : How can I have a smooth movement because it's a bit hard here. Can I do it without this teleport but more like a movement where I'm ai$$anonymous$$g ?

Show more comments
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

85 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What's the best logic to make this weapon recoil in VR? 0 Answers

I want to be able to create a script that when I look at an object the player moves to the location of that object. 0 Answers

How do you make the vrtk rotator accommodate more than one axis at a time? 0 Answers

Head tracking stuttering/lag/blur issue with Google Carboard 0 Answers

How do i increase the vertical sensitivity in a VR game? 0 Answers


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