plays animation

Guys i am new to unity Iphone and been trying to make my character to play the walk animation when i push the joystick up or down can somebody can show me a code sample i try the Sophie tutorial but it not work all

thanks

var myAnimation : Animation;
private var walking : boolean = true;

function Start() {
 myAnimation = GetComponentInChildren(Animation);
 myAnimation["walk"].wrapMode = WrapMode.Loop;
}

function Update() {
 if ( walking ) myAnimation.CrossFade( "walk", 0.5 );
}

function Walk() {
 walking = true;
}


/* input file pseudocode */

function Update() {
 if ( Input.GetAxis("Vertical") != 0 ) {
  mainplayer.Walk();
 }
}/**/

i try but i didn’t work guys do you have another sugestion

can you post your script so we can have a look, it’s hard to guess your setup…

I use the script of the player relative i will put tomorrow some screenshots but you guys have any suggestions please you can posted
thanks

“It didn’t work” doesn’t give us a whole lot to go on. What exactly is the problem?

this are the screenshots





when i use remote or play the character doesn’t move

sorry i forgot i am using
Joystick.js

this is the code

script RequireComponent( GUITexture )

// A simple class for bounding how far the GUITexture will move
class Boundary
{
var min : Vector2 = Vector2.zero;
var max : Vector2 = Vector2.zero;
}

static private var joysticks : Joystick[ ]; // A static collection of all joysticks
static private var enumeratedJoysticks : boolean = false;
static private var tapTimeDelta : float = 0.3; // Time allowed between taps

var touchPad : boolean; // Is this a TouchPad?
var touchZone : Rect;
var deadZone : Vector2 = Vector2.zero; // Control when position is output
var normalize : boolean = false; // Normalize output after the dead-zone?
var position : Vector2; // [-1, 1] in x,y
var tapCount : int; // Current tap count

private var lastFingerId = -1; // Finger last used for this joystick
private var tapTimeWindow : float; // How much time there is left for a tap to occur
private var fingerDownPos : Vector2;
private var fingerDownTime : float;
private var firstDeltaTime : float = 0.5;

private var gui : GUITexture; // Joystick graphic
private var defaultRect : Rect; // Default position / extents of the joystick graphic
private var guiBoundary : Boundary = Boundary(); // Boundary for joystick graphic
private var guiTouchOffset : Vector2; // Offset to apply to touch input
private var guiCenter : Vector2; // Center of joystick

function Start()
{
// Cache this component at startup instead of looking up every frame
gui = GetComponent( GUITexture );

// Store the default rect for the gui, so we can snap back to it
defaultRect = gui.pixelInset;

if ( touchPad )
{
// If a texture has been assigned, then use the rect ferom the gui as our touchZone
if ( gui.texture )
touchZone = gui.pixelInset;
}
else
{
// This is an offset for touch input to match with the top left
// corner of the GUI
guiTouchOffset.x = defaultRect.width * 0.5;
guiTouchOffset.y = defaultRect.height * 0.5;

// Cache the center of the GUI, since it doesn’t change
guiCenter.x = defaultRect.x + guiTouchOffset.x;
guiCenter.y = defaultRect.y + guiTouchOffset.y;

// Let’s build the GUI boundary, so we can clamp joystick movement
guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
}
}

function Disable()
{
gameObject.active = false;
enumeratedJoysticks = false;
}

function ResetJoystick()
{
// Release the finger control and set the joystick back to the default position
gui.pixelInset = defaultRect;
lastFingerId = -1;
position = Vector2.zero;
fingerDownPosition = Vector2.zero;

if ( touchPad )
gui.color.a = 0.025;
}

function IsFingerDown() : boolean
{
return (lastFingerId != -1);
}

function LatchedFinger( fingerId : int )
{
// If another joystick has latched this finger, then we must release it
if ( lastFingerId == fingerId )
ResetJoystick();
}

function Update()
{
if ( !enumeratedJoysticks )
{
// Collect all joysticks in the game, so we can relay finger latching messages
joysticks = FindObjectsOfType( Joystick );
enumeratedJoysticks = true;
}

var count = iPhoneInput.touchCount;

// Adjust the tap time window while it still available
if ( tapTimeWindow > 0 )
tapTimeWindow -= Time.deltaTime;
else
tapCount = 0;

if ( count == 0 )
ResetJoystick();
else
{
for(var i : int = 0;i < count; i++)
{
var touch : iPhoneTouch = iPhoneInput.GetTouch(i);
var guiTouchPos : Vector2 = touch.position - guiTouchOffset;

var shouldLatchFinger = false;
if ( touchPad )
{
if ( touchZone.Contains( touch.position ) )
shouldLatchFinger = true;
}
else if ( gui.HitTest( touch.position ) )
{
shouldLatchFinger = true;
}

// Latch the finger if this is a new touch
if ( shouldLatchFinger ( lastFingerId == -1 || lastFingerId != touch.fingerId ) )
{

if ( touchPad )
{
gui.color.a = 0.15;

lastFingerId = touch.fingerId;
fingerDownPos = touch.position;
fingerDownTime = Time.time;
}

lastFingerId = touch.fingerId;

// Accumulate taps if it is within the time window
if ( tapTimeWindow > 0 )
tapCount++;
else
{
tapCount = 1;
tapTimeWindow = tapTimeDelta;
}

// Tell other joysticks we’ve latched this finger
for ( var j : Joystick in joysticks )
{
if ( j != this )
j.LatchedFinger( touch.fingerId );
}
}

if ( lastFingerId == touch.fingerId )
{
// Override the tap count with what the iPhone SDK reports if it is greater
// This is a workaround, since the iPhone SDK does not currently track taps
// for multiple touches
if ( touch.tapCount > tapCount )
tapCount = touch.tapCount;

if ( touchPad )
{
// For a touchpad, let’s just set the position directly based on distance from initial touchdown
position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
}
else
{
// Change the location of the joystick graphic to match where the touch is
gui.pixelInset.x = Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x );
gui.pixelInset.y = Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );
}

if ( touch.phase == iPhoneTouchPhase.Ended || touch.phase == iPhoneTouchPhase.Canceled )
ResetJoystick();
}
}
}

