- Home /
joystick changing my character facing direction and moving within screen only
i'm new to unity3d, i readed a guide here
and i using a joystick controller that can easily found it using google name MPJoystick
i try to change the mouse click move to move using joystick, now my character can move, but the character facing direction won't, i have no idea why.
1 april 2014 9:50PM: after some try, finally i can change the facing direction, next i need to control the character inside the screen and not go over, anyone know how?i tried using the Matf.Clamp solutions that i found but not working, no idea why
here is my code
controller.cs
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public float moveSpeed;
public float screenWidth;
public float screenHeight;
public MPJoystick moveJoystick;
private Vector3 moveDirection;
public Vector3 faceDirection;
public float turnSpeed;
public Vector3 playerSize;
public float distance;
public float leftBorder;
public float rightBorder;
public float bottomBorder;
public float topBorder;
public void Awake() {
}
void Start () {
screenWidth = Screen.width;
screenHeight = Screen.height;
playerSize = renderer.bounds.size;
distance = (transform.position - Camera.main.transform.position).z;
leftBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).x + (playerSize.x/2);
rightBorder = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance)).x - (playerSize.x/2);
bottomBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).y + (playerSize.y/2);
topBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, distance)).y - (playerSize.y/2);
faceDirection = Vector3.right;
}
void Update () {
Vector3 currentPosition = transform.position;
float touchKey_x = moveJoystick.position.x;
float touchKey_y = moveJoystick.position.y;
if (touchKey_x == 0 && touchKey_y == 0) {
return;
} else {
/* Change facing direction */
faceDirection.y = moveDirection.y + touchKey_y;
faceDirection.x = moveDirection.x + touchKey_x;
float targetAngle = Mathf.Atan2(faceDirection.y, faceDirection.x) * Mathf.Rad2Deg;
transform.rotation =
Quaternion.Slerp( transform.rotation,
Quaternion.Euler( 0, 0, targetAngle ),
turnSpeed * Time.deltaTime );
/* i think the next 3 line is change the facing direction? am i right? answer: i think i'm wrong */
moveDirection.x = moveDirection.x + touchKey_x;
moveDirection.y = moveDirection.y + touchKey_y;
moveDirection.Normalize ();
/* i guess here is the moving code? am i right? */
Vector3 target = moveDirection * moveSpeed + currentPosition;
/*target.x = Mathf.Clamp (target.x, leftBorder, rightBorder);
target.y = Mathf.Clamp (target.y, topBorder, rightBorder);*/
transform.position = Vector3.Lerp (currentPosition, target, Time.deltaTime);
}
}
}
and how can i avoid the character go over the screen? if i try without the target.x = Math.Clamp blablabla and target.y = Math.Clamp blablabla, the character can move well but the problem is the character can go over the screen,
but when i try using Math.Clamp, the character move become so weird, sometime i move top but go bottom, i move bottom but go top, sometime stuck at center but sometime can go over...
Your answer
Follow this Question
Related Questions
2d Movement Jumps to the Left 3 Answers
Using animated transform to push object away? 2D 0 Answers
2d gameplay movement script 1 Answer
2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers
Type"PathDefinition" does not contain a definition for "GetPathEnumerator" 0 Answers