Move player along Y pos - c#

I've made the move from SpriteKit and Swift to Unity recently, and I've started an Android project. What I want is to move the player up and down so along the Y position only. In xcode I would have simply done this:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let action = SKAction.moveToY(location.y, duration: 0.7)
action.timingMode = .EaseInEaseOut
player.runAction(action)
}
}
Would this sort of code translate to C# in the same sort of way? Or does it work differently.
** UPDATE **
This is the code I tried with one of the answers below, but the player is disappearing rather than moving, any suggestions? What I am after is for the player to move to where the screen is tapped, but only on the Y axis. Thanks :)
Vector2 touchPosition;
[SerializeField] float speed = 1f;
void Update() {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// assign new position to where finger was pressed
transform.position = new Vector3 (transform.position.x, Input.GetTouch(i).position.y, transform.position.z);
}
}
}

You can move any object in your scene by calling transform.Translate() from a script on that object, or by getting a reference to another object's transform and calling Translate() on that.
Move current script's scene object based on finger movement
Vector2 touchDeltaPosition;
[SerializeField] float speed = 1f;
void Update() {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Moved) {
// get new finger position
touchDeltaPosition = Input.GetTouch(i).deltaPosition;
// move scene object along y axis
transform.Translate(0f, -touchDeltaPosition.y * speed, 0f);
}
}
}
Move current script's scene object to finger press location
void Update() {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// assign new position to where finger was pressed
transform.position = new Vector3 (transform.position.x, Input.GetTouch(i).position.y, transform.position.z);
}
}
}

public float speed = 5.0f;
public void Update()
{
Vector3 moveDelta = new Vector3(0f, Input.GetAxis("Vertical"), 0f);
transform.Translate(moveDelta * speed * Time.deltatime)
}

For anyone who needs to know, this is how it worked for me:
public GameObject thingToMove;
public float smooth = 2;
private Vector3 _endPosition;
private Vector3 _startPosition;
private void Awake()
{
_startPosition = thingToMove.transform.position;
}
private void Update()
{
if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
_endPosition = HandleTouchInput();
}
else
{
_endPosition = HandleMouseInput();
}
thingToMove.transform.position = Vector3.Lerp(thingToMove.transform.position, new Vector3(transform.position.x, _endPosition.y, 0), Time.deltaTime * smooth);
}
private Vector3 HandleTouchInput()
{
for (var i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
var screenPosition = Input.GetTouch(i).position;
_startPosition = Camera.main.ScreenToWorldPoint(screenPosition);
}
}
return _startPosition;
}
private Vector3 HandleMouseInput()
{
if(Input.GetMouseButtonDown(0))
{
var screenPosition = Input.mousePosition;
_startPosition = Camera.main.ScreenToWorldPoint(screenPosition);
}
return _startPosition;
}
This moves the object to touch location smoothly using .Lerp. More information:
http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

Related

How to implement a Raycast interaction system in a 2d top level style game?

