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 /
avatar image
0
Question by timobugz3 · Feb 15, 2020 at 11:01 AM · unity 5rotationmovementvrdirection

How to walk in the direction the player is looking at in the Vive headset

Hi,

I am trying to make player walk in the direction he/she is looking at in the Vive headset.

Before I put it into VR, I am using c# code that uses the mouse to control which direction the player is looking at and use "W" key to walk towards that direction and it works. After I transfer into the HTC Vive. The player can only move towards the left no matter which direction he/she is looking at inside the headset.

I have used three scripts to govern the movement and they are 1. Camera rotation 2. Movement Controller 3. HeadBobber

Script MovementController:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class MovementController : MonoBehaviour
     {
         public float speed;
         // Start is called before the first frame update
         void Start()
         {
             
         }
     
         // Update is called once per frame
         void Update()
         {
             if(Input.GetKey(KeyCode.W))
             {
                 this.transform.position += this.transform.localRotation * Vector3.forward * speed * Time.deltaTime;
             }
     
         }
     
     }

Script CameraController:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraController : MonoBehaviour
 {
     public GameObject eye;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         float rotationSpeed = 5.0f;
         float mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
         float mouseY = Input.GetAxis("Mouse Y") * rotationSpeed;
 
         this.transform.localRotation = Quaternion.Euler(x: 0, y: mouseX, z: 0) * this.transform.localRotation;
         eye.transform.localRotation = Quaternion.Euler(x: mouseY * -1, y: 0, z: 0) * eye.transform.localRotation;
     }
 }

HeadBobber Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HeadBobber : MonoBehaviour
 {
     public GameObject eye;
     private float timer = 0.0f;
     float bobbingSpeed = 0.18f;
     float bobbingAmount = 2f;
     float midpoint;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         midpoint = eye.transform.position.y;
         float waveslice = 0.0f;
         float vertical = Input.GetAxis("Vertical");
 
         Vector3 cSharpConversion = transform.localPosition;
 
         if(Input.GetKey(KeyCode.W))
         {
             waveslice = Mathf.Sin(timer);
             timer = timer + bobbingSpeed;
             if (timer > Mathf.PI * 2)
             {
                 timer = timer - (Mathf.PI * 2);
             }
         }
         else
         {
             timer = 0.0f;
         }
 
         if (waveslice != 0)
         {
             float translateChange = waveslice * bobbingAmount;
             translateChange = vertical * translateChange;
             cSharpConversion.y = midpoint + translateChange;
         }
         else
         {
             cSharpConversion.y = midpoint;
         }
 
         transform.localPosition = cSharpConversion;
     }
 }

Notice that in the hierarchy I have put my camera rig from steamvr under empty object player which contains all these script.

Your help would be so much appreciated. Thanks!

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 JPhilipp · Feb 15, 2020 at 07:38 PM 0
Share

Just a related note: $$anonymous$$oving the camera forward in VR, if it's your only option, will make some people sick (because of the different signals their brain gets in terms of eyes vs balance/ acceleration). As optional feature it's very welcome, if you have a teleport laser type system as well.

avatar image timobugz3 JPhilipp · Feb 16, 2020 at 07:09 PM 0
Share

Hi JPhilipp,

Thank you for pointing out that. $$anonymous$$ay I know what would be the more natural options apart from moving the camera forward, which could also be close to the movement behavior in reality?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · Feb 17, 2020 at 07:02 AM

In VR the camera rotation has another component to it.

Try disabling the head bobbing script for debugging this, then try something like this in the Movement controller. And go on from there.

 this.transform.position += Camera.main.transform.rotation * Vector3.forward * speed * Time.deltaTime;
Comment
Add comment · 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

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

315 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 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 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 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

Look rotation viewing is zero warning 0 Answers

Character rotating to previous rotation 1 Answer

Temporarily move object specific distance along own axis 0 Answers

Character rotation and move to mouse click point with speed 0 Answers

How to make an object go the direction it is facing? 6 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