if ( !touchPad )
{
// Get a value between -1 and 1 based on the joystick graphic location
position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;
position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;
}

// Adjust for dead zone
var absoluteX = Mathf.Abs( position.x );
var absoluteY = Mathf.Abs( position.y );

if ( absoluteX < deadZone.x )
{
// Report the joystick as being at the center if it is within the dead zone
position.x = 0;
}
else if ( normalize )
{
// Rescale the output after taking the dead zone into account
position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
}

if ( absoluteY < deadZone.y )
{
// Report the joystick as being at the center if it is within the dead zone
position.y = 0;
}
else if ( normalize )
{
// Rescale the output after taking the dead zone into account
position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );
}
}

and this is the animation controller

var walkSpeed = 2.3;
var runSpeed = 3.6;
var idleThreshold = 0.3;
var speedSmoothing = 8.0;

private var run : AnimationState;
private var walk : AnimationState;
private var leanLeft : AnimationState;
private var leanRight : AnimationState;

private var currentSpeed = 0.0;
private var smoothedSpeed = 0.0;
private var currentLean = 0.0;
private var smoothedLean = 0.0;

function SetCurrentSpeed ( newSpeed : float)
{
currentSpeed = newSpeed;
}

function SetCurrentLean ( newSpeed : float)
{
currentLean = newSpeed;
}

function Start ()
{
// We are in full control here - don’t let any other animations play when we start
animation.Stop();

// By default loop all animations
animation.wrapMode = WrapMode.Loop;

walk = animation[“walk”];
run = animation[“runSlow”];

leanLeft = animation[“leanLeft”];
leanRight = animation[“leanRight”];

// Do we actually have leaning animations? They are not necessary
if (leanLeft)
{
leanLeft.blendMode = AnimationBlendMode.Additive;
leanRight.blendMode = AnimationBlendMode.Additive;
leanLeft.wrapMode = WrapMode.ClampForever;
leanRight.wrapMode = WrapMode.ClampForever;
leanLeft.layer = 100;
leanRight.layer = 100;
leanLeft.weight = 1;
leanRight.weight = 1;
leanLeft.enabled = true;
leanRight.enabled = true;
}

// Enable all walk / run cycles
// We will manually adjust the blend weights every frame
walk.enabled = true;
run.enabled = true;

// Synchronize all walk / run cycle animations
animation.SyncLayer(0);

// Put the idle animation in a lower layer.
// This will make it only play if no walk / run cycle is faded in
animation[“idle”].layer = -1;
animation[“idle”].enabled = true;
animation[“idle”].weight = 1;
}

function Update () {
smoothedSpeed = Mathf.Lerp(smoothedSpeed, currentSpeed, Time.deltaTime * speedSmoothing);

// Calculate the weight between walk and run from the current speed
var runWeight = Mathf.InverseLerp(walkSpeed, runSpeed, Mathf.Abs(smoothedSpeed));

// Only fast running animations get the lean added on top. A walking human does not lean
var targetLean = currentLean * runWeight;
smoothedLean = Mathf.Lerp(smoothedLean, targetLean, Time.deltaTime * speedSmoothing);

// adjust the animation playback to match the characters speed
run.speed = smoothedSpeed / runSpeed;
walk.speed = smoothedSpeed / walkSpeed;

// When the character slows down fade out the run and walk weights
if (Mathf.Abs(smoothedSpeed) < idleThreshold)
{
run.weight -= Time.deltaTime * 5.0;
walk.weight -= Time.deltaTime * 5.0;
}
else
{
var totalWeight = run.weight + walk.weight;
totalWeight += Time.deltaTime * 12;
totalWeight = Mathf.Clamp01(totalWeight);

// Set the weights based on the current speed
run.weight = runWeight * totalWeight;
walk.weight = (1.0 - runWeight) * totalWeight;
}

if (leanLeft)
{
if (smoothedLean > 0)
{
leanLeft.normalizedTime = 0;
leanRight.normalizedTime = smoothedLean;
}
else
{
leanLeft.normalizedTime = -smoothedLean;
leanRight.normalizedTime = 0;
}
}
}

position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );

That right there is your vertical input, right? That’s where you’d add animation control.

Looks like you need to send current speed in to your animation control script.

thanks Vicenti he work pretty well i will put screenshoots next week