Put Joystick into my script

Hi,

I’m running an edited third person controller script that comes with Unity, Ive got this character running around the screen, throwing things and jumping by using keyboard and arrow keys, what I’m now trying to do is add some script to it so I can use a joystick on IOS.

Below is the edited third person controller script I’m trying to add it to, I’ve setup the Controller with the joystick script attached and the GUI texture with my joystick graphic on, I put a Debug line in the third person controller script and it gave me the joystick vector so I know it’s working but just can’t figure how to integrate it into the script.

Below is some script I’ve tried with no luck.

Any help will be great fully appreciated.

Kevin.

#pragma strict

var ThrownObject : GameObject;							// Set var for a object to throw
var Explosion : Transform;								// Set var for the exposion to use when he throws
var movementJoystick : Joystick;						//Joystick that controls the player movement
var mouseSoundCntrl : MouseSoundController;				// Set link in the Inspector window to the MouseSoundController
														// on empty game object called Sound Mouse Files
var swooshSoundCntrl : MouseSwooshSoundController;		// Set link in the Inspector window to the MouseSwooshSoundController
														// on empty game object called Sound Swoosh File
var popSoundCntrl : MousePopSoundController;			// Set link in the Inspector window to the MousePopSoundController
														// I've put it on empty game object called Sound Pop File
var mouseSound : GameObject;
var swooshSound : GameObject;

// Require a character controller to be attached to the same game object
@script RequireComponent(CharacterController)

public var idleAnimation : AnimationClip;
public var walkAnimation : AnimationClip;
public var runAnimation : AnimationClip;
public var jumpPoseAnimation : AnimationClip;
public var swingAnimation : AnimationClip;
public var throwAnimation : AnimationClip;
public var punchAnimation : AnimationClip;

public var pivotTransform : Transform; //Transform of the camera pivot
public var walkMaxAnimationSpeed : float = 0.75;
public var trotMaxAnimationSpeed : float = 1.0;
public var runMaxAnimationSpeed : float = 1.0;
public var jumpAnimationSpeed : float = 1.15;
public var landAnimationSpeed : float = 1.0;

private var _animation : Animation;
private var counter : float = 0.0;

enum CharacterState {
	Idle = 0,
	Walking = 1,
	Trotting = 2,
	Running = 3,
	Jumping = 4,
	Swing = 5,
	Throw = 6,
	Punch = 7,
}

private var _characterState : CharacterState;

// The speed when walking 
var walkSpeed = 2.0;
// after trotAfterSeconds of walking we trot with trotSpeed
var trotSpeed = 4.0;
// when pressing "Fire3" button (cmd) we start running
var runSpeed = 6.0;
// The gravity for the character
var gravity = 20.0;
// The gravity in controlled descent mode
var speedSmoothing = 10.0;
var rotateSpeed = 500.0;
var trotAfterSeconds = 2.0;

// The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around.
private var lockCameraTimer = 0.0;

// The current move direction in x-z
private var moveDirection = Vector3.zero;
// The current vertical speed
private var verticalSpeed = 0.0;
// The current x-z move speed
private var moveSpeed = 0.0;

// The last collision flags returned from controller.Move
private var collisionFlags : CollisionFlags; 

// Are we moving backwards (This locks the camera to not do a 180 degree spin)
private var movingBack = false;
// Is the user pressing any keys?
private var isMoving = false;
// When did the user start walking (Used for going into trot after a while)
private var walkTimeStart = 0.0;

private var inAirVelocity = Vector3.zero;

private var isControllable = true;


function Awake ()
{
	moveDirection = transform.TransformDirection(Vector3.forward);
	
	_animation = GetComponent(Animation);
	if(!_animation)
		Debug.Log("The character you would like to control doesn't have animations. Moving him or her might look weird.");	
	/*
public var idleAnimation : AnimationClip;
public var walkAnimation : AnimationClip;
public var runAnimation : AnimationClip;
public var jumpPoseAnimation : AnimationClip;
public var swingAnimation : AnimationClip;
public var throwAnimation : AnimationClip;
public var punchAnimation : AnimationClip;	
	*/
	if(!idleAnimation) {
		_animation = null;
		Debug.Log("No idle animation found. Turning off animations.");
	}
	if(!walkAnimation) {
		_animation = null;
		Debug.Log("No walk animation found. Turning off animations.");
	}
	if(!runAnimation) {
		_animation = null;
		Debug.Log("No run animation found. Turning off animations.");
	}
	if(!jumpPoseAnimation) {
		_animation = null;
		Debug.Log("No jump animation found and the character has canJump enabled. Turning off animations.");
	}
	if(!swingAnimation) {
		_animation = null;
		Debug.Log("No swing animation found. Turning off animations.");
	}
	if(!throwAnimation) {
		_animation = null;
		Debug.Log("No throw animation found. Turning off animations.");
	}
	if(!punchAnimation) {
		_animation = null;
		Debug.Log("No punch animation found. Turning off animations.");
	}
			
}

