• 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
Question by Gary 2 · Jan 05, 2011 at 10:28 AM · androidiphoneconvertaccelerometer

how to convert Input.mousePosition into iphone/android accelerometer?

URGENTLY IN NEED OF HELP!!!!

Hi all,

Recently my boss bought a starterkit by activeden called Active Jump!

I have been trying to get it port into iphone/android with no success...

well here is the code, t$$anonymous$$s is attached to the player prefab.

Basically i need to tilt the phone left to right like those jumping game clones on iphone & android.. aka like Abduction, doodle jump, papi jump etc etc...

i tried so far but no avail, keep getting errors like float cannot convert into vector3 stuff like that.. usually t$$anonymous$$s is my last resort posting here...

PS: t$$anonymous$$s is a part of the full code.. i hope im not voiding any stupid license from Activeden...

private var theZ : float; private var jumpEnabled : boolean; private var canMove : boolean; private var $$anonymous$$t : RaycastHit; private var mousePos : float; private var xVel : float; private var xPos : float; private var yVel : float;

 function FixedUpdate () {

     // always assume we're NOT grounded every step (and expect to be proved wrong by raycasting etc.)
     grounded=false;

     // check to see if we're on top of a platform - here we cast two rays, one on each side of the player
     // LEFT:
     if (Physics.Raycast (transform.position- Vector3.up * 0.5
  • Vector3.right * 0.5, -Vector3.up, $$anonymous$$t, 1, 1<<9)) { // we found ground, so set our grounded flag to true, so that the player will jump grounded=true; if(rigidbody.velocity.y<0){ theTransformHit=$$anonymous$$t.transform; }

        }
         // RIGHT:
         if (Physics.Raycast (transform.position- Vector3.up * 0.5
    
  • Vector3.right * 0.5, -Vector3.up, $$anonymous$$t, 1, 1<<9)) { // we found ground, so set our grounded flag to true, so that the player will jump grounded=true; if(rigidbody.velocity.y<0){ theTransformHit=$$anonymous$$t.transform; } }

        // do jumping, if we're on top of a platform
         if(jumpEnabled){
             if(grounded &amp;&amp; rigidbody.velocity.y&lt;0){
                 doJump();
                 // send a message to the platform to tell it to destroy itself
                 theTransformHit.gameObject.SendMessage("$$anonymous$$tPlatform");
             }
         }
    
         // grab the mouse position and take off the width of the screen /2 so that moving the
         // mouse to the left of the window will produce a negative number and moving it to the right of
         // the window will produce a positive number
    
         mousePos = (Screen.width/2)-Input.mousePosition.x;
    
    
             // now we check the mouse position and move our player accordingly
             xVel=0;
             if (mousePos&gt;1 || mousePos&lt;-1){
                 xVel+=(mousePos*0.02);
             }
    
             if(canMove){
    
                 // add x velocity to our position
                 //xPos=transform.position.x;
    
                 if(xVel&gt;maxMoveSpeed)
                 xVel=maxMoveSpeed;
    
                 if(xVel&lt;-maxMoveSpeed)
                 xVel=-maxMoveSpeed;
    
                 if(xPos&gt;gameWidth){
                     xPos=gameWidth;
                     rigidbody.MovePosition(Vector3(gameWidth,transform.position.y,transform.position.z));
                     if(xVel&gt;0){
                         xVel=0;
                         rigidbody.velocity.x=0;
                     }
                 }
    
                 if(xPos&lt;-gameWidth){
                     xPos=-gameWidth;
                     rigidbody.MovePosition(Vector3(-gameWidth,transform.position.y,transform.position.z));
                     if(xVel&lt;0){
                         xVel=0;
                         rigidbody.velocity.x=0;
                     }
                 }
    
                 // set our movement velocity
                 rigidbody.velocity.x=xVel*5;
    
                 // lean our player some
                 transform.eulerAngles.z=-(mousePos*0.2);
    
             }
    

Comment

People who like this

0 Show 0
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

Answer by Landern · Jan 05, 2011 at 10:13 PM

First, don't post licensed code here, thats not cool for the developer that made it.

Second, you should probably get to know what you're doing first before porting somet$$anonymous$$ng or at least the basics. If you're trying to automagically convert a float into a vector3 then you're at the losing end. A vector3 and it's overrides can take floats as parameters but a converter doesn't come for free. I mean if you pass in a single float into a vector3 type what vector would it represent? X, Y, Z?

Perhaps redo your question with the error and the line and it can be explained instead of just done for you and your boss.

Comment

People who like this

0 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 Gary 2 · Jan 06, 2011 at 02:30 AM 0
Share

i do know that it's not cool to do so but im already @ the losing end..

& the problem mainly lies in the fixedupdate portion, & i couldnt find any tuts on the jumping game thingy, that's also why this is my last resort posting here

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

No one has followed this question yet.

Related Questions

Accelerometer question 1 Answer

Sensor Fusion - Angular Velocity 0 Answers

Match the tilted angle of phone to object. 0 Answers

Issues with accelerometer calibration? 1 Answer

Mapping accelerometer to Camera's position 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