Skip to content

Instantly share code, notes, and snippets.

@nabesi777
Created October 4, 2018 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabesi777/6e826db4e77f355c4d781abe60a95296 to your computer and use it in GitHub Desktop.
Save nabesi777/6e826db4e77f355c4d781abe60a95296 to your computer and use it in GitHub Desktop.
スマホ BlueToothコントローラー用スクリプト
using UnityEngine;
using System.Collections;
public class BlueToothCon2 : MonoBehaviour
{
private Animator anim;
private CharacterController characterController;
private Vector3 velocity;
private float jumpPower = 5f;
public float forwardSpeed = 5.0f;
public float backwardSpeed = 2.0f;
void Start()
{
anim = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
velocity = Vector3.zero;
Input.backButtonLeavesApp = true;
}
void FixedUpdate()
{
if (characterController.isGrounded)
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool fire1 = Input.GetButtonDown("Fire1");
// キャラクターのローカル空間での方向
velocity = new Vector3(0, 0, v);
//velocity = transform.transform.forward * speed * Input.GetAxis("Vertical");
// 回転
transform.Rotate(0, h, 0);
// キャラクターのローカル空間での方向に変換
velocity = transform.TransformDirection(velocity);
if (v > 0.1)
{
velocity *= forwardSpeed; // 移動速度を掛ける
anim.SetBool("Run", true);
}
else if (v < -0.1)
{
velocity *= backwardSpeed; // 移動速度を掛ける
anim.SetBool("Walkback", true);
}
else
{
anim.SetBool("Run", false);
anim.SetBool("Walkback", false);
}
// PS4コントローラーXボタンor左クリックを押したらY軸方向の速度にジャンプ力を足す
if (Input.GetButtonDown("Fire1"))
{
velocity.y += jumpPower;
}
anim.SetBool("Jump", fire1);
}
velocity.y += Physics.gravity.y * Time.deltaTime;
characterController.Move(velocity * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment