Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 pradyumnp508 · Aug 18, 2020 at 07:21 AM · camera-movementplayer movementaugmented-reality

How to make A Joystick Movement Controller that moves the character based on the camera Angle (ARCore)

Hi!

I am working in a ARCore Game project. In a scene, Where user will spawn the Playable Character, I want the user to be able to move the character using a joystick. But the catch is, since I am making it with Augmented reality, that means User will move around the area with their phone.

So, I want the joy stick to work such that, if the User Pushes "Vertical Up" the character should always walk Away from the camera. In other worlds I want the joystick to take the Phones's camera Origin and Orientation as its parent rather than taking the Game-Scene's Origin as Parent when it comes to defining X, Y and Z Axis.

Also I have added a second Joystick that user can use to rotate the Character around the Character's axis to shoot enemies at 360 degrees around it.

I found "THIS" tutorial that I Implemented. When I tested it, the object is moving and rotating just fine, But it is not moving the way I mentioned above. It seems to go forward in the same direction regardless of where I position the camera.

Here's the Scripts I have written (Movement_Controller.cs):

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement_Controller : MonoBehaviour
 {
     public FixedJoystick moveJoystick;
     GameObject objMove_Joystick;
 
     public FixedJoystick LookJoystick;
     GameObject objLook_Joystick;
 
 
     private static float hoz;
     private static float ver;
 
     Vector3 direction;
     void Update()
     {
         if (NewTap2Place.objectinScene == true)
         {
             MoveJoystick();
             look_JoyStick();
         }
     }
 
     void MoveJoystick()
     {
         objMove_Joystick = GameObject.FindGameObjectWithTag("Movement_Stick") as GameObject;
         moveJoystick = objMove_Joystick.GetComponent<FixedJoystick>();
 
         hoz = moveJoystick.Horizontal;
         ver = moveJoystick.Vertical;
 
         Vector3 convertedXY = ConvertWithCamera(Camera.current.transform.position, hoz, ver);
 
         direction = new Vector3(convertedXY.x, 0, convertedXY.y);
 
       
         //Debug.Log(direction);
     }
 
     void look_JoyStick()
     {
         objLook_Joystick = GameObject.FindGameObjectWithTag("Look_Stick") as GameObject;
         LookJoystick = objLook_Joystick.GetComponent<FixedJoystick>();
 
         hoz = LookJoystick.Horizontal;
         ver = LookJoystick.Vertical;
 
         Vector3 convertedXY = ConvertWithCamera(Camera.current.transform.position, hoz, ver);
 
         direction = new Vector3(convertedXY.x, 0, convertedXY.y);
 
         Vector3 LookatPosition = transform.position + direction;
         transform.LookAt(LookatPosition);
         //Debug.Log(direction);
     }
     private Vector2 ConvertWithCamera(Vector3 cameraPos, float hor, float ver)
     {
         Vector2 joyDirection = new Vector2(hor, ver).normalized;
         Vector2 camera2Pos = new Vector2(cameraPos.x, cameraPos.y);
         Vector2 cameraToPlayerDirection = (Vector2.zero - camera2Pos).normalized;
         float angle = Vector2.SignedAngle(cameraToPlayerDirection, new Vector2(0, 1));
         Vector2 finalDirection = RotateVector(joyDirection, -angle);
         return finalDirection;
     }
 
     private Vector2 RotateVector(Vector2 v, float angle)
     {
         float radian = angle * Mathf.Deg2Rad;
         float _x = v.x * Mathf.Cos(radian) - v.y * Mathf.Sin(radian);
         float _y = v.x * Mathf.Sin(radian) + v.y * Mathf.Cos(radian);
 
         return new Vector2(_x, _y);
     }
 }
 

I am using Unity: 2019.4, ARFoundation: 4.1.0 I have also tagged the ARCamera as Main Camera in its inspector.

Also I am instantiating the object after the user taps on the screen, does that affect the coordinate system?

Thanks in advance!

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

226 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

Related Questions

How do you move the camera with the player. Whats wrong with my code? 0 Answers

Touch Camera Movement and Transform Player on Touch 0 Answers

Help about understanding ceratin command problem 1 Answer

Multiple camera and camera relative player movement 0 Answers

how to make camera follow player after a delay 1 Answer


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