I've been trying to learn how to use Raycast2D for days and I'm not able to, I want my raycast to detect when it has collided with an NPC to make some kind of interaction system, but as much as I try the maximum progress I've managed to make is that the raycast works in the direction it should but not the distance, it always reports on screen that it has found the limit map collider, even if I rotate on myself to detect any other collider, first of all thank you very much for reading and for your effort! <3
private void Update() {
movimiento();
if (posesion) {
Debug.Log("ENCONTRADO");
}
hit = Physics2D.Raycast(transform.position, transform.position + direccion2 * 500, 0);
Debug.DrawRay(transform.position, transform.position + direccion2 * 500, Color.red);
if (true) {
Debug.Log(hit.transform.name);
}
if (hit.transform.tag == "NPC" && !posesion) {
Debug.Log("Estamos dentro");
if (Input.GetButtonDown("Fire2")) {
timer = Time.time;
}
if (Input.GetButtonUp("Fire2")) {
timerB = Time.time;
if (timerB - timer >= 3) {
timer = 0;
timerB = 0;
hit.transform.gameObject.GetComponent < NPC > ().turnPosession();
startPossesion();
}
}
}
}
The firsts lines of the code
[SerializeField]
private float velocidadMovimiento;
[SerializeField]
private Vector2 direccion;
private Vector3 direccion2;
private Rigidbody2D rb2D;
private float movimientoX;
private float movimientoY;
private Animator animator;
private bool posesion;
private float timer, timerB;
private RaycastHit2D hit;
private void Start() {
animator = GetComponent < Animator > ();
rb2D = GetComponent < Rigidbody2D > ();
posesion = false;
timer = 0;
timerB = 0;
}
private void movimiento() {
movimientoY = Input.GetAxisRaw("Horizontal");
movimientoX = Input.GetAxisRaw("Vertical");
animator.SetFloat("MovimientoX", movimientoX);
animator.SetFloat("MovimientoY", movimientoY);
direccion = new Vector2(
Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical")
).normalized;
if (movimientoX != 0 || movimientoY != 0) {
direccion2 = new Vector3(
Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical"), 0
).normalized;
animator.SetFloat("UltimoX", movimientoX);
animator.SetFloat("UltimoY", movimientoY);
}
}
This part is wrong:
hit = Physics2D.Raycast(transform.position, transform.position + direccion2 * 500, 0);
It should be:
hit = Physics2D.Raycast(transform.position, direccion2, 500);

How to rotate object towards target smooth slowly ? The object is not rotating facing the target

