- Home /
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, this 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 this is my last resort posting here...
PS: this 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 hit : 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, hit, 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=hit.transform; }
}
// RIGHT:
if (Physics.Raycast (transform.position- Vector3.up * 0.5
Vector3.right * 0.5, -Vector3.up, hit, 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=hit.transform; } }
// do jumping, if we're on top of a platform
if(jumpEnabled){
if(grounded && rigidbody.velocity.y<0){
doJump();
// send a message to the platform to tell it to destroy itself
theTransformHit.gameObject.SendMessage("hitPlatform");
}
}
// 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>1 || mousePos<-1){
xVel+=(mousePos*0.02);
}
if(canMove){
// add x velocity to our position
//xPos=transform.position.x;
if(xVel>maxMoveSpeed)
xVel=maxMoveSpeed;
if(xVel<-maxMoveSpeed)
xVel=-maxMoveSpeed;
if(xPos>gameWidth){
xPos=gameWidth;
rigidbody.MovePosition(Vector3(gameWidth,transform.position.y,transform.position.z));
if(xVel>0){
xVel=0;
rigidbody.velocity.x=0;
}
}
if(xPos<-gameWidth){
xPos=-gameWidth;
rigidbody.MovePosition(Vector3(-gameWidth,transform.position.y,transform.position.z));
if(xVel<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);
}
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 something 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.
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