Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mmiliozzi · Sep 29, 2020 at 10:59 AM · cubes

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!!!!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityToMakeMoney · Sep 30, 2020 at 02:36 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Will Goldstone turtorial; wall of cubes not normal, just exploding. 1 Answer

Scene composing question 0 Answers

Move 8 cubes depening on what round 1 Answer

Making ice/glass shatter without thousands of tiny cubes!(probably particle effect) 2 Answers

Shadows not showing on plane 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges