Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Topthink · Nov 06, 2016 at 02:47 AM · class

I'm trying to move Cubes independently.

I've created a small class of five cubes and I want to be able to move them all independently and at the same time. So far, I can move all of them to the same exact series of waypoints, I simply "right click" and can add any number of waypoints.

BUT...I want to move them to different waypoints and I want to control each cube separately. I'm having a heckuva time figuring this out. Thanks for any suggestions.

 void Start ()
 {
     for (int i = 0; i < 5; i++)
     {
         ///// Creates Member of Class
         CombatUnit blueUnit = new CombatUnit();

         ///// Creates Cube
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         
         ///// Location of Cube selected randomly
         cube.transform.position = new Vector3(Random.Range(5f,25f), 0,Random.Range(5f,25f));

         ///// Adds movement script to cube
         cube.AddComponent<MoveTest>();
     }
 }

 // Update is called once per frame
 void Update() { }

 public class CombatUnit
 {
     public int UnitID { get; set; }
 }

}

//////////////

public class MoveTest : MonoBehaviour { public float speed; public List targetPosition = new List();

 void Start ()
 {
     speed = 5;  /////  This is temporary unit speed
 }
 
 void Update ()
 {
     ///// Click to get ID of unit that will move
     if(Input.GetMouseButtonDown(0))
     {
         RaycastHit hit1;
         Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);

         if(Physics.Raycast (ray1, out hit1))
         {
             BoxCollider bc = hit1.collider as BoxCollider;
             if (bc != null)
             {
                     if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit1, Mathf.Infinity))
                     {
                     Debug.Log(gameObject);
                     }
             }
             else
             {
                 Debug.Log("Miss");
             }
         }
     }

     ///// Creates a waypoint/location if the if the unit/object has arrived at last waypoint (to prevent system errors)
     ///// This also occurs if the unit/object has not been issued any movement orders
     ///// If there have been no right clicks of the mouse, this creates a waypoint at units own location
     if (targetPosition.Count <1)
     {
         targetPosition.Add(this.transform.position);
     }

     ///// Moves object towards First waypoint
     transform.position = Vector3.MoveTowards(transform.position, targetPosition[0], speed * Time.deltaTime);

     ///// Gets Rid of waypoint if unit/object gets really close to waypoint
     ///// When the zero index is eliminated, the next lower list[index] becomes the new 0 -- it becomes the new "First" waypoint
     ///// Thus, the first waypoint index is eliminated and all others move up a notch 
     if (Vector3.Distance(transform.position, targetPosition[0]) < .05)
     {
         targetPosition.RemoveAt(0);
     }

     /////  Adds to list of waypoints when you "right click" 
     if (Input.GetMouseButtonDown(1))
     {
         RaycastHit hit;

         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
         {
             targetPosition.Add(hit.point);
         }
     }

 }///// End Void Update

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Topthink · Nov 12, 2016 at 11:31 AM

I figured out how to do this although I truly appreciate anyone who gave it any thought.

Comment
Add comment · Share
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

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

76 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

Related Questions

Using constructors in Unity (C#) 8 Answers

(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers

How to create new for my custom class? 2 Answers

Can Static classes have static lists? 1 Answer

how to accsess sibling variables from a class. 1 Answer


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