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 /
avatar image
1
Question by Narv · Jun 13, 2013 at 05:44 PM · rotationdirectionjoystick

Implementing a simple on-screen joystick for mouse

So I am trying to implement a simple on screen joystick with the mouse (and later move it to touch). I am aware that the standard assets mobile has the joysticks but I cannot test them with the mouse and they are in JS and i'm trying to do it in c#. Also for my own person reference in understanding how it all works better by doing it myself (for something simple like this.

I currently have the following setup:

plane with a capsule on it. The capsule represents the player. The main camera is stationary

a separate "GUI" area with a small plane, an alpha = 50 background plane, and a "gui" camera that only sees this stuff, even though it's off to the side.

What I have working:

When you use the little on screen "button", it moves the capsule. However when I comment out the code regarding rotation, it moves in the correct direction (so if I move the plane up, it moves the character forward.. and if I move it down, it moves the plane towards me, etc). When I try to introduce rotation (if this was a player, you probably don't want it side stepping all over the place instead of just turning to the left and going left), it seems to go all over the place because I assume my calculations are off.

The Theory

So the theory behind it I was going with was, you have the anchor point of the button, and the raycast hit point on the plane of where the mouse is. This would cause a direction from point A to point B and based on the length (magnitude) of the vector then it affects speed. The speed part seems to be working just fine it's the direction part i'm having trouble with. When it rotates the model it seems to no longer want to go in the direction vector, but instead wants to go in the direction relative to the rotation. I tried debugging with Debug. DrawLine and it pointed to the rotation and direction points being not where I would expect them to be but I can't seem to figure out why.

 using UnityEngine;
 using System.Collections;
 
 public class MoveCapsule : MonoBehaviour {
 
     public Transform theCapsule;
     public Camera cam;
     public float speed = 5.0f;
     
     private Transform trans;
     private Vector3 main;
     
     
     void Start () {
        trans = transform;
         main = trans.position;
         
     }
     
     void Update () {
     
         if(Input.GetMouseButton(0))
         {
             RaycastHit hit;
             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
             
             if(Physics.Raycast(ray, out hit))
             {
                 float mag = (main - hit.point).magnitude;
                 
                 Vector3 direction = (hit.point - main).normalized;
                 Vector3 rotation = direction;
                 
                 Debug.DrawLine(main, hit.point);
                 Debug.DrawLine (theCapsule.position, rotation);
                 
                 if(mag < 0.35f)
                    trans.position = hit.point;
                 
                 
                 if(mag > 0.35f)
                     mag = 0.35f;

                             // used this code to project the Y axis to the Z so 
                             // that it doesn't go up, and only left/right/forward/back.
                             // reverse X direction because camera is on the wrong 
                             // side I think but otherwise controls are backwards
                 direction.z = direction.y;  
                 direction.y = 0;  
                 direction.x = -direction.x;
                 
 
                 rotation.y = 0;
                 
                 
                 
                 theCapsule.rotation = Quaternion.Slerp(theCapsule.rotation, Quaternion.LookRotation(rotation), 0.1f);                
                 theCapsule.Translate(direction * mag * Time.deltaTime * speed);                
             }
 
         }  
         
         // on mouse up, reset position
         if(Input.GetMouseButtonUp(0))
         {
             trans.position = main;
         }
         
     }
 }


Thanks to any input you guys have.

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

14 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

Related Questions

How to get 360 directions from [joystick/mouse] instead of 8? 1 Answer

Player rotating in direction of joystick doesn't work, why?,Rotate Player in direction of Joystick has too little impact 0 Answers

Character rotating to previous rotation 1 Answer

After rotate with joystick, rotate resetting,joystick rotate correction 0 Answers

Camera/Direction Rotation 2 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