- Home /
How to Modify Multiplayer Script for 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);
}
Your answer
Follow this Question
Related Questions
How to Modify a Multiplayer Controller Script for an iPhone 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Help with fps script 2 Answers
Enemy controller.move script slows down and speeds up. How to get it consistent? 0 Answers
Can I change movement from Unity 3d into Unity 3D iOS 2 Answers