I went through a drag and shoot guide to the phone game I make,
The Touch looks like it’s stuck in the middle of the screen. When I tried to click on the screen I got an error saying that I have problems on lines 39, 47, 57 and all of these lines have the code:
vector3 “name” = cam.ScreenToWorldPoint (touch.position);
Here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControll : MonoBehaviour
{
public float power = 10f;
public float maxdrag = 5f;
public Rigidbody2D rb;
public LineRenderer lr;
public Camera cam;
Vector3 dragStartPos;
Touch touch;
private void Update()
{
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) {
DragStart();
}
if (touch.phase == TouchPhase.Moved) {
Dragging();
}
if (touch.phase == TouchPhase.Ended) {
DragRelease();
}
}
}
void DragStart()
{
dragStartPos = cam.ScreenToWorldPoint(touch.position);
dragStartPos.z = 0f;
lr.positionCount = 1;
lr.SetPosition(0, dragStartPos);
}
void Dragging()
{
Vector3 dragginPos = cam.ScreenToWorldPoint(touch.position);
dragginPos.z = 0f;
lr.positionCount = 2;
lr.SetPosition(1, dragginPos);
}
void DragRelease()
{
lr.positionCount = 0;
Vector3 dragReleasePos = cam.ScreenToWorldPoint(touch.position);
dragReleasePos.z = 0f;
Vector3 force = dragStartPos - dragReleasePos;
Vector3 clampedForce = Vector3.ClampMagnitude(force, maxdrag) * power;
rb.AddForce(clampedForce, ForceMode2D.Impulse);
}
}
What am I doing wrong?