Use joystick to change charcters facing?

First of all thanks for reading my question!

I have taken the time to do many forum searches and come up empty handed or just missing it. What I am looking to do is to control the facing with the second joystick. Nothing to do will he camera at all. For example I have the joystick to the right, the character is rotated on his y axis 90 degrees. South would be 180 etc. I would like this to work for any rotation 1-360. But I can’t seem to figure out how to go about this.

I am new to the joysticks and iOS in general. Anyone able to throw a helpful hint or code snippet?

Ok, I have gotten the joystick to work decently, but the rotation is to choppy. Hoping to do some sort of Slerp but not the best at this yet. Here is my code for anyone looking to do a similar thing or someone that can help me smooth it out.

function FaceAttackDirection()
{	
	if(rotateJoystick.position.x != 0 || rotateJoystick.position.y != 0) {
	
    	var rotationDirection = Vector3(rotateJoystick.position.x, 0 , rotateJoystick.position.y);
    	rotationDirection = rotationDirection.normalized;

    	//Create a rotation facing that point.
    	var rotation = Quaternion.LookRotation(rotationDirection);
    	transform.rotation = rotation;
    }
}

Not my code got it from unity answers.

I did this but in a strange way that worked out great. I just had an invisible object that was a child of the character. the relative position of the invisible object would move front back, left and right and all the diagonals based on the joystick moves. then i had the character look at the invisible object. this worked out great and was very smooth.

Look into the Angry Bots Demo scene, its actually done in there.

Thanks for the advice guys!

@GameFaceMe, how did you send the variables of the stick to influence the position of your object since the X and Y are between -1 and 1?

@marjan, what the joystick does in that tutorial in what I would need I imagine. Although it would be an entire overhaul of my code to change to how they are doing it. I’ll keep reading their code to see if i can find something.

Does anyone know how to just slerp the code I posted? I’d rather just use the way I found as it is muc less code and I understand it so far. Adding that much code copy/paste and getting it to work…well I feel that is beyond my level of understanding to implement corretly.

I just take the player’s position and add the rotation joystick x and y to it and assign that to the invisible “look at” object’s position. here’s the code:

	lookAt.position = thisTransform.position + Vector3 (rotateJoystick.position.x,0,rotateJoystick.position.y);
	thisTransform.LookAt(lookAt);

I worked on a joystick for my current game, here it is, and how the player interprets the joysticks for movement and looking (if the look joystick is not in use then the player will look in the direction he is moving). These joysticks are clamped in circles and give full 360 movement, not 8 directional (used to be like that but I needed more precision so I changed it).

Joystick

#pragma strict

var joystick : GUITexture;
var jBG : GUITexture;
var jTZ : GUITexture;
var otherJ : JoystickTest5;
var jOffset : Vector2;
var position : Vector2;
var minDeadZone : Vector2 = Vector2(-5, -5);
var maxDeadZone : Vector2 = Vector2(5, 5);
var bgSize : float;
var latched : boolean = false;
private var Oid : int;

private var maxDistance : float;
private var roundTo : float = 10;
private var rNumIntX : float;
private var rNumIntY : float;
private var radiusX : float;
private var radiusY : float;
private var addX : float;
private var addY : float;
var touchID : int;
private var touch : Touch;
private var tCount : int = 0;
private var inDeadZoneX : boolean = true;
private var inDeadZoneY : boolean = true;
private var guiTouchPos : Vector2;
private var dist : float;
private var dirVector : Vector2;
private var clampedPos : Vector2;
private var center : Vector2;
private var jHit : boolean;
private var purePos : Vector2;
private var activated : boolean = false;

var jID : int;
var randomOffset : float;


function Start(){
jBG.pixelInset.width = bgSize;
jBG.pixelInset.height = bgSize;
jTZ.pixelInset.width = bgSize;
jTZ.pixelInset.height = bgSize;
joystick.pixelInset.width = bgSize / 2;
joystick.pixelInset.height = bgSize / 2;
center.x = joystick.pixelInset.x + (joystick.pixelInset.width/2);
center.y = joystick.pixelInset.y + (joystick.pixelInset.height/2);
jBG.pixelInset.x = center.x - (jBG.pixelInset.width/2);
jBG.pixelInset.y = center.y - (jBG.pixelInset.height/2);
jTZ.pixelInset.x = center.x - (jTZ.pixelInset.width/2);
jTZ.pixelInset.y = center.y - (jTZ.pixelInset.height/2);
maxDistance = (jBG.pixelInset.width/2) / 1.5;
addX = (jBG.pixelInset.width/1000) + 0.25;
addY = (jBG.pixelInset.height/1000) + 0.25;
}

function Recalculate(){
jBG.pixelInset.width = bgSize;
jBG.pixelInset.height = bgSize;
jTZ.pixelInset.width = bgSize;
jTZ.pixelInset.height = bgSize;
joystick.pixelInset.width = bgSize / 2;
joystick.pixelInset.height = bgSize / 2;
center.x = joystick.pixelInset.x + (joystick.pixelInset.width/2);
center.y = joystick.pixelInset.y + (joystick.pixelInset.height/2);
jBG.pixelInset.x = center.x - (jBG.pixelInset.width/2);
jBG.pixelInset.y = center.y - (jBG.pixelInset.height/2);
jTZ.pixelInset.x = center.x - (jTZ.pixelInset.width/2);
jTZ.pixelInset.y = center.y - (jTZ.pixelInset.height/2);
maxDistance = (jBG.pixelInset.width/2) / 1.5;
addX = (jBG.pixelInset.width/1000) + 0.25;
addY = (jBG.pixelInset.height/1000) + 0.25;
}

