Relocate the object from the current position to another 2D



  • The essence is: I've got a violin that, by the pressure of the UI button, reproduces the antenna of the character but only where it is. I started writing a code to give him a speed and direction to push the same button, but I didn't know how to do it properly. You need your help, you tried to use herrp but you didn't.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    

    public class Attack1 : MonoBehaviour
    {
    private Animator anim;

    public void Attack() // во время этой анимации персонаж должен переместиться с текущей его позиции 
    {
        anim = GetComponent<Animator>();
        anim.SetTrigger("Attack"); 
    }
    

    }



  • If you need to move 3d, you can use Lerp through Button.onClick. And in the case of 2d also through Button.onClick but Rigidbody2d either velocity or AddForce.

    You make a scrupt, you add a vein to the button, you throw a violin. введите сюда описание изображения

    This is Lerp:

        [SerializeField] private float speed = 10f;
    
    void Update()
    {
       Vector3.Lerp(Transform.position("наша позиция"),"Укажи координаты куда переместится здесь",(speed*Time.deltaTime))
       
    }
    

    It should work if you need 2d, it's like:

         [SerializeField] private float Speed = 100f;
    private Rigidbody2D body;

    private void Awake()
    {
        body = GetComponent<Rigidbody2D>();
    
    }
    
    void Update()
    {
         
         body.AddForce(new Vector2(speed, body.velocity.y));
         //или body.velocity = new Vector2(speed, body.velocity.y);
       
    }
    

    It should work.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2