- Home /
Make Player face where Joystick is pointing?
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'll try, don't have unity iOS or joysticks, so I can't test it, but I'll use logic and try my best.
since i have no idea about joysticks (im sure you mean the real ones like on console controllers (wii's nunchuck)) i have to play that game to see what you mean...i'm afraid to say i might not be able to help you, but i have to keep my promise and try!
ahh, i see what you mean, is your character two parts (waist-below, waist above?) becaus ethat would be easy (make one joystick only control movement, and one control the rotation of the upper player and if you want, shooting.) so, is your character(s) two parts?
Sorry for the late reply was at a new years thing out of town. Yes $$anonymous$$y character is two parts :). The IOS joysticks act in the exact same way that a Wii mote joystick would act, and u can also get the position of the joystick as well as its position with reference to its origin. In short yes my character is two parts, and so far i have the bottom part moving and the top part rotating the around in circles when the joystick is moved left or right, but I need the gun to face where the joystick is pointing not just rotate when the joystick is moved, so that is my current problem. Thanks a ton BTW
$$anonymous$$uch like the game Alive 4ever for the iphone/ipod control system
Answer by AtomicMarine · Jan 07, 2011 at 03:22 PM
This is the code i used to solve the problem
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
}
Answer by Patrick Neaves · Jan 07, 2011 at 02:49 AM
Can you tell us how? I need to know as well :S
EDIT: Sorry for putting as an answer :/. Can't delete though.
sure ill put the code up now bellow in an actual answer and np about u putting the answer up :) haha
Answer by be7a · Apr 08, 2012 at 10:48 PM
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!
Thanks :)
Answer by juanrodriguez001 · May 09, 2012 at 06:34 PM
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:
The Joystick : The Actual Character
pos.x = pos.x neg.x = neg.x pos.Y = pos.Z neg.Y = neg.Z
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.
regards,
J.
Answer by jona519b · May 24, 2012 at 03:31 PM
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 :)
I work in Blender so when I export to FBX ..Blender allows you to change the "Z" axis to be "Y" in unity. Unity seems to be the only program that considers the "Y" as up. In pretty much all 3D software Packages "z" is up. so before you export make sure to change your "Axis" via the settings of your objects origin. So that when you bring your "object" into unity "z" should be in front of the character as if he/she was looking in the "z" direction.
apply the script and done.
Hope this helps.