- Home /
How to move 2D textures?
Hi. I rewrote a code that was made for moving 3D objects and I want to move 2D Textures. It doesn't seem wrong but I don't think it would work. So what's the problem with it?
(Edit: It's not he code I rewrote but there's a problem with this. So the question is the same.)
using UnityEngine;
using System.Collections;
public class CR_Drag : MonoBehaviour
{
[SerializeField]
float _horizontalLimit = 2.5f, _verticalLimit = 2.5f, dragSpeed = 0.1f;
Transform cachedTransform;
Vector3 startingPos;
void Start()
{
//Make reference to transform
cachedTransform = transform;
//Save starting position
startingPos = cachedTransform.position;
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Vector2 deltaPosition = Input.GetTouch(0).deltaPosition;
//Switch through touch events
switch (Input.GetTouch(0).phase)
{
case TouchPhase.Began:
break;
case TouchPhase.Moved:
DragObject(deltaPosition);
break;
case TouchPhase.Ended:
break;
}
}
}
/// <summary>
/// Drags the object.
/// </summary>
/// <param name='deltaPosition'>
/// Delta position.
/// </param>
void DragObject(Vector2 deltaPosition)
{
cachedTransform.position = new Vector3(Mathf.Clamp((deltaPosition.x * dragSpeed) + cachedTransform.position.x,
startingPos.x - _horizontalLimit, startingPos.x + _horizontalLimit),
Mathf.Clamp((deltaPosition.y * dragSpeed) + cachedTransform.position.y,
startingPos.y - _verticalLimit, startingPos.y + _verticalLimit),
cachedTransform.position.z);
}
}
Comment
Your answer
Follow this Question
Related Questions
Bad piggies-like inventory? 0 Answers
Creating a Game for IPhone/Android but testing it on a P.C. 1 Answer
Scroll Rect Content Disappearing (Video) 1 Answer
Player Jump on Mobile phone? 0 Answers
Remove Drag/Friction 1 Answer