Question by
madman_118 · Aug 27, 2015 at 02:17 PM ·
unity 5error messagemonodevelop
Help im getting the following error: error CS0121. The rest of it is in the description
Assets/Drag.cs(12,66): error CS0121: The call is ambiguous between the following methods or properties: UnityEngine.Vector3.operator -(UnityEngine.Vector3, UnityEngine.Vector3)' and
UnityEngine.Vector2.operator -(UnityEngine.Vector2, UnityEngine.Vector2)'
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider2D))]
public class Drag : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown() {
offset = gameObject.GetComponent<Rigidbody2D> ().velocity - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
GetComponent<Rigidbody2D> ().velocity = curPosition;
}
}
Comment
Best Answer
Answer by Dave-Carlile · Aug 27, 2015 at 02:30 PM
RigidBody2D.velocity is a Vector2, and ScreenToWorldPoint returns a Vector3
so you're subtracting a Vector3
from a Vector2
on line 12 in your sample code. There isn't a vector subtraction operator overload which allows that, so you get the error.
You can resolve this by typecasting the result of ScreenToWorldPoint
to a Vector2
.
offset = gameObject.GetComponent<Rigidbody2D> ().velocity -
(Vector2)Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
So now how do i make it so when you drag on it it moves towards the mouse ins$$anonymous$$d of flying away