• 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 Molmez · Feb 23, 2017 at 05:54 PM · rotationmovementcamera-movementaxisrotation axis

Rotate Object Axis On Swipe Cam Rotation - Android

Hey peeps,

I am trying to make a game similar to Kula world but not exactly as im not quite sure how to custimize the gravity to the level as of yet.

For now I am just sticking to one surface of movement. Using mobile tilt to move forward and backward based on y tilt.

I have incorporated a swipe function to rotate the camera. I want to use this to then move froward along a new path that the user has rotated the camera to view.

I have tried various solutions based on similar scenarios but none match what im doing and am slightly confused.

Basically when the user rotates the camera left or right I want the forward and back vectors to adjust to the view of the camera.

So in this simple screenshot you would move forward to the cross section, swipe to rotate camera then move forward along new path. However right now when you swipe camera it changes nothing you now appear to move left and right. I tried rotating the balls Y axis with camera Y but this causes some crazy behavoir and doesnt effect direction.

Thank You!

![alt text][1] [1]: /storage/temp/88679-testball.jpg

Camera Code:

 public class CameraMovement : MonoBehaviour {
 
     public Transform  lookAt;
 
     private Vector3 offset;
 
     private float distance = 5.0f;
     private float yOffset = 3.5f;
 
     private Vector2 touchPosition;
     private float swipeResistance = 200.0f;
 
     private void Start(){
         offset = new Vector3 (0, yOffset, -1f * distance);
     }
 
     private void Update(){
 
 
         if (Input.GetMouseButtonDown (0) || Input.GetMouseButtonDown (1)) 
         {
             touchPosition = Input.mousePosition;
         }
 
         if (Input.GetMouseButtonUp (0) || Input.GetMouseButtonUp (1)) 
         {
             float swipeforce = touchPosition.x - Input.mousePosition.x;
 
             if (Mathf.Abs (swipeforce) > swipeResistance) 
             {
                 if (swipeforce < 0) 
                 {
                     slideCamera (true);
                 } 
                 else 
                 {
                     slideCamera (false);
                 }            
             }
         }
 
         transform.position = lookAt.position + offset;
         transform.LookAt (lookAt);
     }
 
     public void slideCamera(bool left){
         if (left)
             offset = Quaternion.Euler (0, 90, 0) * offset;
         else {
             offset = Quaternion.Euler (0, -90, 0) * offset;
         }
     }
 }
 

Ball Movement Code:

     // Update is called once per frame
     void Update () {
 
         y = Input.acceleration.y;
 
         if (y > 0.1f) 
         {
             
             controller.AddForce (Vector3.back * Speed * Time.deltaTime);
         } 
         else if (y < -0.1f)         
         {
             
             controller.AddForce (Vector3.forward * Speed * Time.deltaTime);
         } 
         else 
         {
             controller.velocity = Vector3.zero;
         }
             
     }
 }

testball.jpg (39.5 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Molmez · Feb 23, 2017 at 10:19 PM

I tried to use this but ball randomly goes nuts in spinning.

 transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, transform.localEulerAngles.z);


What I am thinking is addforce is world space. even if i addrelativeforce the ball itself rotates when moving so that doesnt work.

Either I need to somehow rotate the level on swipe instead of the camera or I need to use transform forward and rotate the ball inside an animation. Any opinions on this before I end up in therapy?

thanks.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

126 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

Related Questions

how do i reference two input axis at one time on javascript 0 Answers

How do I rotate my object back to the world's up direction whilst keeping it's forward direction? 0 Answers

How can I make my GameObject rotate this way? 0 Answers

How to rotate fluently/smoothly? 1 Answer

How set rotation of a single axis 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