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 jw120691 · Oct 13, 2011 at 04:10 PM · movementtransformuntiy

Selecting and moving an individual object during gameplay..

Hello! I am very new to Unity (currently making my first game) and I am trying to write a script to select and move an individual cuboid object forwards and backwards using the up and down arrow keys. There are 5 cuboid objects in my game and at the moment I have successfully written a script to move them all at the same time but I want to be able to individually move them after selecting them with the mouse during gameplay.

The script I currently have is:

function Update () {

 var speed = 5.0;

 var move : float = Input.GetAxis("Vertical");
 
 Debug.Log(move);
 
 transform.Translate(Vector3(0, 0, move) * Time.deltaTime * speed);

}

Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Story Specialist · Oct 13, 2011 at 04:45 PM

Hey there, I'm somewhat of a beginner myself, but I think I can help. A simple way to this would be to use OnMouseUp: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseUp.html

Make a script and put in an OnMouseUp function. Have this function change a String (eg. SelectedObject). One that you keep in a separate script (I always make a script called GlobalVariables, which holds...global variables ^^). In your original script, make an if-statement that checks if the name of the GameObject it's attached to, is the same as the SelectedObject. Like so:

 function Update () {
 
     var speed = 5.0;
     var move : float = Input.GetAxis("Vertical");
 
     if (this.name == GlobalVariables.SelectedObject) {
         transform.Translate(Vector3(0, 0, move) * Time.deltaTime * speed);
     }
     Debug.Log(move);
 }
 
 function OnMouseUp () {
     GlobalVariables.SelectedObject = this.name;
 }

Where "GlobalVariables" is your separate script and "SelectedObject" is the String.

Your GlobalVariables script would be as simple as something like this:

 static var SelectedObject : String;
 
 function Update () {
 }

Now you can just attach your script to as many GameObjects as you want. I hope that helps you out!

Comment
Add comment · Show 2 · 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
avatar image jw120691 · Oct 13, 2011 at 05:01 PM 0
Share

THAN$$anonymous$$ YOU SO $$anonymous$$UCH!!!!!

That's brilliant mate, works perfectly.

Thanks again.

avatar image Story Specialist · Oct 13, 2011 at 05:50 PM 0
Share

@jw120691: $$anonymous$$y pleasure! I'm glad it worked. Can you please Accept my Answer, so others know a solution has been found? :)

avatar image
0

Answer by jw120691 · Oct 13, 2011 at 05:05 PM

THANK YOU SO MUCH!!!!!

That's brilliant mate, works perfectly.

Thanks again.

Comment
Add comment · Show 1 · 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
avatar image Story Specialist · Oct 13, 2011 at 05:45 PM 0
Share

@jw120691: $$anonymous$$y pleasure! I'm glad it worked. Can you please Accept my Answer, so others know a solution has been found? :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Selecting and moving an individual object during gameplay.. 2 Answers

How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers

Make an object move to a given point by rotating to the correct direction first. 1 Answer

Object moving sideways when translating using forward vector. 1 Answer

how do i slowly translate a object to a other objects position 2 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