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 Allmightysmiter · Jan 27, 2016 at 03:07 PM · camerarotatejoystick

Joystick Rotating wrong after Camera Rotation [Mobile]

I use one joytick to have my player rotating and moving using the Controller.cs script below and I'm using SmoothFollow.cs from the assets to get the camera movement to follow the player the way I want it, however, when I press my button to move the camera behind my player, the rotation corresponding to joystick is off. Can someone help me find what I missing?

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class Controller : MonoBehaviour {
     public float boostMultiplier =2;
 
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         bool isBoosting = CrossPlatformInputManager.GetButton ("Boost");
         Vector3 moveVec = new Vector3 (0, 0, 1);
 
 //This is where joystick rotates player
         if ( CrossPlatformInputManager.GetAxis("Vertical")>0 )
             transform.eulerAngles = new Vector3( 0, Mathf.Asin(CrossPlatformInputManager.GetAxis("Horizontal")) * (180/Mathf.PI), 0 );
 
         if ( CrossPlatformInputManager.GetAxis("Vertical")< 0)
             transform.eulerAngles = new Vector3( 0, 90 + (Mathf.Acos(CrossPlatformInputManager.GetAxis("Horizontal")) * (180/Mathf.PI)), 0 );
 
         //moves cube forward
         if (CrossPlatformInputManager.GetAxis ("Vertical") < 0 || CrossPlatformInputManager.GetAxis ("Horizontal") < 0 || CrossPlatformInputManager.GetAxis ("Vertical") > 0 || CrossPlatformInputManager.GetAxis ("Horizontal") > 0) {
             transform.Translate (moveVec * Time.deltaTime * 15);
             //Debug.Log(CrossPlatformInputManager.GetAxis("Vertical"));
 
 
         }
 
         if (isBoosting == true) {
         
             Camera.main.transform.rotation = Quaternion.Euler(transform.eulerAngles);
         }
 
     }
 
 
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Jan 27, 2016 at 06:59 PM

Your moveVec is in world coordinates. You want to move the object in local coordinates.

So, instead of saying:

 moveVec = new Vector3(0,0,1); 

You probably want something more along the lines of:

 moveVec = transform.forward * 1;

Comment
Add comment · Show 1 · 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 Allmightysmiter · Jan 28, 2016 at 12:39 AM 0
Share

That didn't seem to work, after commenting out the movement code,` transform.Translate (moveVec Time.deltaTime 15);`, I realized the rotation is not correct after switching the camera, so I think it's something to do with that?

avatar image
0

Answer by Allmightysmiter · Feb 02, 2016 at 05:30 PM

I found the solution, I had to take the objects eulerAngles.y when pressing the button and add that to the current eulerAngles, unless the total is greater than 360, then subtract 360 by the added numbers to normalize it. Here's the revised code. using UnityEngine; using System.Collections; using UnityStandardAssets.CrossPlatformInput;

 public class Controller : MonoBehaviour {
     public float boostMultiplier =2;
 
     public float angle = 0;
     public float angleTwo = 0;
     public Vector3 changeRot = new Vector3 (0, 0, 0);
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         bool isBoosting = CrossPlatformInputManager.GetButton ("Boost");
         Vector3  moveVec = Vector3.forward;
 
 
 
 //This is where joystick rotates player
         if (CrossPlatformInputManager.GetAxis ("Vertical") > 0) {
             angle = Mathf.Asin (CrossPlatformInputManager.GetAxis ("Horizontal")) * (180 / Mathf.PI) + changeRot.y;
             if (angle <360){
             transform.eulerAngles = new Vector3 (0, angle, 0);
             }
             else{
                 angle = Mathf.Abs(360 - angle);
                 transform.eulerAngles = new Vector3 (0, angle, 0);
                 
             }
         
         }
     
         if (CrossPlatformInputManager.GetAxis ("Vertical") < 0) {
             angleTwo = (90 + (Mathf.Acos (CrossPlatformInputManager.GetAxis ("Horizontal")) * (180 / Mathf.PI))) + changeRot.y;
             if (angleTwo < 360){
             transform.eulerAngles = new Vector3 (0, angleTwo, 0);
             }
             else{
                 angleTwo = Mathf.Abs(360 - angleTwo);
                 transform.eulerAngles = new Vector3 (0, angleTwo, 0);
 
             }
         }
         //moves cube forward
         if (CrossPlatformInputManager.GetAxis ("Vertical") < 0 || CrossPlatformInputManager.GetAxis ("Horizontal") < 0 || CrossPlatformInputManager.GetAxis ("Vertical") > 0 || CrossPlatformInputManager.GetAxis ("Horizontal") > 0) {
             transform.Translate (moveVec * Time.deltaTime * 5);
         
 
         }
 
         if (isBoosting == true) {
         
             Camera.main.transform.rotation = Quaternion.Euler(transform.eulerAngles);
             changeRot = transform.eulerAngles;
         
 
         }
 
     }
 
 
 }
 
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Move character and rotate camera on same joystick 0 Answers

Move character in direction its facing 1 Answer

Camera rotate - point and click game 1 Answer

Global Fog, rotating the camera by itself 0 Answers

Can the Camera Smoothly rotate AFTER a button is pressed? (Javascript) 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