The goal in the end is to rotate the object when he start moving so the object will facing towards the target he is moving to and also that the laser that coming out from the object eye will be facing the same target.
The object that i want to rotate is at first at position 0,0,0 because the object is a child. the player is holding the object by hand. Then the object is getting throw with animation towards a target.
The object is moving to the target but now i want the object to rotate also smooth slowly to the target in this case i set the rotation duration 1 second. and i see that the object rotation is changing but not facing the target.
This screenshot show the object while it's still in the player hand at position 0,0,0
Then the object get throw and start moving to the target at this time the object rotation change :
The object that throw have a child transform the eye and from the eye a laser beam should be coming out and then i want to make that the object will rotate towards the target so the laser from the eye will be facing the target.
I marked the eye with a small red circle :
In this screenshot i marked the target far away, the laser in green is not facing the target and also the eye is not facing the target. the laser should come out the eye and the whole object should facing the target so the laser also should be facing the target with the eye.
This screenshot is of the object before running the game the eye is facing forwards with the object. this is just to show that the eye is aligned with the object facing forward.
This script is attached to my player and make the object to be thrown to the target and also should make the object to be rotating towards the target :
The throw part that make the object move to the target is working fine but the rotating part is not :
private IEnumerator AnimateRotationTowards(Quaternion transformRot, Quaternion targetRot, float dur)
{
float t = 0f;
while (t < dur)
{
transformRot = Quaternion.Slerp(transformRot, targetRot, t / dur);
yield return null;
t += Time.deltaTime;
}
transformRot = targetRot;
}
Also i see that making transformRot = targetRot; for some reason there is a small message say unnecessary assignment not sure why.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThrowObject : MonoBehaviour
{
public Transform objectToThrow;
public Transform target;
public Transform objectToThrowParent;
public float throwingSpeed;
public float waitAtTargetTime;
public float distanceToStopFromTarget;
public bool go = false;
public AnimationCurve animationCurve;
private Animator anim;
private bool startThrowAnimationOnce = true;
private bool reParent = false;
private bool startMovingBack = false;
private void Start()
{
anim = GetComponent<Animator>();
}
private void Update()
{
if (anim != null && startThrowAnimationOnce)
{
anim.SetTrigger("Throw");
startThrowAnimationOnce = false;
}
if (go)
{
objectToThrow.parent = null;
StartCoroutine(Throw());
StartCoroutine(AnimateRotationTowards(objectToThrow.rotation, target.rotation, 1f));
go = false;
}
if (reParent)
{
objectToThrow.position = objectToThrowParent.position;
}
if (startMovingBack)
{
if (Vector3.Distance(objectToThrow.position, objectToThrowParent.position) >= 0.1f)
{
float step = 5 * Time.deltaTime;
objectToThrow.position = Vector3.MoveTowards(objectToThrow.position, objectToThrowParent.position, step);
}
else
{
objectToThrow.position = objectToThrowParent.position;
objectToThrow.parent = objectToThrowParent;
objectToThrow.localPosition = new Vector3(0, 0, 0);
}
}
}
public void ThrowEvent()
{
go = true;
}
IEnumerator Throw()
{
while(Vector3.Distance(objectToThrow.position, target.position) >= distanceToStopFromTarget)
{
objectToThrow.position = Vector3.MoveTowards(
objectToThrow.position,
target.position,
throwingSpeed * Time.deltaTime
);
yield return null;
}
yield return new WaitForSeconds(waitAtTargetTime);
startMovingBack = true;
}
private IEnumerator AnimateRotationTowards(Quaternion transformRot, Quaternion targetRot, float dur)
{
float t = 0f;
while (t < dur)
{
transformRot = Quaternion.Slerp(transformRot, targetRot, t / dur);
yield return null;
t += Time.deltaTime;
}
transformRot = targetRot;
}
}
This script is attached to the object eye and fire the laser :
The target in this script is the same target as in the ThrowObject script :
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System;
using UnityEngine;
public class Hovl_DemoLasers : MonoBehaviour
{
public List<Transform> targets;
public GameObject FirePoint;
public Camera Cam;
public float MaxLength;
public GameObject[] Prefabs;
private Ray RayMouse;
private Vector3 direction;
private Quaternion rotation;
[Header("GUI")]
private float windowDpi;
private int Prefab;
private GameObject Instance;
private Hovl_Laser LaserScript;
private Hovl_Laser2 LaserScript2;
private bool rotateMouse = true;
private bool startLaser = true;
private float buttonSaver = 0f;
private Hovl_LaserDemo hovl_laserDemo;
private float maxDistance = 0;
private int closestIndex = 0;
void Start()
{
if (Screen.dpi < 1) windowDpi = 1;
if (Screen.dpi < 200) windowDpi = 1;
else windowDpi = Screen.dpi / 200f;
Counter(0);
}
void Update()
{
//Enable lazer
if (Input.GetMouseButtonDown(0))
{
Destroy(Instance);
Instance = Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
Instance.transform.parent = transform;
LaserScript = Instance.GetComponent<Hovl_Laser>();
LaserScript2 = Instance.GetComponent<Hovl_Laser2>();
rotateMouse = true;
}
if (Input.GetMouseButtonDown(1))
{
rotateMouse = false;
}
if ((Input.GetKey(KeyCode.A) || Input.GetAxis("Horizontal") < 0) && buttonSaver >= 0.4f)// left button
{
buttonSaver = 0f;
Counter(-1);
}
if ((Input.GetKey(KeyCode.D) || Input.GetAxis("Horizontal") > 0) && buttonSaver >= 0.4f)// right button
{
buttonSaver = 0f;
Counter(+1);
}
buttonSaver += Time.deltaTime;
if (startLaser)
{
rotateMouse = false;
Destroy(Instance);
Instance = Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
Instance.transform.parent = transform;
LaserScript = Instance.GetComponent<Hovl_Laser>();
LaserScript2 = Instance.GetComponent<Hovl_Laser2>();
hovl_laserDemo = Instance.GetComponent<Hovl_LaserDemo>();
startLaser = false;
}
if (targets != null)
{
maxDistance = Mathf.Infinity;
for (int i = 0; i < targets.Count; i++)
{
float distance = Vector3.Distance(gameObject.transform.position, targets[i].position);
if (distance < maxDistance)
{
maxDistance = distance;
closestIndex = i;
}
}
if (hovl_laserDemo != null)
{
float distance = Vector3.Distance(gameObject.transform.position, targets[closestIndex].position);
MaxLength = distance;
hovl_laserDemo.MaxLength = distance;
}
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, MaxLength))
{
RotateToMouseDirection(gameObject, targets[closestIndex].position);
}
else
{
RotateToMouseDirection(gameObject, targets[closestIndex].position);
}
}
if (Cam != null && rotateMouse)
{
RaycastHit hit;
var mousePos = Input.mousePosition;
RayMouse = Cam.ScreenPointToRay(mousePos);
if (Physics.Raycast(RayMouse.origin, RayMouse.direction, out hit, MaxLength))
{
RotateToMouseDirection(gameObject, hit.point);
}
else
{
var pos = RayMouse.GetPoint(MaxLength);
RotateToMouseDirection(gameObject, pos);
}
}
else
{
Debug.Log("No camera");
}
}
void OnGUI()
{
GUI.Label(new Rect(10 * windowDpi, 5 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use the keyboard buttons A/<- and D/-> to change lazers!");
GUI.Label(new Rect(10 * windowDpi, 20 * windowDpi, 400 * windowDpi, 20 * windowDpi), "Use left mouse button for shooting!");
}
void Counter(int count)
{
Prefab += count;
if (Prefab > Prefabs.Length - 1)
{
Prefab = 0;
}
else if (Prefab < 0)
{
Prefab = Prefabs.Length - 1;
}
}
void RotateToMouseDirection(GameObject obj, Vector3 destination)
{
direction = destination - obj.transform.position;
rotation = Quaternion.LookRotation(direction);
obj.transform.localRotation = Quaternion.Lerp(obj.transform.rotation, rotation, 1);
}
}
This is one situation where it is important to understand the difference between passing by value, or passing by reference. In your AnimateRotationTowards function, all of the parameters to that function are passed by value. Changes to those rotations will affect the local values within the function scope, but those are not the same values that get used in the scene.
What you probably want instead is a reference to some Transform object. If you assign a new rotation to that Transform, it will use that rotation in the scene.
It looks like this script component is attached to the GameObject that will be rotating. Is that right? If yes, you can set a new rotation by assigning a value to transform.rotation:
private IEnumerator AnimateRotationTowards(Quaternion transformRot, Quaternion targetRot, float dur)
{
float t = 0f;
while (t < dur)
{
transform.rotation = Quaternion.Slerp(transformRot, targetRot, t / dur);
yield return null;
t += Time.deltaTime;
}
transform.rotation = targetRot;
}
If you need to assign a rotation to some other GameObject, you can pass in a reference to that GameObject or its Transform and use that to assign the rotation.

Can't raycast mouse click at good position on my map

I decided to develop a game of gomoku, which the principle is very simple : you have to place circle or cross in a map of tile and the goal is to align 5 items. For that i choose to use unity in 2d and i use prefab with tile image to create my map. The problem i encountered is that, in my script GameManager, when i raycast my mouse click position, the click position is not the same as my tile of my board which i generated so i can't place item on my map.
BoardGenerator.cs
public float startX;
public float startY;
public Transform tiles;
public int BOARD_SIZE = 19;
void Start()
{
CreateBoard();
}
void Update()
{
}
void CreateBoard()
{
float tileX = tiles.GetComponent<SpriteRenderer>().bounds.size.x;
float tileY = tiles.GetComponent<SpriteRenderer>().bounds.size.y;
float yCor = Camera.main.ScreenToWorldPoint(new Vector3(0, startY, 0f)).y;
int count = 0;
for (int i = 0; i < BOARD_SIZE; i++)
{
float xCor = Camera.main.ScreenToWorldPoint(new Vector3(startX, 0, 0f)).x;
for (int j = 0; j < BOARD_SIZE; j++)
{
Vector3 tilesPos = new Vector3(xCor, yCor, 0f);
Transform tile = Instantiate(tiles, tilesPos, Quaternion.identity);
tile.gameObject.name = "Tile" + count;
count++;
xCor += tileX;
}
yCor -= tileY;
}
}
GameManager.cs
public int firstPlayer;
private PlayerTurn playerTurn;
public Sprite xSprite;
public Sprite oSprite;
public enum PlayerTurn
{
PLAYER1,
PLAYER2,
BOT
}
void Start()
{
if (firstPlayer == 1)
playerTurn = PlayerTurn.PLAYER1;
else
playerTurn = PlayerTurn.PLAYER2;
}
void Update()
{
Play();
}
void Play()
{
if (Input.GetMouseButtonDown(0))
{
float xCor = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
float yCor = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
Vector2 origin = new Vector2(xCor, yCor);
RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.zero, 0);
Debug.Log("mouse = " + origin);
Debug.Log("first tile = " + (Vector2)GameObject.Find("Tile0").transform.position);
if (hit.collider != null && hit.transform.gameObject.tag.Equals("Tile"))
{
Debug.Log("hit");
if (this.playerTurn == PlayerTurn.PLAYER1)
{
hit.transform.gameObject.GetComponent<SpriteRenderer>().sprite = xSprite;
hit.transform.tag = "crossTile";
playerTurn = PlayerTurn.PLAYER2;
}
else if (this.playerTurn == PlayerTurn.PLAYER2)
{
hit.transform.gameObject.GetComponent<SpriteRenderer>().sprite = oSprite;
hit.transform.tag = "roundTile";
playerTurn = PlayerTurn.PLAYER1;
}
}
}
}
You can take advantage of a different overload for the Raycast in order to return the object hit, like so:
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics2D.Raycast(ray, out hit, rayDist)){
GameObject obj = hit.transform.gameObject;
// Do something with obj
}
In the above example, 'obj' will be the GameObject that was hit by the Raycast, so you can get its position by then referencing:
obj.transform.position
The reason for:
GameObject obj = hit.transform.gameObject;
is that 'hit.gameObject' will return a parent object with a Rigidbody, if there is one. This can lead to a lot of confusion down the track when working with more complex object structures, so it is good practise to grab 'hit.transform.gameObject', which will always return the GameObject that the collider the Raycast hit is attached to.

