• 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 UnityFun · Dec 31, 2012 at 07:58 AM · playeriphonecontrollermodify

How to Modify a Multiplayer Controller Script for an iPhone

I hope this one is easy

I have (2) scripts, one from SmartFox called Player Controller (Script #1) and the second one from the Unity Asset Store called EasyMobileJoystick (Script #2)

Script #1 is from SmartFox and is provided as a Unity example called the Object Movement and is part of a multiplayer example. However, instead of using the standard keyboard keys to move the player, I would like to modify the Player Controller Script (Script #1) to enable moving the player using an iPhone script for movement (Script #2)

The Key: Script #1 is provided as part of the Object Movement example by SmartFox and is applied to a multiplayer format, I would like to modify Script #1 so instead of using keyboard keys you can use standard joystick thumb presses on an iPhone (Script #2) to move the player

Thank you in advance :)

SCRIPT #1 (Player Controller using the Keyboard)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     public float forwardSpeed = 10;
     public float backwardSpeed = 8;
     public float rotationSpeed = 40;
     
     // Dirty flag for checking if movement was made or not
     public bool MovementDirty {get; set;}
 
     void Start() {
         MovementDirty = false;
     }
     
     void Update () {
         // Forward/backward makes player model move
         float translation = Input.GetAxis("Vertical");
         if (translation != 0) {
             this.transform.Translate(0, 0, translation * Time.deltaTime * forwardSpeed);
             MovementDirty = true;
         }
     
         // Left/right makes player model rotate around own axis
         float rotation = Input.GetAxis("Horizontal");
         if (rotation != 0) {
             this.transform.Rotate(Vector3.up, rotation * Time.deltaTime * rotationSpeed);
             MovementDirty = true;
         }
     }
 }

