这是我的虚拟操纵杆控制器脚本。现在我怎样才能将它用于控制fps角色控制器的前后移动?
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class JoyStickController : MonoBehaviour , IDragHandler, IPointerUpHandler ,IPointerDownHandler{
private Image bgImg;
private Image joyStickImg;
public Vector3 InputDirection{ set; get; }
// Use this for initialization
void Start () {
bgImg = GetComponent<Image> ();
joyStickImg = transform.GetChild (0).GetComponent<Image> ();
InputDirection = Vector3.zero;
}
public virtual void OnDrag(PointerEventData ped){
Vector2 pos = Vector2.zero;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle (bgImg.rectTransform,
ped.position, ped.pressEventCamera, out pos)) {
pos.x = (pos.x / (bgImg.rectTransform.sizeDelta.x));
pos.y = (pos.y / (bgImg.rectTransform.sizeDelta.y));
float x = (bgImg.rectTransform.pivot.x == 1) ? pos.x * 2 + 1 : pos.x * 2 - 1;
float y = (bgImg.rectTransform.pivot.y == 1) ? pos.y* 2 + 1 : pos.y * 2 - 1;
InputDirection = new Vector3 (x, 0, y);
InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
joyStickImg.rectTransform.anchoredPosition = new Vector3 (InputDirection.x * (bgImg.rectTransform.sizeDelta.x / 3)
, InputDirection.z * (bgImg.rectTransform.sizeDelta.y / 3));
}
}
public virtual void OnPointerDown(PointerEventData ped){
OnDrag (ped);
}
public virtual void OnPointerUp(PointerEventData ped){
InputDirection = Vector3.zero;
joyStickImg.rectTransform.anchoredPosition = Vector3.zero;
Debug.Log("Unpress");
}
public float Horizontal()
{
if (InputDirection.x != 0)
return InputDirection.x;
else
return Input.GetAxis("Horizontal");
}
public float Vertical()
{
if (InputDirection.z != 0)
return InputDirection.z;
else
return Input.GetAxis("Vertical");
}
}
这是我的虚拟操纵杆控制器脚本。现在我该如何将它用于控制fps角色控制器的前后移动?
答案 0 :(得分:0)
FPS使用CharacterController
。使用CharacterController.Move
移动CharacterController,移动函数将mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(EmailPasswordActivity.this, R.string.auth_failed,
Toast.LENGTH_SHORT).show();
}
// ...
}
});
作为参数。
您只需将Vector3
从视觉操纵杆传递到InputDirection
参数。
Move
或者
使用Visual Joystick中的float speed = 20;
CharacterController controller = GetComponent<CharacterController>();
controller.Move(InputDirection * speed * Time.deltaTime);
和Horizontal()
功能。
Vertical()