function Update(){
for(var i = 0; i < Input.touchCount; i++){
var realTouch : Touch = Input.touches[i];
guiTouchPos = touch.position - jOffset;
jHit = jTZ.HitTest(realTouch.position);
if(realTouch.phase == TouchPhase.Began){
if(jHit){
latched = true;
touchID = realTouch.fingerId;
Oid = touchID;
}
}

if(latched){
if(Input.touchCount <= 1){
if(touchID >= 1){
touchID = 0;
}
}
else if(Input.touchCount > (Oid)){
touchID = Oid;
}
else{
touchID = Input.touchCount - 1;
}

touch = Input.touches[touchID];

if(touch.phase == TouchPhase.Ended){
if(!jHit){
latched = false;
touchID = -1;
}
}
}
}

if(latched){
        
clampedPos = GetConstrainedPosition(center, guiTouchPos);
        
if(activated){
joystick.pixelInset.x = clampedPos.x - (joystick.pixelInset.width/2);
joystick.pixelInset.y = clampedPos.y - (joystick.pixelInset.height/2);
}
else{
joystick.pixelInset.x = guiTouchPos.x - (joystick.pixelInset.width/2);
joystick.pixelInset.y = guiTouchPos.y - (joystick.pixelInset.height/2) ;
}
}
else{
joystick.pixelInset.x = center.x - (joystick.pixelInset.width/2);
joystick.pixelInset.y = center.y - (joystick.pixelInset.height/2);
}

purePos.x = (joystick.pixelInset.x - center.x);
purePos.y = (joystick.pixelInset.y - center.y);

if(purePos.x > 0){
purePos.x += addX;
}
else{
purePos.x -= addX;
}
if(purePos.y > 0){
purePos.y += addY;
}
else{
purePos.y -= addY;
}

if(purePos.x < maxDeadZone.x){
if(purePos.x > minDeadZone.x){
inDeadZoneX = true;
}
else{
inDeadZoneX = false;
}
}
else{
inDeadZoneX = false;
}

if(purePos.y < maxDeadZone.y){
if(purePos.y > minDeadZone.y){
inDeadZoneY = true;
}
else{
inDeadZoneY = false;
}
}
else{
inDeadZoneY = false;
}

//Check if in DeadZoneX
if(inDeadZoneX){
position.x = 0;
}
else{
radiusX = (10/maxDistance)/10;
//position.x = Mathf.Round((purePos.x * radiusX) + 0.75);// * roundTo;
position.x = (purePos.x * radiusX) + randomOffset;
}
//Check if in DeadZoneY
if(inDeadZoneY){
position.y = 0;
}
else{
radiusY = (10/maxDistance)/10;
//position.y = Mathf.Round((purePos.y * radiusY) + 0.75);// * roundTo;
position.y = (purePos.y * radiusY) + randomOffset;
}
if(jID == 1){
Debug.Log("position = " + position);
}
}

function GetConstrainedPosition(midPoint : Vector2, endPoint : Vector2)
{
dist = Vector2.Distance(midPoint, endPoint);

if (Vector2.Distance(midPoint, endPoint) > maxDistance)
{
activated = true;
//the touch is outside the radius.  Return the clamped position.
dirVector = endPoint - midPoint;
dirVector.Normalize();
return (dirVector * maxDistance) + midPoint;
}
//this returns the touched position if we're inside the radius.
activated = false;
return clampedPos;
}

Character Controller

//Character Controller
var character : CharacterController;

//Vector3's
var movement : Vector3;
var ForwardLook : Vector3;
var newForward : Vector3;
var velocity : Vector3;
var absJoyPos : Vector3;

//Joysticks
var LookJoystick : Joystick;
var MoveJoystick : Joystick;

//Floats
var movementSpeed : float;
var rotationSpeed : float;

function Start(){
Move();
Look();
}

function Move(){
while(true){
movement = Vector3(MoveJoystick.position.x, 0, MoveJoystick.position.y);

movement.y = 0;
movement.Normalize();
    
absJoyPos = Vector2(Mathf.Abs(MoveJoystick.position.x), Mathf.Abs(MoveJoystick.position.y));
movement *= movementSpeed;

movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
	
character.Move(movement); 

yield;
}
}

function Look(){
while(true){
if(!LookJoystick.latched){
ForwardLook = Vector3(MoveJoystick.position.x, 0, MoveJoystick.position.y);
}
else{
ForwardLook = Vector3(LookJoystick.position.x, 0, LookJoystick.position.y);
}
newForward = Vector3.Slerp(transform.forward, ForwardLook.normalized, rotationSpeed * Time.deltaTime);
transform.forward = newForward;
yield;
}
}

Thank you, the first good answer of this type q-s