Player follows mouse, but I need only Y
Hi everyone! as you can read in the title I have this script that follows the mouse in X and Y axis, but I only want it to move in Y axis. I'm a total noob so I have no idea how to do it, here's my script:
using System;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
private Vector3 _target;
public Camera Camera;
public bool FollowMouse;
public bool ShipAccelerates;
public float ShipSpeed = 2.0f;
public void OnEnable()
{
if (Camera == null)
{
throw new InvalidOperationException("Camera not set");
}
}
public void Update()
{
if (FollowMouse || Input.GetMouseButton(0))
{
_target = Camera.ScreenToWorldPoint(Input.mousePosition);
_target.z = 0;
}
var delta = ShipSpeed*Time.deltaTime;
if (ShipAccelerates)
{
delta *= Vector3.Distance(transform.position, _target);
}
transform.position = Vector3.MoveTowards(transform.position, _target, delta);
}
}
I'm trying to create a control like THIS GAME
you can see gameplay at 0:40, you touch the screen and the spaceship follows the touch in Y axis
if you know a tutorial or something.
Thank you!
Answer by ismaelnascimento01 · May 26, 2017 at 07:48 PM
See it: - https://docs.unity3d.com/ScriptReference/Touch.html - https://docs.unity3d.com/ScriptReference/Input.GetTouch.html - https://docs.unity3d.com/ScriptReference/TouchPhase.html
Thank you
Your answer
Follow this Question
Related Questions
Z position moving in 2D space 1 Answer
KillPlayer, Null Reference Exception. 2 Answers
Issues with 2D collision/overlap detection,Help detecting 2D collision/overlap 0 Answers
How to make an enemy 2d chasing player? 2 Answers
2D fixed cam aim problem 0 Answers