Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 snainner · Apr 13, 2021 at 12:39 PM · scripting problemscripting beginnermovement scriptselection

How do I tie my movement script to only selected objects?,How do I tie my movement script to the my selection script?

I'm trying to make the player only move the selected item and not all at once. Right now I have a movement script that looks like this:

  void Update()
     {
         if (Input.GetKey(KeyCode.A))
             transform.Rotate(-Vector3.forward * RotateSpeed * Time.deltaTime);
         else if (Input.GetKey(KeyCode.D))
             transform.Rotate(Vector3.forward * RotateSpeed * Time.deltaTime);
     }

And a selection script that looks like this:

 void MouseInput()
     {
         // When Mouse 0 is pressed
         if (Input.GetMouseButton(0))
         {
             // Raycast from camera to mouse position
             Vector2 raycastPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             RaycastHit2D hit = Physics2D.Raycast(raycastPos, Vector2.zero);
 
             // If a collider is hit
             if (hit.collider != null)
             {
                 // Log the name of the hit object
                  Debug.Log(hit.collider.gameObject.name);
                 
             }
 
            if (hit.collider.gameObject.name != null)
             {
                 Debug.Log("Im called " + hit.collider.gameObject.name + " and I'm hit and should move");
             }
            
 
         }

Unity does know when something is selected or "hit" and I have my movement script on all the game objects that need movement right now.

Comment
Add comment · Show 2
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 jackmw94 · Apr 13, 2021 at 01:24 PM 1
Share

Just to clarify, your desired behaviour is to:
-Click (and hold) on an object
-Have it then be "selected"
-Once selected your A and D key inputs will move it

Let me know if that's correct.

avatar image snainner jackmw94 · Apr 13, 2021 at 01:30 PM 0
Share

Yes, with the click a want to select them (ideally with a button a press that cycles through them all) and then move it with A and D key.

1 Reply

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

Answer by HellsHand · Apr 13, 2021 at 04:06 PM

Here try this in 1 script on the objects you want the player to move:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class RotateObject : MonoBehaviour, ISelectHandler, IDeselectHandler
 {
     bool selected = false;
     void Update()
     {
         if (selected)
         {
             if (Input.GetKey(KeyCode.A))
                 transform.Rotate(-Vector3.forward * RotateSpeed* Time.deltaTime);
             else if (Input.GetKey(KeyCode.D))
                 transform.Rotate(Vector3.forward * RotateSpeed* Time.deltaTime);
         }
     }
 
     private void OnMouseDown()
     {
         if (Input.GetMouseButtonDown(0))
         {
             EventSystem.current.SetSelectedGameObject(gameObject);
         }
     }
 
     public void OnSelect(BaseEventData eventData)
     {
         selected = true;
     }
 
     public void OnDeselect(BaseEventData eventData)
     {
         selected = false;
     }
 }

Unless I'm misunderstanding your problem that should work.

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 snainner · Apr 15, 2021 at 12:20 PM 0
Share

It looks like it would be solve my issue but I'm having a bit of a problem with using it because I am getting this error: Object reference not set to an instance of an object selected.Update () (at Assets/selected.cs:14) And I'm not sure how to fix it. My selected script looks like this:

   void Update()
     {
         {
             if (EventSystem.current.currentSelectedGameObject)
             {
                 if (Input.GetKey(KeyCode.A))
                     transform.Rotate(-Vector3.forward * RotateSpeed * Time.deltaTime);
                 else if (Input.GetKey(KeyCode.D))
                     transform.Rotate(Vector3.forward * RotateSpeed * Time.deltaTime);
             }
         }
     }
 
     private void OnMouseDown()
     {
         if (Input.GetMouseButtonDown(0))
         {
             EventSystem.current.SetSelectedGameObject(gameObject);
         }
     }
 
 
 }

Edit: So I think I need to let unity know that the item is also selected in the event system but I'm having a syntax error right now trying to do that and I don't know how to fix that.

  // If a collider is hit
             if (hit.collider != null)
             {
                 // Log the name of the hit object
                 Debug.Log(hit.collider.gameObject.name);
                 EventSystem.SetSelectedGameObject(1Hole); 
 
             }

With the syntax error being: Assets\selection.cs(31,52): error CS1003: Syntax error, ',' expected

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

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

Auto-move on grid and draw line problem 0 Answers

Stop movement while attacking 1 Answer

Why wont my character stop moving when it collides with the obstacles?,Why doesn't my movement script stop? 0 Answers

How can I make my 2D sprite move in the direction of the most recent key pressed? 1 Answer

[SOLVED] How to still move the character but with different keys? 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