I am using Unity IOS
I have 2 joysticks in my game.
One for moving.
and one for aiming.
The one for moving is working fine.
But I can't seem to get the one for aiming right, How would I the top half of the player face where the joystick is pointing and preferably stay facing that direction until the joystick is pressed again?
Much like the "alive for ever" game for the iPod and iPhone.
here is the code for the movement joystick I have been trying to fiddle with this code to try and get it to be applicable for the top half of the player, only problem is this code moves the player while making them face in the direction of the joystick.
Thanks in advance :)
@script RequireComponent( CharacterController )
var moveJoystick : Joystick;
var cameraTransform : Transform;
var speed : float = 6;
private var thisTransform : Transform;
private var character : CharacterController;
function Start(){
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
}
function FaceMovementDirection(){
var horizontalVelocity : Vector3 = character.velocity;
horizontalVelocity.y = 0;
if ( horizontalVelocity.magnitude > 0.1)
thisTransform.forward = horizontalVelocity.normalized;
}
function Update(){
var movement = cameraTransform.TransformDirection( Vector3(
moveJoystick.position.x, 0, moveJoystick.position.y ) );
movement.y =0;
movement.Normalize();
var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ),
Mathf.Abs( moveJoystick.position.y ) );
movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
movement *= Time.deltaTime;
// Actually move the character
movement += Physics.gravity;
character.Move( movement );
FaceMovementDirection();
}
I have commented it out heavily so hopefully that helps
if you have any other problems just leave a comment
Also this code was done with the IOS
so if you are using a different system you may have to adjust
//Author Dominic Obojkovits
//Dual Movement Script for TopDown Shooter, or dual Joystick controlled shooter
//This script is COPYLEFTED and cannont be copyrighted
@script RequireComponent( CharacterController )
var moveJoystick : Joystick;
var cameraTransform : Transform;
var speed : float = 6;
private var thisTransform : Transform;
private var character : CharacterController;
var newTransform : Transform;
var rotateJoystick : Joystick;
var rotationSpeed : Vector2 = Vector2( 50, 50 );
var botTransform : Transform;
var targetTrans : Transform;
function Start(){
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
}
function FaceMovementDirection(){
//This finds the direction that the character is moving in, and will face his Bottom half in that direction
var horizontalVelocity : Vector3 = character.velocity;
horizontalVelocity.y = 0;
if ( horizontalVelocity.magnitude > 0.1)
botTransform.forward = horizontalVelocity.normalized * 180;
}
function Update(){
//Code for Moving
//The code for moving just moves the player in the direction your left hand joystick is facing
//but this code is specific to the IOS so if you are using a different system I reccomend something else
var movement = cameraTransform.TransformDirection( Vector3(
moveJoystick.position.x, 0, moveJoystick.position.y ) );
movement.y =0;
movement.Normalize();
var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ),
Mathf.Abs( moveJoystick.position.y ) );
movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
movement *= Time.deltaTime;
// Actually move the character
movement += Physics.gravity;
character.Move( movement );
//called to make the bottom half face the direction he is walking
FaceMovementDirection();
//here we make a vector of where your right joystick is
var targetPos = rotateJoystick.position;
//With this code we take an empty game object and put it in the direction that your right Joystick is pointing
//a side not is that I timesed the possition of the object by 150 because 150 is far larger then the field my level is in
//if u sence is bigger make sure the 150 bellow is larger then your sence to insure the code works
targetTrans.position = Vector3(targetPos.x*150,1.3,targetPos.y*150);
//Then we make the Top half of our player face that empty game object
newTransform.LookAt(targetTrans);
//this way the player can look in the direction of the Right hand side Joystick
//and walk in the direction of the Left Hand Side Joystick
}
Anyone know how i can add a jump to the right joystick and have a character moving with the left joystick as the script above shows, (so not clamping back to the center) i have tried and struggling to put the two together!
I might be wrong on this but I have come to realize that the Joystick connects to the character controller and the character controller makes the character move in whatever direction. Well I had the same problem and it was easily solved by setting up my camera behind my character and adhering to the the following values:
so positive motion on the X in the joystick is equal to positive movement of the character, positive motion on the Y in the joystick = positive motion on the Z in the actual character.
so if your character is facing the right way and your camera is right behind it facing the correct direction you should have no problem using the first person character controller provided by unity and simply moving the camera back a bit. Its silly but the original position of your character and how it’s facing is very critical.
Can anyone tell me how i can change the axis that the player is moving on, im working on a 2d game and would like my player to move up and down on the Y axis, but atm it is moving on the X and Z axis meaning that it walks through my background
im using the script from the answer above (Dominic) and then the Joystick from the Penelope tutorial, please help me thank you