- Home /
I need independent object movement
Hi, i'm brand new at Unity, and following some videos I manage to create my cube movement (it simply rotates in 90 fixed degrees)
Now my problem is that every cube I put in the scene moves at the same time, I need to move just the one it's being touched/clicked.
this is my swipe detector
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Swipe : MonoBehaviour { private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown; private bool isDraging = false; private Vector2 startTouch, swipeDelta;
private void Update()
{
tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
#region Standalone Inputs
if(Input.GetMouseButtonDown(0))
{
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}
else if(Input.GetMouseButtonUp(0))
{
isDraging = false;
Reset();
}
#endregion
#region Movile Inputs
if(Input.touches.Length != 0)
{
if(Input.touches[0].phase == TouchPhase.Began)
{
isDraging = true;
tap = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDraging = false;
Reset();
}
}
#endregion
//Calculate the distance
swipeDelta = Vector2.zero;
// see about deadzone
if(isDraging)
{
if(Input.touches.Length >0)
swipeDelta = Input.touches[0].position - startTouch;
else if(Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
// did we cross the deadzone
if(swipeDelta.magnitude > 150)
{
// which direction
float x = swipeDelta.x;
float y = swipeDelta.y;
if(Mathf.Abs(x) > Mathf.Abs(y))
{
// left or right
if(x<0)
{
swipeLeft=true;
Debug.Log("Swipe Left");
}
else
{
swipeRight=true;
Debug.Log("Swipe Right");
}
}
else
{
// up or down
if(y<0)
{
swipeDown = true;
Debug.Log("Swipe Down");
}
else
{
swipeUp = true;
Debug.Log("Swipe Up");
}
}
Reset();
}
}
private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
isDraging = false;
}
public Vector2 SwipeDelta { get { return swipeDelta;}}
public bool SwipeLeft { get { return swipeLeft;}}
public bool SwipeRight { get { return swipeRight;}}
public bool SwipeUp { get { return swipeUp;}}
public bool SwipeDown { get { return swipeDown;}}
}
and the movement (rotation) using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SwipeTest : MonoBehaviour { public Swipe swipeControls; public Transform player; public float smoothTime = 1.0f;
private void Start()
{
//Debug.Log(player);
}
// Update is called once per frame
private void Update()
{
if (swipeControls.SwipeLeft)
{
StartCoroutine( RotateAround( Vector3.up, 90.0f, smoothTime) );
IEnumerator RotateAround( Vector3 axis, float angle, float duration )
{
float elapsed = 0.0f;
float rotated = 0.0f;
while( elapsed < duration )
{
float step = angle / duration * Time.deltaTime;
player.transform.RotateAround(player.transform.position, axis, step );
elapsed += Time.deltaTime;
rotated += step;
yield return null;
}
player.transform.RotateAround(player.transform.position, axis, angle - rotated );
}
}
if (swipeControls.SwipeRight)
{
{
StartCoroutine( RotateAround( Vector3.up, -90.0f, smoothTime) );
IEnumerator RotateAround( Vector3 axis, float angle, float duration )
{
float elapsed = 0.0f;
float rotated = 0.0f;
while( elapsed < duration )
{
float step = angle / duration * Time.deltaTime;
player.transform.RotateAround(player.transform.position, axis, step );
elapsed += Time.deltaTime;
rotated += step;
yield return null;
}
player.transform.RotateAround(player.transform.position, axis, angle - rotated );
}
}
}
if (swipeControls.SwipeUp)
{
{
StartCoroutine( RotateAround( Vector3.right, 90f, smoothTime) );
IEnumerator RotateAround( Vector3 axis, float angle, float duration )
{
float elapsed = 0.0f;
float rotated = 0.0f;
while( elapsed < duration )
{
float step = angle / duration * Time.deltaTime;
player.transform.RotateAround(player.transform.position, axis, step );
elapsed += Time.deltaTime;
rotated += step;
yield return null;
}
player.transform.RotateAround(player.transform.position, axis, angle - rotated );
}
}
}
if (swipeControls.SwipeDown)
{
StartCoroutine( RotateAround( Vector3.right, -90f, smoothTime) );
IEnumerator RotateAround( Vector3 axis, float angle, float duration )
{
float elapsed = 0.0f;
float rotated = 0.0f;
while( elapsed < duration )
{
float step = angle / duration * Time.deltaTime;
player.transform.RotateAround(player.transform.position, axis, step );
elapsed += Time.deltaTime;
rotated += step;
yield return null;
}
player.transform.RotateAround(player.transform.position, axis, angle - rotated );
}
}
//player.transform.rotation = Quaternion.Lerp (transform.rotation, desiredRotation, smoothTime);
}
}
How do I make this independent for each cube???
THAKS A LOTTT!!!!
Why not add a boolean flag like 'isCurrentCube' which would indicate which cube is being controlled.
so all you would have to do is get the cube's movement script and through a public method just set the flag to true then you would be able to move that cube. However, to change the flag back to what it was originally, we also need a variable that points to the game object that was being controlled, so then when we click another game object we can compare the last game object with the current object that was clicked and set the last game object's flag to false if they aren't the same.
Some pseudo-code: For Clicking Script
private GameObject lastCube;//the last cube that was clicked
/*when click on any cube make a check*/
if(lastCube != null && clickedCube != lastCube){
lastCube = clickedCube;
SetControlFlag(false);
}
/*sets the flag of object to true or false*/
private void SetControlFlag(bool val){
lastCube.GetComponent<CubeScript>().ChangeControl(val);
}
For Cube Script
private bool can$$anonymous$$ove = false;//flag to deter$$anonymous$$e if cube can move
/*public function to change control*/
public void ChangeControl(bool val){
can$$anonymous$$ove = val;
}
/*before any movement check if user in control*/
if(can$$anonymous$$ove){
//code to make cube move
}
I hope this helps