touch control to move cube (in an array that generates them) left and right

I am moving an object that consists of two cubes: left and right. These cubes are randomly generated on the y-axis upwards.
I am able to move them left and right with no problem, however, when I move one of the cubes left or right they all move.
How am I able to only move one of the cubes when touched only left or right rather than all of them? Here is my code below:
Generate cubes code:
public Transform block;
public Transform player;
private float objectSpawnedTo = 5.0f;
public static float distanceBetweenObjects = 5.0f;
private float nextCheck = 0.0f;
private ArrayList objects = new ArrayList();
void Start () {
maintenance(0.0f);
}
void Update () {
float playerX = player.position.y;
if(playerX > nextCheck)
{
maintenance(playerX);
}
}
private void maintenance(float playerX)
{
nextCheck = playerX + 30;
for (int i = objects.Count-1; i >= 0; i--)
{
Transform blck = (Transform)objects[i];
if(blck.position.y < (transform.position.y - 30))
{
Destroy(blck.gameObject);
objects.RemoveAt(i);
}
}
spawnObjects(5);
}
private void spawnObjects(int howMany)
{
float spawnX = objectSpawnedTo;
for(int i = 0; i<howMany; i++)
{
Vector3 pos = new Vector3(-3.5f,spawnX, 0);
//float firstRandom = Random.Range(-6.0f, 1.0f);
Transform blck = (Transform)Instantiate(block, pos, Quaternion.identity);
//blck.localScale+=new Vector3(firstRandom*2,0,0);
objects.Add(blck);
//pos = new Vector3(0,spawnX, 0);
//blck = (Transform)Instantiate(block, pos, Quaternion.identity);
//blck.localScale +=new Vector3((8.6f-firstRandom)*2,0,0);
//objects.Add(blck);
spawnX = spawnX + distanceBetweenObjects;
}
objectSpawnedTo = spawnX;
}
}
Cube-control code:
public float speed = 5;
// Use this for initialization
void Start () {
}
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
Vector3 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate(touchDeltaPosition.x * speed, 0, 0);
Vector3 boundaryVector = transform.position;
boundaryVector.x = Mathf.Clamp (boundaryVector.x, -5.5f, -2.8f);
transform.position = boundaryVector;
}
}
}
The problem is your cube-control code has no condition in it about which cube is selected : you simply wait for Input.touchCount to be > 0 and then move the cube. So all the cube having this script will move.
I think what you need to do is a raycast to check which cube has been "touched" and then only move it if raycast is successful :
[SerializeField]
private float speed = 0.5f;
private int MAX_TOUCH_COUNT = 5;
private bool[] touched;
protected void Start()
{
touched = new bool[MAX_TOUCH_COUNT];
}
void Update()
{
if (Input.touchCount > 0)
{
for(int i = 0; i < (Input.touchCount <= MAX_TOUCH_COUNT ? Input.touchCount : MAX_TOUCH_COUNT); i++)
{
if(touched[i] && Input.GetTouch(i).phase == TouchPhase.Ended)
{
touched[i] = false;
}
else if (!touched[i] && Input.GetTouch(i).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.transform.GetComponentInChildren<*YOUR_CUBE_CONTROL_CLASS_NAME*>() == this)
{
touched[i] = true;
}
}
}
else if (touched[i] && Input.GetTouch(i).phase == TouchPhase.Moved)
{
// Get movement of the finger since last frame
Vector3 touchDeltaPosition = Input.GetTouch(i).deltaPosition;
// Move object across XY plane
transform.Translate(touchDeltaPosition.x * speed, 0, 0);
Vector3 boundaryVector = transform.position;
boundaryVector.x = Mathf.Clamp (boundaryVector.x, -5.5f, -2.8f);
transform.position = boundaryVector;
}
}
else
{
touched = false;
}
}
}
Hope this help,

