简体   繁体   中英

Errors while changing unity3d movement to drag (c#)

My errors are

  • 'UnityEngine.Collider2D' is a 'type' but is used like a 'variable'
  • The best overloaded method match for 'UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments
  • Argument '1': cannot convert from 'double' to 'float'

     using UnityEngine; using System.Collections; public class MoveRacket : MonoBehaviour { public float speed = 30; public string axis = "Vertical"; public object racket = "Racket"; public bool touchInput = true; public Vector2 touchPos; void FixedUpdate () { //used to not have anything in parentheses //float v = Input.GetAxisRaw (axis); float v = Input.GetAxisRaw (axis); GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed; if (Input.touchCount == 1) { Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); Vector2 touchPos = new Vector2(wp.x, wp.y); if (Touch = Physics2D.OverlapPoint(touchPos)) { this.transform.position = new Vector3 (3.94,wp.y,0); } } } } 

You have 2 issues here. The first being you are trying to assign the Collider2D result of Physics2D.OverlapPoint(touchPos) to the unity struct type of Touch. Not really sure what your intentions are here. If you just want to test if the touchPos overlaps, then use this code:

public class MoveRacket : MonoBehaviour {
public float speed = 30;
public string axis = "Vertical";
public object racket = "Racket";
public bool touchInput = true;
public Vector2 touchPos;





void FixedUpdate () {

    //used to not have anything in parentheses
    //float v = Input.GetAxisRaw (axis);
    float v = Input.GetAxisRaw (axis);
    GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
    if (Input.touchCount == 1)
    {
        Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
        Vector2 touchPos = new Vector2(wp.x, wp.y);
        if (Physics2D.OverlapPoint(touchPos) != null)
        {
            this.transform.position =  new Vector3 (3.94f,wp.y,0);

    }
    }
}
}

Your other issue was that the value of 3.94 is being treated as a double data type, when the vector3 constructor expects a float. To interpret it as a float, add an "f" to the end of the number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM