Notifications
Article
移動した方向のままでいてほしい
Published 6 months ago
152
0
とりあえず全方向ではないですが、移動する方向に斜めにしました

一応移動はあと前進後退ではなく全方向に同じように向くように作りたいと思ってます。 課題としては今は停止しているとカメラのほうに向いてしまうことですかね。 自由度の高い移動よりも4方向にしか移動できないほうがよさそうな気がしました。
移動のコードです。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movement : MonoBehaviour { public float speed; public float rotateSpeed; public float jumpPower; public float jumpHeight = 0; public float playerYpos; private float maxJumpHeight = 1.0f; public bool isJumpOk = false; public bool isGround = false; Rigidbody rd; // Start is called before the first frame update void Start() { rd = GetComponent<Rigidbody>(); rotateSpeed = Mathf.Floor(rotateSpeed); //playerYpos = transform.position.y; } // Update is called once per frame void FixedUpdate() { _Move(); _Jump(); } void _Move() { float moving = Input.GetAxis("Vertical") * speed; moving *= Time.deltaTime; transform.Translate(new Vector3(moving, 0, 0)); float rotating = Input.GetAxis("Horizontal") * speed; rotating *= Time.deltaTime; //transform.Translate(new Vector3(rotating, 0, 0)); float checkMove = Input.GetAxisRaw("Vertical"); float checkRotate = Input.GetAxisRaw("Horizontal"); //jumpHeight = playerYpos - transform.position.y; if (isGround) { jumpHeight = 0; playerYpos = transform.position.y; if (checkMove != 0 || checkRotate!=0) { rd.AddForce(Vector3.up * 5.0f, ForceMode.Impulse); } } //移動すると移動方向に傾ける if (checkMove == 1) { transform.rotation = Quaternion.Euler(0, 0, -5.0f); //transform.Rotate(new Vector3(0, 0, -5.0f)); } else if (checkMove == 0) { transform.rotation = Quaternion.Euler(0, 0, 0); } else if (checkMove == -1) { transform.rotation = Quaternion.Euler(0, 0, 5.0f); } //以下、両方-rotatingでちゃんと違う方向むいて動いているのが不思議。。。わけわからん if (checkRotate == 1) { transform.Translate(new Vector3(0, 0, -rotating)); transform.rotation = Quaternion.Euler(0,rotateSpeed,-5.0f); } else if (checkRotate == -1) { transform.Translate(new Vector3(0, 0, -rotating)); transform.rotation = Quaternion.Euler(0, -rotateSpeed, -5.0f); } } void _Jump() { jumpHeight = transform.position.y - playerYpos; if (Input.GetKey(KeyCode.Space)){ if (jumpHeight < maxJumpHeight && isJumpOk == true) { rd.AddForce(Vector3.up * jumpPower, ForceMode.Impulse); } if(jumpHeight > maxJumpHeight) { isJumpOk = false; } } } private void OnCollisionStay(Collision coll) { if (coll.gameObject.tag == "Ground") { //jumpHeight = transform.position.y - playerYpos; isGround = true; isJumpOk = true; //jumpHeight = 0; } } private void OnCollisionExit(Collision collision) { if(collision.gameObject.tag == "Ground") { //jumpHeight = transform.position.y - playerYpos; isGround = false; } } }
Satoshi Watanabe
Unity Beginner - Other
6
Comments