Move a player along Y axis using touch location

I'm trying to move a player when the screen is tapped to where the screen is tapped, but only along the Y axis. I've tried this:
Vector2 touchPosition;
[SerializeField] float speed = 1f;
void Update() {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// assign new position to where finger was pressed
transform.position = new Vector3 (transform.position.x, Input.GetTouch(i).position.y, transform.position.z);
}
}
}
But the player disappears rather than moves. What am I doing wrong?
You need to convert the touch position from Screen to World. This is very easy to do, I've just knocked this quick script together, hopefully it helps:
using UnityEngine;
using System.Collections;
public class TouchSomething : MonoBehaviour
{
public GameObject thingToMove;
public float smooth = 2;
private Vector3 _endPosition;
private Vector3 _startPosition;
private void Awake()
{
_startPosition = thingToMove.transform.position;
}
private void Update()
{
if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
_endPosition = HandleTouchInput();
}
else
{
_endPosition = HandleMouseInput();
}
thingToMove.transform.position = Vector3.Lerp(thingToMove.transform.position, new Vector3(_endPosition.x, _endPosition.y, 0), Time.deltaTime * smooth);
}
private Vector3 HandleTouchInput()
{
for (var i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
var screenPosition = Input.GetTouch(i).position;
_startPosition = Camera.main.ScreenToWorldPoint(screenPosition);
}
}
return _startPosition;
}
private Vector3 HandleMouseInput()
{
if(Input.GetMouseButtonDown(0))
{
var screenPosition = Input.mousePosition;
_startPosition = Camera.main.ScreenToWorldPoint(screenPosition);
}
return _startPosition;
}
}
This allows you to also test in the editor as well.
I hope this helps.

Categories