JavaScript on iPhone is definitely busted

Hi all. I converted the ragdoll c# code to JavaScript and ran into a weird problem:

    function FixedUpdate()
    {
    	var pos : Vector3 = Input.mousePosition;
        pos.z = Mathf.Abs( Camera.main.transform.position.z );
        pos = Camera.main.ScreenToWorldPoint( pos ); 
    	
    	if(isDragging)
    		transform.position.x = pos.x;// position.x = pos.x;

    	Scoring.Info = " " + pos + " --> " + transform.position;    		
    }

In the editor this behaves as expected, but when I run this code on my phone, transform.position has new values for both the X and Y values. How is this possible? The camera is placed very close to the ground. As such, movement is restricted between -4.5 and 4.5 on the x-axis. When I slide my finger, the X and Y go through the chart into the -2000 to +5000 range…

How is it possible that moving 3 on the x plane and using:

transform.position.x = pos.x;

or

var t : Vector3 = transform.position;
t.x = pos.x;
transform.position = t;

to modify the Y-axis by up to 5000???

I’ve never had any problem getting pixel positions of touches in javascript, so there’s something else at work here. It might be because you’re using Input.mousePosition instead of the iPhone touch API - I normally just use iPhoneInput.GetTouch and it works flawlessly. I’ve also heard of people getting bogus values for touch positions if they don’t properly set the screen dimensions in Unity to an iPhone format (i.e. iPhone Wide, etc.) but that could be a different issue.

I was thinking it might be that also, so I moved away from Input towards Touches, but then I saw Unity’s own example using Input so I figured “why the hell not?”

I am now thinking it is because of the ScreenToWorldPoint as opposed to ScreenPointToRay. The reason I say this is because I use the second example I provided above, the one where I replace the entire vector and what happens here is that the entire model now starts to float or drop through the ground and what have you and what not.

At the end of the day whatever method I use to impliment my code it still doesn’t explain why:

transform.position.x = pos.x;

gives me a transform.position.x of 1200 and a pos.x of -1.5

update…

    function FixedUpdate()
    {
    	var pos : Vector3 = Input.mousePosition;
        pos.z = Mathf.Abs( Camera.main.transform.position.z );
    	
		var ray = Camera.main.ScreenPointToRay (pos);
		var hit : RaycastHit;
		if (Physics.Raycast (ray, hit, 100))
		{
		    Debug.DrawLine (ray.origin, hit.point);
			transform.position.x = hit.point.x;
		}    	
        	Scoring.Info = " " + hit.point + " --> " + transform.position;    		
    }

x now = x… but y is totally f*cked…

This is an easy one; some other script is modifying your Transform’s position.

I’d just like to say that Javascript on iPhone is definitely not busted. Can’t blame the tools, sorry. :wink:

–Eric

Of course I can blame the tools… just see above :stuck_out_tongue: Lol

Seriously, though this is the simplest project I have worked on ever in any language. Heck I coded most of it at work out of boredom… in notepad, with no unity in sight…

  1. Antagonist.js deals only with moving the moving the enemy player around, pausing the enemy and instantiating the bullets.
    2.Constants.js has nothing but enums and global variables
  2. MainMenu.js is in a completely different scene altogether
  3. projectile.js deals only with it’s own transform and sends a Die message upon impact.
  4. Protagonist.js deals with moving the player character
  5. Scoring.js deals with the display of scores :stuck_out_tongue:

Allow me:
Scoring.js

var theFont 		: Font;
var scorePosition	: Rect;
static var Info		: String;
static var currentScore : int;

private var theStyle	: GUIStyle;

static function AdjustScore(by : float)
{
	currentScore += by;
}

function Start()
{
	currentScore = 0;
	theStyle = new GUIStyle();
	theStyle.font = theFont;
	theStyle.normal.textColor = Color(1,0,0,1);
}

function OnGUI()
{
	GUI.Label(scorePosition, currentScore + Info, theStyle);
}

Protagonist:

var 		maxLeft		: float = -5.0;
var 		maxRight	: float =  5.0;

var 		moveSpeed	: float = 5;

var		Avatar		: Transform;
var		Positions	: Vector3[];

private var	Avatars		: Transform[];
private var	Lives		: int;
private var theEnemy 		: GameObject; 
private var enemyCode 	: antagonist; 

