У меня есть лестница, которая есть, например, x = 20, и у меня есть игрок, который бежит или перемещается в лестницу, тогда, когда столкновение происходит, когда игрок встречает лестницу, я хочу, чтобы игрок привязал его координату x в местоположение координат x, а не координату Collision x. /> Playermocement: < /p>
Vector3 viewportPoint;
Vector3 ladderX;
private void FixedUpdate()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, verticalMove * Time.fixedDeltaTime, false, jump, isHorizontalMove, ladderX);
jump = false;
//isLadder = false;
}
void Update()
{
//Debug.Log("isLadder:" + isLadder);
if (Input.GetButtonDown("Up") && isLadder)
{
//Debug.Log("Up");
isHorizontalMove = false;
animator.SetBool("Climb", true);
rigidbody2D.gravityScale = 0;
ladderX = viewportPoint;
}
}
void OnTriggerEnter2D(Collider2D collision)
{
//Debug.Log("Object that collided with me: " + collision.gameObject.tag);
if (collision.gameObject.tag == "Ladder" || collision.gameObject.tag == "Ladder_Bottom")
{
Vector3 pos = collision.gameObject.transform.position;
viewportPoint = Camera.main.WorldToViewportPoint(pos);
Debug.Log("gameObject pos: " + viewportPoint);
isLadder = true;
}
}
< /code>
carmationController: < /p>
public void Move(float moveH, float moveV, bool crouch, bool jump, bool isHorizonal, Vector3 ladderX)
{
// If crouching, check to see if the character can stand up
if (!crouch)
{
// If the character has a ceiling preventing them from standing up, keep them crouching
if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
{
crouch = true;
}
}
//only control the player if grounded or airControl is turned on
if (m_Grounded || m_AirControl)
{
// If crouching
if (crouch)
{
if (!m_wasCrouching)
{
m_wasCrouching = true;
OnCrouchEvent.Invoke(true);
}
// Reduce the speed by the crouchSpeed multiplier
moveH *= m_CrouchSpeed;
// Disable one of the colliders when crouching
if (m_CrouchDisableCollider != null)
m_CrouchDisableCollider.enabled = false;
} else
{
// Enable the collider when not crouching
if (m_CrouchDisableCollider != null)
m_CrouchDisableCollider.enabled = true;
if (m_wasCrouching)
{
m_wasCrouching = false;
OnCrouchEvent.Invoke(false);
}
}
// Move the character by finding the target velocity
if (isHorizonal)
{
targetVelocity = new Vector2(moveH * 10f, m_Rigidbody2D.linearVelocity.y);
// If the input is moving the player right and the player is facing left...
if (moveH > 0 && !m_FacingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if (moveH < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
}
else
{
//Debug.Log(ladderPositionX + " : " + moveV * 5f);
targetVelocity = new Vector2(ladderX.x, moveV * 5f);
}
// And then smoothing it out and applying it to the character
m_Rigidbody2D.linearVelocity = Vector3.SmoothDamp(m_Rigidbody2D.linearVelocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
}
// If the player should jump...
if (m_Grounded && jump)
{
// Add a vertical force to the player.
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... s-a-ladder
Как сделать Snap Player X координировать с x value игрового объекта, такого как лестница ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Есть ли обратная сторона замены all Remember(value) на Remember {deriveStateOf {value}}?
Anonymous » » в форуме Android - 0 Ответы
- 46 Просмотры
-
Последнее сообщение Anonymous
-