RAGHAVENDRA DUBEY
RAGHAVENDRA DUBEY

Reputation: 11

not able to convert from 'UnityEngine.Touch' to 'float' in line 18

I am not able to convert from 'UnityEngine.Touch' to 'float' in line no.18 Please help?

GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * movementSpeed;

public class RacketPlayer1 : MonoBehaviour {

    public float movementSpeed;

    private void FixedUpdate(){

        if(Input.touchCount>0)
        {
            UnityEngine.Touch v = Input.touches[0];

            if(v.phase == TouchPhase.Stationary || v.phase == TouchPhase.Moved )
            {           
                GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * movementSpeed;
            }   
        } 
    }
}

Upvotes: 0

Views: 90

Answers (1)

TimChang
TimChang

Reputation: 2417

Because vector2 => (float x,float y) , I guess you want to set position so

 GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * movementSpeed;

change to :

GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v.position.y) * movementSpeed;

https://docs.unity3d.com/ScriptReference/Touch-position.html

Upvotes: 2

Related Questions