Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 unity_gd3I5NJvhKz8CA · Jan 20, 2021 at 10:07 AM · scripting problemmovementobject

Multiple object with same script

Hi all, new here and I have small problem that I cannot solve. I tried to read the forum and couldn't find solution that will help me. Anyway to the point:

I have a script attached to object. to move the object. All good. but when I add another object, they are moving together. I tried to debug and saw that line 34 was executed twice (2 object on the display) So I was thinking can it be because of the Raycast detection in same script?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class moveBlock : MonoBehaviour
 {
 
     private RaycastHit hit;                 //Detect side of block that was presssed
     private Vector3 mousePos;               //Mouse position on the collision
     private string hitName ;                //Collider name
     private Vector2 origPos, targetPos;     //Initioate original and target positions
     private float timeToMove = 0.2f;        //Time that will take to move the block
     private bool isMoving;                  //Is the block moving
     // Update is called once per frame
     void Update()
     {
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         if (Physics.Raycast(mousePos, Vector3.forward, out hit, 100.0f))
         {
             hitName = hit.collider.name;        //Write the name of the collider that was hit
         }
         else
         {
             hitName = "none";                   //If no collision accured, none. 
         }
 
         //set if velocity is not zero, isMoving = true;
 
         if (Input.GetMouseButtonDown(0) && !isMoving)
         {
             switch (hitName)
             {
                 case "r":
                     StartCoroutine(MoveBlock(Vector2.right / 2));
                     break;
                 case "l":
                     StartCoroutine(MoveBlock(Vector2.left / 2));
                     break;
                 case "u":
                     StartCoroutine(MoveBlock(Vector2.up / 2));
                     break;
                 case "d":
                     StartCoroutine(MoveBlock(Vector2.down / 2));
                     break;
                 default:
                     break;
             }
         }
 
     }
 
 
     private IEnumerator MoveBlock (Vector2 direction)
     {
         isMoving = true;
         float elapsedTime = 0;
 
         origPos = transform.position;
         targetPos = origPos + direction;
 
         while(elapsedTime<timeToMove)
         {
             transform.position = Vector2.Lerp(origPos, targetPos, (elapsedTime / timeToMove));
             elapsedTime += Time.deltaTime;
             yield return null;
         }
 
         transform.position = targetPos;
 
         isMoving = false;
     }
 
 }

Any idea where can I find good explanation or maybe someone have a explanation? Thanks in advance.

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
1

Answer by logicandchaos · Jan 20, 2021 at 03:27 PM

The move together because they have the same script on them. To make them move differently you will need to write a new script. Line 34 is called twice, once for each object. When case are is triggered for object 1 it is also triggered for object 2. How do you want the second object to move?

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 unity_gd3I5NJvhKz8CA · Jan 20, 2021 at 04:26 PM 0
Share

I don't want it to move until I click on it.

So I started to write 2 different scripts already.

One will detect a click on the block. Specific side of the block (to what side to move) and also will get name of specific block.

the other script will move block (it will get name of the block and to what side it will move)

Think I will connect both scripts to $$anonymous$$ain camera? Or is there any other way around. $$anonymous$$aybe more proper?

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

252 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

manipulating position.y in unity 2 Answers

How to Add Knockback Force Based on What Rotation it Came From 2 Answers

I can't get any type of joystick to move my player 1 Answer

How make movement speed constant with FPS? 1 Answer

Mouse Player Movement Controller 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