function UpdateSmoothedMovementDirection ()
{
	var cameraTransform = Camera.main.transform;
	var walking = IsWalking();
	
	// Forward vector relative to the camera along the x-z plane	
	var forward = cameraTransform.TransformDirection(Vector3.forward);
	forward.y = 0;
	forward = forward.normalized;

	// Right vector relative to the camera
	// Always orthogonal to the forward vector
	var right = Vector3(forward.z, 0, -forward.x);

	var v = Input.GetAxisRaw("Vertical");
	var h = Input.GetAxisRaw("Horizontal");

	// Are we moving backwards or looking backwards
	if (v < -0.2)
		movingBack = true;
	else
		movingBack = false;
	
	var wasMoving = isMoving;
	isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
		
	// Target direction relative to the camera
	var targetDirection = h * right + v * forward;

		// Lock camera for short period when transitioning moving & standing still
		// REM the below code out so the camera just watches the player it dont rotate around him
		lockCameraTimer += Time.deltaTime;

		// We store speed and direction seperately,
		// so that when the character stands still we still have a valid forward direction
		// moveDirection is always normalized, and we only update it if there is user input.
		if (targetDirection != Vector3.zero)
		{

// ------------

// Put below line back in if you wand to face the direction instantly

			//moveDirection = targetDirection.normalized;

// ------------
		moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
				
		moveDirection = moveDirection.normalized;
	}
		
		// Smooth the speed based on the current target direction
		var curSmooth = speedSmoothing * Time.deltaTime;
		
		// Choose target speed
		//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
		var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
	
		_characterState = CharacterState.Idle;


		if(Input.GetKey (KeyCode.T)){
			_characterState = CharacterState.Throw;
			}
			
		else if(Input.GetKey (KeyCode.Y)){
			_characterState = CharacterState.Swing;
			}
			
		else if(Input.GetKey (KeyCode.P)){
			_characterState = CharacterState.Punch;
			}
						
		else if(Input.GetKeyDown(KeyCode.Space)){
			_characterState = CharacterState.Jumping;
			}
			

// ------------


		// Pick speed modifier

		else if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
		{
			targetSpeed *= runSpeed;
			_characterState = CharacterState.Running;
		}
		else if (Time.time - trotAfterSeconds > walkTimeStart)
		{
			targetSpeed *= trotSpeed;
			_characterState = CharacterState.Trotting;
		}
		else
		{
			targetSpeed *= walkSpeed;
			_characterState = CharacterState.Walking;
		}
		
		moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
		
		// Reset walk time start when we slow down
		if (moveSpeed < walkSpeed * 0.3)
			walkTimeStart = Time.time;
	}

function ApplyGravity ()
{
	if (isControllable){	// don't move player at all if not controllable.

		if (IsWalking ())
			verticalSpeed = 0.0;
		else
			verticalSpeed -= gravity * Time.deltaTime;
	}
}

