LoginSignup
0
0

More than 1 year has passed since last update.

Unityメモ 移動する床とプレイヤー 親子関係

Last updated at Posted at 2020-11-22

プレイヤーのスクリプト
レイでの判定分け レイヤー使用

void OnCollisionStay(Collision col)
{

    if (Physics.Linecast(m_charaRay.position, m_charaRay.position + Vector3.down, LayerMask.GetMask("Ground")))
    {
        m_isGroundCollider = true;
    }
    else if(Physics.Linecast(m_charaRay.position, m_charaRay.position + Vector3.down,LayerMask.GetMask("MoveGround")))
    {
        m_isGroundCollider = true;
        gameObject.transform.SetParent(col.transform);//親子関係を設定
    }
    else
    {
        m_isGroundCollider = false;
        gameObject.transform.SetParent(null);//親子関係を外す
    }


}

※注意点
プレイヤーでの移動方法で挙動が変わってくるので現在は
transform.positionを操作している

    Vector3 moveDir = Vector3.zero;

    if (Input.GetKey(KeyCode.W))
    {
        moveDir += forwardDir;
    }
    if (Input.GetKey(KeyCode.S))
    {
        moveDir -= forwardDir;
    }
    if (Input.GetKey(KeyCode.D))
    {
        moveDir.z += m_moveSpeed;
    }
    if (Input.GetKey(KeyCode.A))
    {
        moveDir.z -= m_moveSpeed;
    }

    //トランスフォームで移動させたら移動する床でも子オブジェクト状態でも移動できる
    if (moveDir.sqrMagnitude > Mathf.Epsilon)
    {

        transform.position += moveDir;
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0