I'm trying to determine between tapping the screen and swiping the screen.
I want to determine swipes from left, right, up and down.
Right now when I swipe left or right, it also turns my player.
Which isn't suppose to happen. It should be one or the other, either turn or move left or right. My question is how can I determine all five things?
Swipe left, right, up, and down as-well as just tapping the screen.
Here is my code
using UnityEngine;
using System.Collections;
public class PlayerMotor : MonoBehaviour {
private CharacterController controller;
private Vector3 moveVector;
private float speed = 2.0f;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
public Touch touch;
private void Start() {
controller = GetComponent<CharacterController> ();
}
private void Update()
{
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
if (Input.GetMouseButton (0))
{
if (touch.position.x == touch.deltaPosition.x && touch.position.x == touch.deltaPosition.x)
{ //3px accuracy, stationery :P
moveVector.x = transform.forward.x * speed;
transform.Rotate (new Vector3 (0, -90, 0));
}
else if (touch.position.x != touch.deltaPosition.x && touch.position.x != touch.deltaPosition.x)
{
if (Input.mousePosition.x > Screen.width / 2)
moveVector.x = speed;
else
moveVector.x = -speed;
}
}
moveVector.y = verticalVelocity;
moveVector.z = transform.forward.z * speed;
controller.Move (moveVector * Time.deltaTime);
}
}
I found this code with a little bit of searching. (Here)
Adding an else statement after all the if (currentSwipe...) statements should work to detect the click
//inside class
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
public void Swipe()
{
if(Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if(t.phase == TouchPhase.Began)
{
//save began touch 2d point
firstPressPos = new Vector2(t.position.x,t.position.y);
}
if(t.phase == TouchPhase.Ended)
{
//save ended touch 2d point
secondPressPos = new Vector2(t.position.x,t.position.y);
//create vector from the two points
currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
//normalize the 2d vector
currentSwipe.Normalize();
//swipe upwards
if(currentSwipe.y > 0 currentSwipe.x > -0.5f currentSwipe.x < 0.5f)
{
Debug.Log("up swipe");
}
//swipe down
if(currentSwipe.y < 0 currentSwipe.x > -0.5f currentSwipe.x < 0.5f)
{
Debug.Log("down swipe");
}
//swipe left
if(currentSwipe.x < 0 currentSwipe.y > -0.5f currentSwipe.y < 0.5f)
{
Debug.Log("left swipe");
}
//swipe right
if(currentSwipe.x > 0 currentSwipe.y > -0.5f currentSwipe.y < 0.5f)
{
Debug.Log("right swipe");
}
}
}
}
Related
For Smooth touch in unity but i dont know where should i use Mathf.Clamp to restrict the movement of player in the map.
Got It.
You can simply change the position calculation and add a certain hard limit like e.g.
public float minX;
public float minY;
public float maxX;
public float maxY;
public float playerSpeed;
[SerializeField] private Camera _mainCamera;
void Awake()
{
if(!_mainCamera) _mainCamera = Camera.main;
}
void Update()
{
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
var touchPosition = touch.position;
var currentPosition = transform.position;
var speed = playerSpeed * Time.deltaTime;
// get this objects position in screen (pixel) space
var screenPosition = _mainCamera.WorldToScreenPoint(transform.position);
if (touchPosition.x < screenPosition.x)
{
currentPosition.x -= speed;
}
else if (touchPosition.x > screenPosition.x)
{
currentPosition.x += speed;
}
currentPosition.x = Mathf.Clamp(currentPosition.x, minX, maxX);
if (touchPosition.y < screenPosition.y)
{
currentPosition.y -= speed;
}
else if (touchPosition.y > screenPosition.y)
{
currentPosition.y += speed;
}
currentPosition.y = Mathf.Clamp(currentPosition.y, minX, maxX);
transform.position = currentPosition;
}
}
I've set up my swipe controls to transform my player left/right/up/down and I've restricted the movement on 3 lines; -1, 0, 1. Everything works fine but the movement is not smooth at all and the player seems to be "teleporting" from one position to another. I wanted to smooth the movement by playing the animation but the result was that the animation was being played after the player has changed his position.
Is there any way to play the animation while the player is changing his position or a way to smooth the movement so it looks right ?
I've tried everything and now I'm stuck with the problem, please help
Here's my code
public class SwipeControls : MonoBehaviour {
public float speed = 5.0f;
private Vector3 startpos; // start position
private Vector3 endpos; //end position
public int pozioma = 0;
public int pionowa = 0;
Animator anim;
void Start() {
GetComponent<Animator>();
}
void Update() {
foreach (Touch touch in Input.touches) {
Vector3 newPosition;
if (touch.phase == TouchPhase.Began) {
startpos = touch.position;
endpos = touch.position;
}
if (touch.phase == TouchPhase.Moved) {
endpos = touch.position;
}
if (touch.phase == TouchPhase.Ended) {
newPosition = transform.position;
if (Mathf.Abs(startpos.y - endpos.y) > Mathf.Abs(startpos.x - endpos.x)) {
if ((startpos.y - endpos.y) > 100 && pionowa > -1) //swipe down
{
pionowa--;
newPosition.y -= speed;
transform.position = newPosition;
anim.SetTrigger("Flydown");
}
if ((startpos.y - endpos.y) < -100 && pionowa < 1) //swipe up
{
pionowa++;
newPosition.y += speed;
transform.position = newPosition;
anim.SetTrigger("Flyup");
}
}
else {
if ((startpos.x - endpos.x) > 100 && pozioma > -1) //swipe left
{
pozioma--;
newPosition.z -= speed;
transform.position = newPosition;
anim.SetTrigger("Flyleft");
}
}
if ((startpos.x - endpos.x) < -100 && pozioma < 1) //swipe right
{
pozioma++;
newPosition.z += speed;
transform.position = newPosition;
anim.SetTrigger("Flyright");
}
}
}
}
}
Modify your code instead of using just
transform.position = newPosition;
in all position use
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
where smooth is float variable assign it required value may be 0.5f or whatever you want
Note : What it actually does is that it makes game object to move little based on smooth value, every frame
Note that we do not know your animator setup. If in those animation clips there is anywhere a keyframe for the position then the animator will always overrule anything done by scripts!
Instead of setting the new position immediately you could use a Coroutine with StartCoroutine like (also fixing some coding styles)
public class SwipeControls : MonoBehaviour
{
// Flag for ignoring input until current animation finished
private bool routineRunning;
// Configure in the Inspector distance to swipe
// in unity units
[SerializeField] private float swipeDistance = 5;
// configure this in the Inspector
// How long should the swiping take in seconds
[SerializeField] private float swipeDuration = 1;
private Vector3 _startpos; // start position
private Vector3 _endpos; //end position
public int pozioma = 0;
public int pionowa = 0;
private Animator _anim;
private void Start()
{
GetComponent<Animator>();
}
private void Update()
{
// Ignore any Input while a routine is running
if (routineRunning) return;
foreach (var touch in Input.touches)
{
switch (touch.phase)
{
case TouchPhase.Began:
_startpos = touch.position;
_endpos = touch.position;
break;
case TouchPhase.Moved:
_endpos = touch.position;
break;
case TouchPhase.Ended:
if (Mathf.Abs(_startpos.y - _endpos.y) > Mathf.Abs(_startpos.x - _endpos.x))
{
if (_startpos.y - _endpos.y > 100 && pionowa > -1) //swipe down
{
pionowa--;
StartCoroutine(MoveSmooth(Vector3.up * -1, swipeDuration));
_anim.SetTrigger("Flydown");
}
else if (_startpos.y - _endpos.y < -100 && pionowa < 1) //swipe up
{
pionowa++;
StartCoroutine(MoveSmooth(Vector3.up, swipeDuration));
_anim.SetTrigger("Flyup");
}
}
else
{
if (_startpos.x - _endpos.x > 100 && pozioma > -1) //swipe left
{
pozioma--;
StartCoroutine(MoveSmooth(Vector3.forward * -1, swipeDuration));
_anim.SetTrigger("Flyleft");
}
else if (_startpos.x - _endpos.x < -100 && pozioma < 1) //swipe right
{
pozioma++;
StartCoroutine(MoveSmooth(Vector3.forward, swipeDuration));
_anim.SetTrigger("Flyright");
}
}
break;
}
}
}
private IEnumerator MoveSmooth(Vector3 direction, float duration)
{
routineRunning = true;
var currentPosition = transform.localPosition;
var targetPosition = currentPosition + direction * swipeDistance;
var timePassed = 0f;
do
{
// Additionally add some ease in and out to the movement to get
// even smoother movement
var lerpFactor = Mathf.SmoothStep(0, 1, timePassed / duration);
// Interpolate the position between currentPosition and targetPosition
// using the factor between 0 and 1
transform.localPosition = Vector3.Lerp(currentPosition, targetPosition, lerpFactor);
// increase by time since last frame
timePassed += Time.deltaTime;
// let this frame render and go on from
// here in the next frame
yield return null;
} while (timePassed <= duration);
// To avoid over or undershooting in the end set a fixed value
transform.localPosition = targetPosition;
routineRunning = false;
}
}
I don't know why you are using y+= and z+= .. it seems to me that it meeans the Camera is facing left in your scene e.g. with rotation = 0, -90, 0 and e.g. position = 10, 0, 0 than this should be the result (Note I copied the same code into if(GetMouseButtonDown(0)) etc in order to simulate the touch on the PC.)
Edit : I need an answer for a 2D implementation, not 3D. So the 'Asked Before' question doesn't work for my project.
Edit : Using the accepted answer below, this is my working script. It's slightly off because the 0.1F part of the Vector3 is hard to get right, but it works. Time.deltaTime didn't work though.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraZoom : MonoBehaviour
{
private float zoom = 10;
Vector3 newPosition;
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0 && zoom > 9)
{
zoom -= 1;
Camera.main.orthographicSize = zoom;
newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = Vector3.Lerp(transform.position, newPosition, 0.1F);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && zoom < 101)
{
zoom += 1;
Camera.main.orthographicSize = zoom;
}
}
}
Original Question : I have a basic script for zooming in and out, but when I zoom back in with the mouse-wheel, I want to zoom in to the point of the mouse cursor. I think you can see something similar in the Paradox Games (EU4 etc), where you zoom out, hold your mouse over a country off to the left of the screen, then zoom in and the country zooms in and becomes centered.
Here's my basic script so far, attached to my camera, which just zooms in and out in a straight line.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraZoom : MonoBehaviour {
public float zoom = 10F;
void Update () {
if (Input.GetAxis("Mouse ScrollWheel") > 0 && zoom > 9)
{
zoom -= 1;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && zoom < 101)
{
zoom += 1;
}
GetComponent<Camera>().orthographicSize = zoom;
}
}
I haven't tried this myself but I hope this will work.
Vector3 newPosition;
bool canZoom;
bool isMoving;
void Update ()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0 && zoom > 9)
{
zoom -= 1;
canZoom = true;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && zoom < 101)
{
zoom += 1;
canZoom = true;
}
if (canZoom)
{
isMoving = true;
Camera.main.orthographicSize = zoom;
newPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
}
if (isMoving)
{
transform.position = Vector3.Lerp (transform.position, newPosition, Time.deltaTime);
}
if(transform.position == newPosition)
{
isMoving = false;
}
}
Note: If you want the camera to maintain certain camera height, adjust newPosision's y value before moving.
Hope this helps.
Calculate the difference in world space and move towards it while zooming.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraZoom : MonoBehaviour {
public float zoom = 10F;
void Update () {
if (Input.GetAxis("Mouse ScrollWheel") > 0 && zoom > 9)
{
zoom -= 1;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && zoom < 101)
{
zoom += 1;
}
Camera cam = GetComponent<Camera>();
RaycastHit hit;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
Vector3 targetPos;
if (Physics.Raycast(ray , out hit))
{
targetPos = hit.point;
}
Camera cam = GetComponent();
cam.orthographicSize = zoom;
cam.transform.position += (targetPos - transform.position) / 5f;
}
}
The bases of my game is a simple 2D top down click to move game I. I created a walk blend tree and a idle blend tree. The both blend tree has 6 directional movement. I watch this tutorial on Top-down 8 directions movement. (https://www.youtube.com/watch?v=7URRg8J6mz8). Basically I change the script around so that it could match with my click to move script, but it not 100% perfect. My idle state is not working properly or neither is my Y-axis, to be more speific, when I click long the Y-axis (let's say i'm going up(+)) it's not really playing the right animation in the animator. (Now let's say I went down) It would play the right animation but (when finished with movement) it would continue playing the animation. My parameters are also not working properly, SpeedY and LastMoveY aren't stable (if i'm making any sense). Can someone help me fix this? Here's my game preview uploaded in google drive if anyone doesn't understand what I mean! http://html.editey.com/file/0B6cfYGCOex7BVC1Ja3Q3N3d3NWc#
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour {
private Animator anim;
public float speed = 15f;
public move playerMovementRef;
private Vector3 target;
private Vector3 playerObject;
void Start () {
target = transform.position;
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
float inputX = Input.GetAxis ("Mouse X");
float inputY = Input.GetAxis ("Mouse Y");
if (Input.touchCount > 0)
{
inputX = Input.touches[0].deltaPosition.x;
inputY = Input.touches[0].deltaPosition.y;
}
anim.SetFloat ("SpeedX", inputX);
anim.SetFloat ("SpeedY", inputY);
}
void FixedUpdate () {
float LastInputX = Input.GetAxis ("Mouse X");
float LastInputY = Input.GetAxis ("Mouse Y");
if (Input.touchCount > 0)
{
LastInputX = Input.touches[0].deltaPosition.x;
LastInputY = Input.touches[0].deltaPosition.y;
}
if (LastInputX != 0 || LastInputY != 0) {
anim.SetBool ("walking", true);
if (LastInputX > 0) {
anim.SetFloat ("LastMoveX", 1f);
} else if (LastInputX < 0) {
anim.SetFloat ("LastMoveX", -1f);
} else {
anim.SetBool ("walking", false);
}
if (LastInputY > 0) {
anim.SetFloat ("LastMoveY", 1f);
} else if (LastInputY < 0) {
anim.SetFloat ("LastMoveY", -1f);
} else {
anim.SetFloat ("LastMoveY", 0f);
}
} else {
anim.SetBool ("walking", false);
}
}
You should not use Input values inside FixedUpdate. You should store it in a variable inside Update and then check it inside FixedUpdate.
You should also add some depth to the mouse position according to your camera.
wrong:
void FixedUpdate () {
if (Input.touchCount > 0)
{
...
}
}
correct:
void Update () {
...
if (Input.touchCount > 0)
{
touched = true;
}
}
void FixedUpdate () {
if (touched)
{
...
}
}
sample:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint(mousePosition);
target.z = transform.position.z;
}
if (Input.touchCount > 0)
{
target.x = Input.touches[0].deltaPosition.x;
target.y = Input.touches[0].deltaPosition.y;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
void FixedUpdate()
{
float LastInputX = transform.position.x - target.x;
float LastInputY = transform.position.y - target.y;
if (LastInputX != 0 || LastInputY != 0)
{
anim.SetBool("walking", true);
if (LastInputX > 0)
{
anim.SetFloat("LastMoveX", 1f);
}
else if (LastInputX < 0)
{
anim.SetFloat("LastMoveX", -1f);
}
else {
anim.SetBool("walking", false);
}
if (LastInputY > 0)
{
anim.SetFloat("LastMoveY", 1f);
}
else if (LastInputY < 0)
{
anim.SetFloat("LastMoveY", -1f);
}
else {
anim.SetFloat("LastMoveY", 0f);
}
}
else {
anim.SetBool("walking", false);
}
}
Here is a video of the problem:
https://pbs.twimg.com/tweet_video/CPmbdpMWIAAUesX.mp4
The issue is whenever the ship flies in the direction of its shield, it seems to stop at regular intervals.
Here's the code for the ship and the shield:
public class MoveOnAxis : MonoBehaviour {
public float speed = 1.0f;
public float defaultDrag = 0.0f;
public float maxDrag = 3.0f;
public float maxAngularDrag = 3.0f;
public float maxTumble = 720;
public Transform shield;
private float distance;
private float hAxis;
private float deadzone = 0.15f;
private float aVel;
private Rigidbody2D rb;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
hAxis = Input.GetAxis("Horizontal");
aVel = rb.angularVelocity;
if(Mathf.Abs(hAxis) > 0.15f) //Controlls ship rotation
{
if(aVel > 10.0f) //if spinning out after a crash clockwise
{
if(aVel <= maxTumble && hAxis > deadzone)
{
rb.angularVelocity -= hAxis*speed*2;
}
else if(aVel < maxTumble && hAxis < -deadzone)
{
rb.angularVelocity -= hAxis*speed*2;
}
else if(aVel > maxTumble && hAxis < -deadzone)
{
rb.angularVelocity = maxTumble;
}
}
else if (aVel < -10.0f) //if spinning out of a crash anti-clockwise
{
if(aVel >= -maxTumble && hAxis < -deadzone)
{
rb.angularVelocity -= hAxis*speed*2;
}
else if(aVel > -maxTumble && hAxis > deadzone)
{
rb.angularVelocity -= hAxis*speed*2;
}
else if(aVel < -maxTumble && hAxis > deadzone)
{
rb.angularVelocity = -maxTumble;
}
}
else //if no physics angular momentum go back to using transform for rotation
{
rb.angularVelocity = 0;
transform.Rotate(0.0f, 0.0f, hAxis*-speed);
}
}
if(Input.GetAxis("triggerAxis") > 0.1f) //Controlls ship accelleration and decelleration
{
//distance = Mathf.Abs((transform.position - shield.position).magnitude);
//if (distance <= 1.0f && distance >= 0.6f)
//{
rb.AddForce(transform.up * speed * Input.GetAxis("triggerAxis"));
//}
//else if (distance < 0.6f || distance > 1.0f)
//{
// rb.velocity = new Vector2(0f,0f);
//}
}
if(Input.GetAxis("triggerAxis") < -0.1f)
{
rb.drag = maxDrag*-Input.GetAxis("triggerAxis");
rb.angularDrag = maxAngularDrag*-Input.GetAxis("triggerAxis");
}
else
{
rb.drag = defaultDrag;
rb.angularDrag = defaultDrag;
}
}
}
and
public class ShieldMovement : MonoBehaviour {
public Transform target; //player shield is attaced to
public float circSpeed = 0.1f; // Speed Shield moves around ship
private Vector3 direction = Vector3.up;
private float distance = 0.8f; // distance from player so it doesn't clip
private float deadzone = 0.15f;
private float separation;
private bool canMove = true;
private Rigidbody2D tRB;
private Rigidbody2D rb;
// Use this for initialization
void Start ()
{
tRB = target.GetComponent<Rigidbody2D>();
rb = GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D (Collision2D other)
{
canMove = false;
}
void OnCollisionStay2d(Collision2D other)
{
canMove = false;
}
void OnCollisionExit2D (Collision2D other)
{
canMove = true;
}
// Update is called once per frame
void Update () {
float rh = Input.GetAxisRaw("rightH");
float rv = Input.GetAxisRaw("rightV");
separation = Mathf.Abs((transform.position - target.position).magnitude);
if(Mathf.Abs(rh) > deadzone || Mathf.Abs(rv) > deadzone)
{
Vector3 targetDir = new Vector3(rh, rv, 0.0f);
direction = Vector3.Slerp(transform.position-target.position, targetDir, circSpeed);
}
Ray ray = new Ray(target.position, direction); // cast ray in direction of point on circle shield is to go
if(canMove)
{
transform.position = ray.GetPoint(distance); //move shield to the ray as far as the distance
}
else if(!canMove)
{
tRB.velocity = new Vector2(0.0f, 0.0f);
//rb.velocity = new Vector2(0.0f, 0.0f);
transform.position = transform.position;
}
if(separation != distance) //If the shield get torn away from the ship
{
transform.position = ray.GetPoint(distance); //move shield to the ray as far as the distance
}
float angleY = transform.position.y - target.position.y;
float angleX = -(transform.position.x - target.position.x);
float angle = Mathf.Atan2 (angleY, angleX) * Mathf.Rad2Deg-90; //Get angle
if(Mathf.Abs(rh) > deadzone || Mathf.Abs(rv) > deadzone)
{
transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1); // keep shield facing outwards in respect to player
}
}
}
I've tried taking out the stuff that stops moving the ship like tRB.velocity = new Vector2(0.0f, 0.0f); but it didn't make any difference at all. Any ideas on what I need to do here?
Edit
Removing the collider from the shield fixes the problem so it seems like somehow the shield is colliding with the ship even though it doesn't look like it and the raycast should keep it far enough away from the ship. Not sure where to go from here.
For physics system updates, it's best practice to put them in FixedUpdate() instead of Update(). The former can be used to change the velocity or add forces, and the latter can be used for getting the input from the user.
To prevent the ship and shield from colliding, use a different tag on each one, then compare the other.collider.tag to ignore those collisions.
// e.g. in ShieldMovement
void OnCollisionEnter2D (Collision2D other)
{
if (other.collider.tag != "Ship") {
canMove = false;
}
}
It seems the shield was colliding with the ship somehow, once I put them on separate non interacting layers the problem went away :D