Question by
Jomelskie · Oct 19, 2016 at 01:05 PM ·
camerarotationcharactercharacter controllercharacter movement
Help for my character Rotation When using Joystick
Hi. I just want to ask help for my Character Rotation..
My Code works when i'm using keyboard input instead of Joystick..
The desired output is this: If turn the camera using swipe.. the character should go forward to the new rotation of the camera..
when i apply my joystick, it doesn't work and the character just walk all the way to its original rotation not to the new rotation of the camera..
Care to see my code? Hehe Thank you very much. Here it is. VIRTUALJS.CS using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.EventSystems;
public class virtualJS : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler{
public Animator anim;
private Image bgJoy;
private Image joystick;
public Vector3 InputDirection { set; get;}
private void Start()
{
bgJoy = GetComponent<Image> ();
joystick = transform.GetChild(0).GetComponent<Image> ();
InputDirection = Vector3.zero;
setTransparency (0.5f);
}
private void setTransparency(float num)
{
Color color2 = joystick.color;
color2.a = num;
joystick.color = color2;
}
public virtual void OnDrag(PointerEventData ped)
{
setTransparency (0.9f);
Vector2 pos = Vector2.zero;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle (bgJoy.rectTransform, ped.position, ped.pressEventCamera, out pos)) {
pos.x = (pos.x / bgJoy.rectTransform.sizeDelta.x);
pos.y = (pos.y / bgJoy.rectTransform.sizeDelta.y);
float x = (bgJoy.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
float y = (bgJoy.rectTransform.pivot.y == 1) ? pos.y * 2 + 1 : pos.y* 2 - 1;
InputDirection = new Vector3 (x, 0, y);
if (InputDirection.magnitude == 0) {
anim.SetBool ("isWalking",false);
anim.SetBool ("isIdle",true);
anim.SetBool ("isPunching",false);
anim.SetBool ("isRunning",false);
}
else if (InputDirection.magnitude > 0.9) {
anim.SetBool ("isWalking",false);
anim.SetBool ("isIdle",false);
anim.SetBool ("isPunching",false);
anim.SetBool ("isRunning",true);
} else {
anim.SetBool ("isIdle",false);
anim.SetBool ("isPunching",false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isWalking", true);
}
InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
Debug.Log (InputDirection);
joystick .rectTransform.anchoredPosition =
new Vector3 (InputDirection.x * (bgJoy.rectTransform.sizeDelta.x / 3.3333333f)
, InputDirection.z * (bgJoy.rectTransform.sizeDelta.y/3.3333333f));
}
}
public virtual void OnPointerUp(PointerEventData ped)
{
InputDirection = Vector3.zero;
joystick.rectTransform.anchoredPosition = Vector3.zero;
anim.SetBool ("isIdle",true);
anim.SetBool ("isPunching",false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isWalking", false);
setTransparency (0.5f);
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag (ped);
}
}
CHARACTERWALKER.CS
using UnityEngine;
using System.Collections;
using Devdog.General;
using Devdog.General.UI;
[RequireComponent(typeof(CharacterController))]
public class CharacterWalker : MonoBehaviour, IPlayerInputCallbacks
{
[InspectorReadOnly]
public bool isGrounded;
[Header("Config")]
public float walkSpeed = 2f;
public float turnSpeed = 2f;
public float jumpSpeed = 40f;
public float gravity = 9.81f;
public float rotationSpeed = 1.0f;
public float heightModifier = 1.45f;
[SerializeField]
[Required]
private Camera _cam;
private CharacterController _controller;
private Animator _anim;
private Vector3 _moveDirection = Vector3.zero;
public virtualJS joystick;
protected virtual void Start()
{
_controller = GetComponent<CharacterController>();
_anim = GetComponent<Animator> ();
}
public void SetInputActive(bool active)
{
this.enabled = active;
}
protected void Update()
{
if (UIUtility.isFocusedOnInput)
{
return;
}
if (joystick.InputDirection.magnitude != 0) {
_controller.transform.forward = Vector3.Normalize (joystick.InputDirection);
}
RotateToCamera(false); // Don't force rotation only if we move
if (isGrounded)
{
_moveDirection = CalcDirection();
if (Input.GetButtonDown("Jump"))
{
_moveDirection.y = jumpSpeed * heightModifier;
}
}
_moveDirection.y -= gravity * Time.deltaTime;
var flags = _controller.Move(_moveDirection * Time.deltaTime);
isGrounded = (flags & CollisionFlags.CollidedBelow) != 0;
float x = 0;
if (Input.GetMouseButton(1))
{
x = Input.GetAxis("Mouse X") * turnSpeed;
}
else if (joystick.InputDirection.y != 0 || joystick.InputDirection.x != 0f)
{
x = Input.GetAxis("Mouse X") * turnSpeed;
}
transform.Rotate(0, x * rotationSpeed, 0);
}
public Vector3 CalcDirection()
{
//_moveDirection = new Vector3(GetHorizontal(), 0, GetVertical());
_moveDirection = new Vector3(joystick.InputDirection.x, 0 , joystick.InputDirection.y);
_moveDirection = _cam.transform.TransformDirection(_moveDirection);
_moveDirection *= walkSpeed;
_moveDirection.y = 0;
return _moveDirection;
}
public void RotateToCamera(bool force)
{
//Vector3 t = new Vector3(GetHorizontal(), 0, GetVertical());
Vector3 t = new Vector3(joystick.InputDirection.x, 0, joystick.InputDirection.y);
if (t != Vector3.zero || force == true)
{
Quaternion nRot = _cam.transform.rotation;
nRot.x = 0;
nRot.z = 0;
transform.rotation = Quaternion.Lerp(transform.rotation, nRot, Time.deltaTime * rotationSpeed);
}
}
public float GetVertical()
{
/* Original */
/*
float vert = Input.GetAxis("Vertical");
if (vert < 0)
{
vert = (vert / 2f); // Twice as slow backwards
}
return vert;
*/
return Input.GetAxis("Vertical");
}
public float GetHorizontal()
{
return Input.GetAxis("Horizontal");
}
}
Comment