0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Unity 極力少ない労力で移動操作

Last updated at Posted at 2021-09-25

極力少ない労力で。

using UnityEngine;
public class moveo : MonoBehaviour
{
    void Update()
    {
        //カメラをこのオブジェクトの下に加えると追従。
        float speed = 10.0f;
        float rotonce=15f;
        float tz = Input.GetAxis("Vertical") * speed * Time.deltaTime;
        float tx = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        float ry = Input.GetKey("space") ? rotonce : 0;
        //
        transform.Translate(tx, 0, tz);
        transform.Rotate(0, ry, 0);
    }
}

ctr版


using UnityEngine;
public class CharMove : MonoBehaviour
{
    CharacterController ctr;
    void Start(){
# if !UNITY_WEBGL
        Application.targetFrameRate = 20;
# endif
        ctr = gameObject.GetComponent<CharacterController>();
        if(ctr==null){
            gameObject.AddComponent<CharacterController>();
            ctr = gameObject.GetComponent<CharacterController>();
            ctr.height = 0.9f;
            ctr.skinWidth = ctr.height * 0.1f;
            ctr.radius = 0.4f;
            ctr.stepOffset = ctr.radius * 0.1f;
            ctr.minMoveDistance = 0.001f;
            ctr.slopeLimit = 45f;
        }
        //ctr = gameObject.GetComponent<CharacterController>();
    }
    void Update()
    {
        //カメラをこのオブジェクトの下に加えると追従。
        float speed = 10.0f / 2;
        float rotspeed = 360f / 1;
        float gravity =9.8f;
        float ty = ctr.isGrounded?0:-1*gravity*gravity*Time.deltaTime;
        float tz = ctr.isGrounded?Input.GetAxis("Vertical") * speed * Time.deltaTime:0;
        float ry = ctr.isGrounded?Input.GetAxis("Horizontal") * rotspeed * Time.deltaTime:0;

        //transform.Translate(0, 0, tz);
        ctr.Move( transform.TransformDirection(0,ty,tz)); 
        transform.Rotate(0, ry, 0);

        if (Input.GetKeyDown("space")){
            transform.Round().RoundAngleY();
            //Debug.Log(ctr.isGrounded);
        }
    }
}//class

static class transformRoundExtension
{
    public static Transform Round(this Transform @this, bool yesX = true, bool yesY = false, bool yesZ = true)
    {
        var demi = 0.001f;//just 0.5 is upper;
        var p = @this.localPosition;
        var x = yesX ? Mathf.Round(p.x + demi) : p.x;
        var y = yesY ? Mathf.Round(p.y + demi) : p.y;
        var z = yesZ ? Mathf.Round(p.z + demi) : p.z;
        @this.localPosition = new Vector3(x, y, z);
        return @this;
    }
    public static Transform RoundAngleY(this Transform @this, float angle = 90)
    {
        var demi = angle / 2 + 0.001f;//just 0.5 is upper;
        var q = @this.localRotation.eulerAngles;
        var y = (int)(Mathf.Round(q.y + demi) / angle) * angle;
        @this.localRotation = Quaternion.Euler(q.x, y, q.z);
        return @this;
    }

}//class

最適な値はこのあたりに。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?