SCRIPT #2 (Player Controller using the iPhone)

 #pragma strict
 
     // amount of time the user has to place finger on screen before joystick appears
     var fingerTimeBeforeJoystickAppears : float = 0.1f;
     
     // If after scaling the image you want to ensure the thumbstick
     // rotates in a tight circle you can click this option
     var forceCircleConstraint : boolean;
     
     // joystick image must contain GUITexture
     var imageJoystick : GameObject;
     
     // bg for joystick must contain GUITexture
     var imageBG : GameObject;    
     
     // public property that any script can get the position of the joystick and use it to move an object
     var  position : Vector2;
     
     // which side of screen you want the stick to appear
     // left stick uses left hand side only
     // for right stick mark as false and it uses the right hand side of screen
     var  isLeftStick : boolean;
     
     // How much the position is multiplied by
     // If using Unity Controller scripts it can be
     // used to change the speed of the player
     var positionMultiplier : float = 5;
     
     // makes the joystick act as a button
     var  isButton : boolean = false;
     
     var  tapCount : int;
     
     // private vars no need to worry
     private var  isInMotionStartingArea : Rect;
     private var  rotFTime : float;
     private var  motionFTime : float;    
     private var  isMotionPress : boolean = false;
     private var  movementStickShowing : boolean = false;
     private var  maxAmount : float  = 0.1f;
     private var motionFingerNum : int = -1;
     private var lastFingerId = -1;    
     
     
     private var imageJoystickScale : float;
     
     
     
     
     function Awake(){
             SetUpImagesIfMissing();
     
     }
     
     function Start ()
     {
     
         imageJoystickScale =  imageJoystick.transform.localScale.x /  imageJoystick.transform.localScale.y;
         // hide sticks intially
         ShowHideMotion(false);
         
         
             
             
     
     }
     function Update ()
     {
         //// create bounds
         if(isLeftStick)
             isInMotionStartingArea = new Rect (50, 50, Screen.width/2 - 100, Screen.height - 100);
         else
             isInMotionStartingArea    = new Rect (Screen.width / 2 + 50, 50, Screen.width / 2 - 100, Screen.height - 100);
         // Touches 
         if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) {
             if (Input.touchCount == 0) {
                 ShowHideMotion(false);
             }
                     
             for(var touch : Touch in Input.touches)
             {
                 if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended) {
                     if (motionFingerNum == touch.fingerId) {
                         motionFingerNum = -1;
                         position = Vector2.zero;
                         ShowHideMotion(false);
                 
                     }
                     
                 }
                 if (Time.time > motionFTime + fingerTimeBeforeJoystickAppears && motionFingerNum == touch.fingerId ) {
                     if (movementStickShowing)
                         UpdateMotion(touch.position);
                     else
                         ShowHideMotion(true);
                 
                 }
                 
                 if (touch.phase == TouchPhase.Began) {
                     if ( motionFingerNum == -1 && isInMotionStartingArea.Contains (touch.position)) {
                         SetMotionPoint (touch.position);
                         motionFTime = Time.time;
                         motionFingerNum = touch.fingerId;
                     }
                     
                 }
             }
         }
         
         // if using editor then allow the mouse button to be the "touches"
         else {
             if (Input.GetMouseButtonDown(0)) {
                 if (isInMotionStartingArea.Contains (Input.mousePosition)) {
                     lastFingerId = 1;
 
                     isMotionPress = true;
                     SetMotionPoint(Input.mousePosition);
                     motionFTime = Time.time;
                 } else { isMotionPress = false; }
                 
             }
             if (Input.GetMouseButton(0) ) {
                 if (isMotionPress && Time.time > motionFTime + fingerTimeBeforeJoystickAppears) {
                     if (movementStickShowing)
                         UpdateMotion (Input.mousePosition);
                     else
                         ShowHideMotion(true);
                 }
                 
             }
             if (Input.GetMouseButtonUp(0)) {
                 lastFingerId = -1;
                 position = Vector2.zero;                
                 ShowHideMotion(false);
             }
         }
     }
     
     
     // you can omit images if you want to have an invisible controller
     function SetUpImagesIfMissing(){
         if(imageJoystick == null)
             {
                 imageJoystick = new GameObject("Joy");
                 imageJoystick.AddComponent ("GUITexture");
             }
             if(imageBG == null)
             {
                 imageBG = new GameObject("BG");
                 imageBG.AddComponent ("GUITexture");
             }
     
     }
     
     function ClampStick (p : Vector3, downPosition :Vector3 )
     {
         
         if (Vector3.Distance (p, downPosition) > maxAmount) {
             var dir : Vector3;
             dir = downPosition - p ;
             
             dir.Normalize ();
             return (dir * maxAmount) + p;    
         }
         return downPosition;
     }
     
     function ClampStickScaleJoyStick (p : Vector3, downPosition :Vector3 )
     {
         
         if (Vector3.Distance (p, downPosition) > maxAmount) {
             var dir : Vector3;
             dir = downPosition - p ;
             
             dir.Normalize ();
             dir.x = dir.x * imageJoystickScale;
             return (dir * maxAmount) + p;    
         }
         return downPosition;
     }    
     
     
     
     
     
     function UpdateMotion( newPos : Vector3) {
         if(!isButton)
         {
             imageJoystick.transform.position = new Vector3 (newPos.x / Screen.width , newPos.y / Screen.height, 0);
             if(!forceCircleConstraint)
                 imageJoystick.transform.position = ClampStick(imageBG.transform.position, imageJoystick.transform.position);
             else
             {
                 imageJoystick.transform.position = ClampStickScaleJoyStick(imageBG.transform.position, imageJoystick.transform.position);
                 position.x = position.x /imageJoystickScale;
             }
 
             position = (imageJoystick.transform.position - imageBG.transform.position) * positionMultiplier;
             
         }
     }
     
     
     
      
     
     function SetMotionPoint( newPos :Vector3) {
         imageBG.transform.position = imageJoystick.transform.position = new Vector3 (newPos.x / Screen.width, newPos.y / Screen.height, 0);
         position = Vector2.zero;
     }
     
     function ShowHideMotion(show : boolean) {
         if(show)
         {
             imageJoystick.guiTexture.active = true;
             imageBG.guiTexture.active = true;
             movementStickShowing = true;
         }
         else
         {
             imageJoystick.guiTexture.active = false;
             imageBG.guiTexture.active = false;
             movementStickShowing = false;
         
         }
         
     }
     
     function Disable()
     {
         gameObject.active = false;    
     }
     
     function IsFingerDown() : boolean
     {
         return (lastFingerId != -1);
     }
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

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Where is the FPSPlayer Script? 0 Answers

Physics controls with look ahead. 0 Answers

How to Modify Multiplayer Script for iPhone 0 Answers

How to script a Save/Load option on IPhone? 1 Answer

I need the right joystick control to control the character's rotation 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