Unity3D 坂道を上がるとYの値が増えてしまい重力が逆向きに上がってしまうコードの修正法
坂道を上がりキーを離すとその分?上に上がってしまうバグの原因と修正方が知りたいです(提示画像参照)。下にさがる重力の仕様があるのでAddForceは消したくありません。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
const float walk_speed_max = 20.0f;
Rigidbody rb;
Vector3 move;
float input_h;
float input_v;
Vector3 move_x;
Vector3 move_z;
public GameObject gGround;
ground spt_g;
bool isJump;
bool isDush;
bool isRay;
bool isGround = false;
float jump_force = 200.0f;
float move_speed = walk_speed_max;
Animator ani;
private Transform StepHit;
private Transform Hit;
private RaycastHit ray;
const float ray_range = 0.3f;//レイを飛ばす距離
// 昇れる段差
private float stepOffset = 0.3f;
// 昇れる角度
private float slopeLimit = 65f;
// 昇れる段差の位置から飛ばすレイの距離
private float slopeDistance = 0.1f;
private Vector3 v;
// GameObject go = GameObject.Find("GameObject");
private Transform prev;
void move_step()
{ //y軸は逆にしてるプラスが下
Debug.DrawLine(transform.position + new Vector3(0f, stepOffset, 0f),
transform.position + new Vector3(0f, stepOffset, 0f) + transform.forward * slopeDistance
, Color.green);
// Debug.Log(transform.forward * slopeDistance);
float s = Mathf.Sqrt((move.x * move.x) + (move.z * move.z));
if (s > 0)
{
if (Physics.Linecast(StepHit.position, StepHit.position + StepHit.forward * ray_range, out ray))
{
if (Vector3.Angle(transform.up, ray.normal) <= slopeLimit)
{
v = new Vector3(0f, (Quaternion.FromToRotation(Vector3.up, ray.normal) * transform.forward * s).y, 0f)
+ transform.forward * s;
move.y = v.y;
isRay = true;
Debug.Log("レイ");
}
else
{
isRay = false;
// move.y = 0;
//move += transform.forward * move_speed;
}
}
}
}
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
// spt_g = gGround.GetComponent<ground>();
ani = GetComponent<Animator>();
isJump = false;
isDush = false;
StepHit = transform.Find("StepRay");
}
// Update is called once per frame
void Update()
{
//input_h = Input.GetAxis("Horizontal");
//input_v = Input.GetAxis("Vertical");
if(Input.GetKey(KeyCode.LeftArrow))
{
input_h = -1;
}else if (Input.GetKey(KeyCode.RightArrow))
{
input_h = 1;
}
else
{
input_h = 0;
}
if (Input.GetKey(KeyCode.UpArrow))
{
input_v = 1;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
input_v = -1;
}
else
{
input_v = 0;
}
move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v;
move_x = Camera.main.transform.right * input_h;
move = move_x + move_z;
move.x *= move_speed;
move.z *= move_speed;
Debug.Log(isRay);
if (move != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(move.normalized);
}
/*走る*/
if(Input.GetKey(KeyCode.RightControl))
{
isDush = true;
move_speed = 15.0f;
}
else
{
isDush = false;
move_speed = walk_speed_max;
}
if(Input.GetKey(KeyCode.Space))
{
if(isGround == true && isJump == false)
{
isJump = true;
}
}
move_step();
Animation_Mng();
}
/*アニメーション管理*/
void Animation_Mng()
{
float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z));
ani.SetFloat("Move_Speed",speed);
ani.SetBool("isDush",isDush);
//ani.speed = 2;
}
private void FixedUpdate()
{
// rb.velocity = new Vector3(move.x, rb.velocity.y, move.z);
if(isJump == true)
{
//rb.AddForce(rb.velocity.x,jump_force,rb.velocity.z);
isJump = false;
}
/*坂道判定のrayが衝突処理*/
if (isRay == false)
{
rb.velocity = new Vector3(move.x, rb.velocity.y, move.z);
rb.AddForce(new Vector3(0, -10.0f, 0));
}
else
{
//rb.AddForce(new Vector3(0, -10.0f, 0));
if(move.y == 0)
{
rb.velocity = new Vector3(move.x, -100, move.z);
}
rb.velocity = new Vector3(move.x, move.y, move.z);
}
}
private void OnCollisionEnter(Collision c)
{
//isGround = true;
// Debug.Log("プレイヤーcollision");
}
private void OnCollisionExit(Collision c)
{
//isGround = false;
}
}