R
R
Rodrigez3212021-05-21 19:31:21
Unity
Rodrigez321, 2021-05-21 19:31:21

I am writing a script for the enemy in Unity, trying to implement the repulsion of the enemy when receiving damage, I ran into a problem who will help?

On the penultimate line, Unity writes that the "-" operator for operand types "Vector2" and "Vector3" is ambiguous.
What does this mean and can you help solve this problem?

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

public class Ememy : MonoBehaviour
{
    [SerializeField]
    int lives;

    [SerializeField]
    float pushPower;

    private Rigidbody2D rb;


    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "AttackHitBox")
        {
            lives--;
            PushAway(transform.position, 50f);
        }
    }

    public void PushAway(Vector2 pushFrom, float pushPowe)
    {
        if (rb == null || pushPower == 0)
        {
            return;
          
        }
        var pushDirection = (pushFrom - transform.position).Normalize();
        rb.AddForce(pushDirection * pushPower);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2021-05-21
@ShockWave2048

public void PushAway(Vector3 pushFrom, float pushPower)

no need to cast the reference to another type (Vector3 -> Vector2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question