function Update() {
	
	if (!isControllable)
	{
		// kill all inputs if not controllable.
		Input.ResetInputAxes();
	}	
	else if(Input.GetKeyDown(KeyCode.R))
	{
		ReLoad();
	}
	
	UpdateSmoothedMovementDirection();

	ApplyGravity ();
	
	// Calculate actual motion
	var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
	movement *= Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	collisionFlags = controller.Move(movement);
	
	// ANIMATION sector
	if(_animation) {

// Check to see if Throw animation is playing if so dont play again to stop throwwing lots of balls
		if((_characterState == CharacterState.Throw) && (animation["Throw"].enabled == false)){
		
		counter = 0;

// Create and set up the Throw AnimationEvent

	var throwObjectEvent = new AnimationEvent();
		throwObjectEvent.functionName = "throwObject";
			throwObjectEvent.time = 1;
		
		_animation[throwAnimation.name].layer = 1; // Place the Throw animation on a higher level
		_animation[throwAnimation.name].clip.AddEvent(throwObjectEvent); // Add the event to an AnimationClip
		_animation[throwAnimation.name].blendMode = AnimationBlendMode.Additive;	// set the blend mode to Additive so it will blend
    																				// into whatever animation is playing when it's called
    	_animation[throwAnimation.name].wrapMode = WrapMode.Once;
		_animation.CrossFade(throwAnimation.name);	// Play the animation
		
		}

// Create and set up the Swing AnimationEvent

	if(_characterState == CharacterState.Swing) {
		
	var myVar: System.Action;
	var swooshSoundEvent = new AnimationEvent(); //swooshSoundEvent.messageOptions = SendMessageOptions.DontRequireReceiver;
    // get the function reference (no parenthesis!)
    myVar = swooshSound.GetComponent(MouseSwooshSoundController).SwingSound;
    swooshSoundEvent.functionName = "RelayMessage";
	swooshSoundEvent.stringParameter = myVar.Method.Name;
    swooshSoundEvent.time = 0.6;
	
	_animation[swingAnimation.name].layer = 1; // Place the Swing animation on a higher level
	_animation[swingAnimation.name].clip.AddEvent(swooshSoundEvent); // Add the event to an AnimationClip
	_animation[swingAnimation.name].blendMode = AnimationBlendMode.Additive;	// set the blend mode to Additive so it will blend
																				// into whatever animation is playing when it's called
    _animation[swingAnimation.name].wrapMode = WrapMode.Once;
    animation.Rewind(swingAnimation.name);
	animation.CrossFade(swingAnimation.name);	// Play the animation
	}

	if(_characterState == CharacterState.Punch) {
	
	_animation[punchAnimation.name].layer = 1; // Place the Punch animation on a higher level
	_animation[punchAnimation.name].blendMode = AnimationBlendMode.Additive;	// set the blend mode to Additive so it will blend
																				// into whatever animation is playing when it's called
    _animation[punchAnimation.name].wrapMode = WrapMode.Once;    
	animation.CrossFade(punchAnimation.name);	// Play the animation
	}

// Create and set up the Jump Animation

	if(_characterState == CharacterState.Jumping) {
	
	_animation[jumpPoseAnimation.name].layer = 1; // Place the Swing animation on a higher level
	_animation[jumpPoseAnimation.name].blendMode = AnimationBlendMode.Additive;	// set the blend mode to Additive so it will blend
																				// into whatever animation is playing when it's called
    _animation[jumpPoseAnimation.name].wrapMode = WrapMode.Once;
	animation.CrossFade(jumpPoseAnimation.name);	// Play the animation

	} 

// Create and set up the Walk and Run Animations

	if(controller.velocity.sqrMagnitude < 0.1) {
	_animation.CrossFade(idleAnimation.name);
	}
		else 
		{
		if(_characterState == CharacterState.Running) {
			_animation[runAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, runMaxAnimationSpeed);
			_animation.CrossFade(runAnimation.name);
			mouseSoundCntrl.MouseRunSound();	
		}
		else if(_characterState == CharacterState.Trotting) {
			_animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, trotMaxAnimationSpeed);
			_animation.CrossFade(walkAnimation.name);
			mouseSoundCntrl.MouseTrotSound();	
		}
		else if(_characterState == CharacterState.Walking) {
			_animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, walkMaxAnimationSpeed);
			_animation.CrossFade(walkAnimation.name);
			mouseSoundCntrl.MouseWalkSound();
		}				
	}
}

	transform.rotation = Quaternion.LookRotation(moveDirection);

}

function OnControllerColliderHit (hit : ControllerColliderHit )
{
//	Debug.DrawRay(hit.point, hit.normal);
	if (hit.moveDirection.y > 0.01) 
		return;
}

function GetSpeed () {
	return moveSpeed;
}

function IsWalking () {
	return (collisionFlags & CollisionFlags.CollidedBelow) != 0;
}

function GetDirection () {
	return moveDirection;
}

function IsMovingBackwards () {
	return movingBack;
}

function GetLockCameraTimer () {
	return lockCameraTimer;
}

function IsMoving ()  : boolean
{
	return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5;
}


// ------------


function throwObject () {

// Make sure you name the spawn point in the hierarchy window to match the GameObject.Find() below

if(counter < 1){
	var bullit : GameObject = Instantiate(ThrownObject, GameObject.Find("throwSpawnPoint").transform.position, transform.rotation) as GameObject;
		bullit.rigidbody.AddForce(transform.forward * 500);
		// Use the next line only if you want an expolsion/smoke when he throws
			Instantiate(Explosion, GameObject.Find("throwSpawnPoint").transform.position, transform.rotation);
				popSoundCntrl.MousePopSound();
					counter = counter + 1;
	}
}

function Reset () {
	gameObject.tag = "Player";
}

function ReLoad () {
	Application.LoadLevel("Level 01");
}

Joystick script.

var movement : Vector3;
	
	if(movementJoystick) { //If movement joystick exists
	
		movement = movementJoystick.GetAxis(); //Get movement joystick axis
    
		// This is where I get lost
    
		Debug.Log(movement); //Debug and return Joystick values
		if(movement.sqrMagnitude > 0){
			movement.Normalize();
			mouseSoundCntrl.MouseWalkSound(); // Add the footsteps if there is Joystick movement
			}
	}

Glad I could help someone out …

winresh, this is a character controller so you put the last script into the first script at function UpdateSmoothedMovementDirection () then attach it to the player you want to control.

I have been looking everywhere for a way to slowly rotate toward a joystick Vector, rather than instantly snapping, thank you endlessly!