snapping the Players psosition to another GameObject for a "Ledge grab"
Hi! Another day another coding problem. I use this PlayerMovement script from a tutorial and try to mod it to my liking. I added a WallJump now I tried to implement a LedgeGrab but everything I saw online with this tag didn't help me. I have ledge recognition up and running. Now I want the player to snap to a certain position of the lede when the recognition triggers. There are several GameObjects with the Layer: "Ledge" and I want it to snap to the one that triggered the recognition (m_Ledged). Somehow everything I tried failed.
This is the most part of the script. The ledge recognition ist more at the top of it while the snapping will be near the End.
public class CharacterController2D : MonoBehaviour {
[SerializeField] private LayerMask m_WhatIsLedge; [SerializeField] private Transform m_LedgeGrabCheck; [SerializeField] private Transform Player;
const float k_LedgeGrabRadius = .2f;
private bool m_Ledged;
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true; // For determining which way the player is currently facing.
private Vector3 m_Velocity = Vector3.zero;
public UnityEvent OnLandEvent;
[System.Serializable]
public class BoolEvent : UnityEvent<bool> { }
public BoolEvent OnCrouchEvent;
private bool m_wasCrouching = false;
private void Awake()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
if (OnLandEvent == null)
OnLandEvent = new UnityEvent();
if (OnCrouchEvent == null)
OnCrouchEvent = new BoolEvent();
}
private void FixedUpdate()
{
bool wasLedged = m_Ledged;
m_Ledged = false;
Collider2D[] collidersLedge = Physics2D.OverlapCircleAll(m_LedgeGrabCheck.position, k_LedgeGrabRadius, m_WhatIsLedge);
for (int i = 0; i < collidersLedge.Length; i++)
{
if (collidersLedge[i].gameObject != gameObject && !m_Grounded)
{
m_Ledged = true;
Debug.Log("Ledged");
}
}
}
public void Move(float move, bool crouch, bool jump)
{
if (m_Ledged)
{
//--------- snap to the position of the triggering ledge + offset until m_Ledged = false---------
}
}
I tried to throw out anything of the script that isn't realted to the problem
Your answer
Follow this Question
Related Questions
2D Unity Instantiate Ragdoll, same Transform as Sprites ( Head, Torso, LArm,RArm,LLeg,RLeg) 2 Answers
How can I snap rooms in game view? 0 Answers
Why is my gameObject in a different position on galaxy s4? 1 Answer
Problem with a rendering of a 2D sprite 1 Answer
Why does my object move to 0 when I play an animation? 0 Answers