I have successfully gotten the character to move with the left stick and rotate…slowly with the right…the problem i have is the movement doesn’t follow the direction the player character is facing it just goes up the screen regardless of the way your facing… also I keep getting a null reference exception that i can’t seem to pin point… any ideas? oh this is a hybrid of penelope player relative and evac city (characters are 2d spritesheets I haven’t added the animation part yet.)
@script RequireComponent ( CharacterController )
//Joystik control variables
var moveJoystick : Joystick;
var rotateJoystick : Joystick;
private var thisTransform : Transform;
private var velocity : Vector3;
private var character : CharacterController;
var forwardSpeed : float = 4;
var backwardSpeed : float = 1;
var sidestepSpeed : float = 1;
var moveSpeed : float = 100;
var Grounded : boolean = true;
//calculation variables
var tempVector : Vector3;
var tempVector2 : Vector3;
//rotation variables
var rotationSpeed : Vector2 = Vector2( 50, 25 );
var Pivot : Transform;
//Game Objects
var objPlayer : GameObject;
private var objCamera : GameObject;
function Start()
{
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
objPlayer = GameObject.FindWithTag ( "Player" );
}
function Update()
{
var movement = thisTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
tempVector = rigidbody.GetPointVelocity(transform.position) * Time.deltaTime * 1000;
rigidbody.AddForce (-tempVector.x, -tempVector.y, -tempVector.z);
rigidbody.AddForce (movement.normalized * moveSpeed * Time.deltaTime);
var cameraTarget = Vector3.zero;
//Apply movement from move joystick
var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
if ( absJoyPos.y > absJoyPos.x )
{
if ( moveJoystick.position.y > 0 )
movement *= forwardSpeed * absJoyPos.y;
else
{
movement *= backwardSpeed * absJoyPos.y;
cameraTarget.z = moveJoystick.position.y * 0.75;
}
}
else
{
movement *= sidestepSpeed * absJoyPos.x;
}
movement += velocity;
movement *= Time.deltaTime;
//Actually move the character
character.Move( movement );
if ( Grounded )
{
var Rotation = rotateJoystick.position;
Rotation.x *= rotationSpeed.x;
Rotation *= Time.deltaTime;
// Rotate the character around world-y using x-axis of joystick
thisTransform.Rotate( 0, Rotation.x, 0, Space.World );
}
}