I can’t figure out how to add a constant velocity vector, a character should, like, magnetize to a wall or ceiling, add velocity to the ceiling so that if you touch the ceiling, the vector speed up, the character drops down, put more force than gravity does not help

[SerializeField] private float m_MaxSpeed = 10f; //speed x axis [SerializeField] private float m_MaxSpeedup = 6f; [SerializeField] private float m_JumpForce = 400f; [Range(0, 1)] [SerializeField] private float m_PotolokSpeed = .5f; [SerializeField] private bool m_AirControl = false; [SerializeField] private LayerMask m_WhatIsGround; [SerializeField] private LayerMask m_whatiswall; [SerializeField] private LayerMask m_whatispotolok; private Transform m_GroundCheck; const float k_GroundedRadius = .4f; const float k_wallradius = .6f; private bool m_Grounded; private bool m_walled; private bool m_potolok; private Vector2 _speed; //private bool m_potolok; private Transform m_PotolokCheck; private Transform m_wallingcheck; const float k_PotolokRadius = .4f; private Animator m_Anim; private Rigidbody2D m_Rigidbody2D; private bool m_FacingRight = true; void Update() { if (m_Grounded && Input.GetKeyDown(KeyCode.Space)) { m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); //jump on ground } if (m_walled && !m_Grounded && Input.GetKeyDown(KeyCode.Space)) // jump on wall { m_Rigidbody2D.velocity =new Vector2(6f, 6f); } if (m_potolok && Input.GetKeyDown(KeyCode.Z)) // { m_Rigidbody2D.AddForce(new Vector2(0f,3)); } } private void Awake() { // Setting up references. m_GroundCheck = transform.Find("GroundCheck"); m_PotolokCheck = transform.Find("PotolokCheck"); m_wallingcheck = transform.Find("WallingCheck"); m_Anim = GetComponent<Animator>(); m_Rigidbody2D = GetComponent<Rigidbody2D>(); } private void FixedUpdate() { { m_Grounded = false; Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject != gameObject) m_Grounded = true; } m_Anim.SetBool("Ground", m_Grounded); //vertical animation m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y); } { m_walled = false; Collider2D[] colliders = Physics2D.OverlapCircleAll(m_wallingcheck.position, k_wallradius, m_whatiswall); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject != gameObject) m_walled = true; } m_Anim.SetBool("Wall", m_walled); } { m_potolok = false; Collider2D[] colliders = Physics2D.OverlapCircleAll(m_PotolokCheck.position, k_PotolokRadius, m_whatispotolok); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject != gameObject) m_potolok = true; } m_Anim.SetBool("Potolok", m_potolok); } } public void Move(float move, bool potolok, bool jump, bool jumpAx)// { // if (!potolok && m_Anim.GetBool("Potolochnik")) { // анимация при остановке на потолке if (Physics2D.OverlapCircle(m_PotolokCheck.position, k_PotolokRadius, m_WhatIsGround)) { potolok = true; } } m_Anim.SetBool("Potolochnik", potolok); if (m_Grounded || m_AirControl) { move = (potolok ? move * m_PotolokSpeed : move); m_Anim.SetFloat("Speed", Mathf.Abs(move)); m_Rigidbody2D.velocity = new Vector2(move * m_MaxSpeed, m_Rigidbody2D.velocity.y); if (move > 0 && !m_FacingRight) { Flip(); } else if (move < 0 && m_FacingRight) { Flip(); } } if (m_Grounded && jump && m_Anim.GetBool("Ground")) { m_Grounded = false; m_Anim.SetBool("Ground", false); m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); } } private void Flip() { m_FacingRight = !m_FacingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } } 

}

  • Connect with a debugger and make sure that your conditions are met (your code is replete with magic constants - well, what can go wrong?;)). If everything is good, the object is updated, the forces are applied, then reduce your example to a minimum. Just an object that stretches to the object, without any checks on the type and button presses. Well, then using the method of half division, reveal the problem code. Successes. - Lunar Whisper

1 answer 1

It must be something like ...

 private bool _wallIsTouched; Function FixedUpdate() { if(_wallIsTouched) rigidbody.AddForce(VectorToWall * Physics.gravity * rigidbody.mass) } 

This code is written almost from Ponte) But the direction is exactly that.

  • You need to use ficsed update so that your acceleration does not skip when braking graphics,
  • need to use a rigidbody mass
  • and the level of gravity of the whole system so that the physics would not look adequately on the floor strange on the walls / ceiling

but you will need to remove the code yourself, since I broke