Ideally I want my character to always face the ‘blackhole’ variable which I would have set up as my target that the player character always faces,
but - if I do the above, the character always faces the object, but my joystick flight controls cause the player to move relative to the direction its facing instead of the direction of the world. I have a fixed camera andI just want the character to move along x and y in world space and either always face the center OR at the very least face the direction it is traveling - forward.
I am not sure how to change the direction of the character without hurting my control scheme, any tips would be appreciated.
Here is the code:
//var blackhole : Transform.target;
static var blackhole : Vector3 = Vector3.one;
var blasterPrefab : GameObject; // The blaster prefab
@script RequireComponent( CharacterController )
var moveJoystick : Joystick;
var rotateJoystick : Joystick;
var forwardSpeed : float = 4;
var backwardSpeed : float = 4;
var sidestepSpeed : float = 4;
private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3;
function Start()
{
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
// Move the character to the correct start position in the level, if one exists
var spawn = GameObject.Find( "PlayerSpawn" );
if ( spawn )
thisTransform.position = spawn.transform.position;
}
function OnEndGame()
{
// Disable joystick when the game ends
moveJoystick.Disable();
rotateJoystick.Disable();
// Don't allow any more control changes when the game ends
this.enabled = false;
}
function Update()
{
//transform.LookAt(PlayerRelativeControl.blackhole, Vector3.forward);
var movement = thisTransform.TransformDirection( Vector3( 0, moveJoystick.position.y, moveJoystick.position.x ) );
//Only x and y movement
movement.z = 0;
movement.Normalize();
// 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;
}
}
if ( absJoyPos.x > absJoyPos.y )
{
if ( moveJoystick.position.x > 0 )
movement *= sidestepSpeed * absJoyPos.x;
else
{
movement *= sidestepSpeed * absJoyPos.x;
}
}
if ( rotateJoystick.tapCount == 2 )
{
var blaster : GameObject = Instantiate(blasterPrefab, GameObject.Find("blasterPoint").transform.position, Quaternion.identity);
// Propells blasted ammunition forward
//blaster.rigidbody.AddForce(blaster.transform.forward * 2000);
//end fire weapon phase
}
//velocity = character.velocity;
movement += velocity;
movement *= Time.deltaTime;
// Actually move the character
character.Move( movement );
}