Question by
jaraujo_unity · May 04, 2021 at 05:07 PM ·
c#rigidbodyplayercontroller
Rigidbody velocity and AddForce overrides,Rigidbody.velocity overrides the Force
Hi, 'im doing a movment of a player with rigidbody.velocity. To the grabity i'm using AddForce. It works perfectly exept when i'm pressing any movment key ( W A S D) direction a wall. When i press the key on a wall the gravity i think is overrieded and it stop falling.
On this first image i hold the D to the wall and it never falls On the second one i holded the D and it does what has to.
public class PlayerMovement : MonoBehaviour { CharacterStats m_ChStats; [HideInInspector]public Rigidbody m_Rigidbody; Animator m_Animator; CapsuleCollider m_capsule; GameObject m_StartPlayerPosition; GameManager m_manager;
//[SerializeField] float m_TurnSmoothTime = 0.1f;
public float v;
public float h;
float m_MovingTurnSpeed;
float m_StationaryTurnSpeed = 180;
float m_GroundCheckDistance = 0.1f;//distance to floor
float m_GravityIncreasingTimer;//distance to floor
[SerializeField] bool m_IsGrounded;
[SerializeField] bool m_Jump;
public bool m_Hooking;
public bool m_CanMove = true;
Vector3 m_Move;
Vector3 m_GroundNormal;
[Range(1f, 4f)] [SerializeField] float m_GravityMultiplier = 2f;
Vector3 extraGravityForce;
float m_TurnAmount;
float m_ForwardAmount;
void Start()
{
m_manager = FindObjectOfType<GameManager>();
m_Animator = GetComponent<Animator>();
m_capsule = GetComponent<CapsuleCollider>();
m_ChStats = GetComponent<CharacterStats>();
m_Rigidbody = GetComponent<Rigidbody>();
m_StartPlayerPosition = GameObject.Find("StartPlayerPosition");
m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
m_GroundCheckDistance = m_capsule.height / 2 + 0.2f;
m_CanMove = true;
}
// Update is called once per frame
void Update()
{
if (!m_Jump)
{
m_Jump = Input.GetKeyDown(m_ChStats.m_JumpKey);
if (m_IsGrounded)
{
OnFloor(m_Jump);
}
}
m_GravityIncreasingTimer += Time.deltaTime;
}
private void FixedUpdate()
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
if (m_CanMove == true)
{
m_Move = v * Vector3.forward + h * Vector3.right;
if (Input.GetKey(m_ChStats.m_SprintKey))
{
m_Move *= 0.5f;//Sprint
}
if (m_Move.magnitude >= 0.1f)
{
Move(m_Move * m_ChStats.m_Speed * Time.deltaTime, m_Jump, m_ChStats.m_Speed);
}
else
{
Move(m_Move, m_Jump, 0);
}
}
// m_Jump = false;
}
public void Move(Vector3 move, bool jump, float speed)
{
if (move.magnitude > 1f)
{
move.Normalize();
}
move = transform.InverseTransformDirection(move);
CheckGround();
move = Vector3.ProjectOnPlane(move, m_GroundNormal);
m_TurnAmount = Mathf.Atan2(move.x, move.z);
m_ForwardAmount = move.z;
Rotation();
if(m_Hooking == false)
{
if (m_IsGrounded && !m_Hooking)
{
OnFloor(jump);
}
else
{
OnAir();
}
m_Rigidbody.velocity = new Vector3(0f, m_Rigidbody.velocity.y, 0f) + transform.forward * speed;
}
m_Jump = false;
}
private void CheckGround()
{
RaycastHit hitInfo;
Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance ));
if (Physics.Raycast(transform.position+(Vector3.up *0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance))
{
m_GroundNormal = hitInfo.normal;
m_IsGrounded = true;
}
else
{
m_IsGrounded = false;
m_GroundNormal = Vector3.up;
}
}
public void OnAir()
{
extraGravityForce = (Physics.gravity * m_GravityMultiplier ) - Physics.gravity;
m_Rigidbody.AddForce(extraGravityForce);
}
public void OnFloor(bool jump)
{
if (jump)
{
m_Rigidbody.velocity += Vector3.up*m_ChStats.m_JumpPower;
m_IsGrounded = false;
}
}
void Rotation()
{
float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Deadzone"))
{
m_manager.RestartGame();
}
}
public void RestartGame()
{
transform.position = m_StartPlayerPosition.transform.position;
transform.rotation = m_StartPlayerPosition.transform.rotation;
m_CanMove = true;
}
}
Comment