function Start()
{
	Lives = Positions.length;
	Avatars = new Transform[Lives];
	for (x = 0; x < Avatars.length; x++)
	{
		var obj : Transform = Instantiate(Avatar, Positions[x], Quaternion.identity);
		obj.parent = transform;
		Avatars[x] = obj;
	}
	
	theEnemy = GameObject.Find("Enemy");
	enemyCode = theEnemy.GetComponent(antagonist);
}

function Update()
{
	if (Lives == 0)
		return;
    
    if (Input.GetButtonDown("Horizontal"))
	{
		var distance : float = moveSpeed * Time.deltaTime;
		if (Input.GetAxis ("Horizontal") < 0)
		{
			transform.position.x -= distance;
		}	else
		if (Input.GetAxis ("Horizontal") > 0)
		{
			transform.position.x += distance;
		}
		if (transform.position.x < maxLeft)
			transform.position.x = maxLeft;
		if (transform.position.x > maxRight)
			transform.position.x = maxRight;
	}

	for (x=0; x< Avatars.length; x++)
	{
		var Av	: Transform = Avatars[x];
		var obj : Animation = Av.FindObjectOfType(Animation);
		
//		if (touchPositionDelta.x < 0)
//			 obj.animation.Play("MoveLeft",PlayMode.StopAll);
//		else obj.animation.Play("MoveRight", PlayMode.StopAll);
	}
}

function Die()
{
	if (enemyCode.GetMoveState() == eMoveState.GameOver)
		return;
		
	if (Lives > 0)
	{
		Lives--;
		Destroy(Avatars[Lives].gameObject);
	} else
	{
		enemyCode.ChangeState(eMoveState.GameOver);
	}
}

And finally dragcontroller.js:

var theProtagonist  : protagonist;

var playerHitRange : float = 4.0;
var isDragging : boolean;
	
function Start()
{
	theProtagonist = GetComponent(protagonist);
}
	
	function OnMouseDown()
    {
        isDragging = true;
    }
    
    function OnMouseUp()
    {
        if ( isDragging )
        {
            isDragging = false;
        }
    } 
	
	function ContainedWithin( a: Vector3, b: Vector3, epsilon : float ) : boolean
	{
		return ( a.x >= b.x - epsilon  a.x <= b.x + epsilon
			 a.y >= b.y - epsilon  a.y <= b.y + epsilon );
	}
	
    // Update is called once per frame
    function Update () 
    {
    	if( Input.GetMouseButtonDown(0))
    	{
  
            var mousePos : Vector3 = Input.mousePosition;
            mousePos.z = -Camera.main.transform.position.z;
            mousePos = Camera.main.ScreenToWorldPoint( mousePos );
           
            if ( ContainedWithin( mousePos, transform.position, playerHitRange ) )
            {
         		OnMouseDown();
            }

        }
        else if(Input.GetMouseButtonUp(0))
        {
        	OnMouseUp();
        }
    }
    
    function FixedUpdate()
    {
    	var pos : Vector3 = Input.mousePosition;
        pos.z = Mathf.Abs( Camera.main.transform.position.z );
    	
		var ray = Camera.main.ScreenPointToRay (pos);
		var hit : RaycastHit;
		if (Physics.Raycast (ray, hit, 100))
		{
		    Debug.DrawLine (ray.origin, hit.point);
		    var p : Vector3 = theProtagonist.Positions[0];
		    var v : Vector3 = Vector3(hit.point.x, 0.5, -4.26);
			transform.position = v;
		}    	
    	
    	Scoring.Info = " " + hit.point + " --> " + transform.position;    		
    }

The only thing moving my character is the mouse move and the Input.GetAxis(“Horizontal”). Don’t tell me theAccelerator works with the Input.GetAxis command ???

I can’t see what else could me moving my player all over the place. Have a look at dragcontroller… there is no time for anything else to move the prefab between my assigning the value and moving it to the GUI for display…

This is why I am so confused and asking for help. I cannot seem to find the problem with the above code… Please oh please tell me you see something clear and plain and obvious that I am overlooking!!!

Holy crapmonkeys…

I changed Update to Updates to avoid the GetAxis and voila it now works like a dream… who knew???

But why does GetButtonDown(“Horizontal”) return true when I touch the screen?

Weird…

Guess I can’t code standalone and iPhone code in the same script after all… Just HAVE to test it with the remote instead of the keyboard :stuck_out_tongue:

I guess because horizontal is a mouse input to which touch 0 is mapped.