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
1
Question by JianZhang969 · Jan 14, 2021 at 07:13 PM · scripting problemtouchrotatescripting beginnerscriptable object

I have a rotation script that is applied to two different objects. If I rotate one it also rotates the other and I do not want that.

Hello, I am making an AR application and I managed to get some of my objects to rotate by touch, however, when I touch one object, the other object rotates at the same time. I want them to act independently of one another, where rotating one doesn't rotate the other.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class rotation : MonoBehaviour
 {
     private float turnSpeed = 100f;
     private float _startingPosition;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
             RaycastHit Hit;
             if (Physics.Raycast(ray, out Hit)) 
             {
                 switch (touch.phase)
                 {
                     case TouchPhase.Began:
                         _startingPosition = touch.position.x;
                         break;
 
                     case TouchPhase.Stationary:
                         if (_startingPosition > touch.position.x)
                         {
                                 this.transform.Rotate(Vector3.forward, turnSpeed * Time.deltaTime);
                         }
                         else if (_startingPosition < touch.position.x)
                         {
                                 this.transform.Rotate(Vector3.forward, -turnSpeed * Time.deltaTime);
                         }
                         break;
                     case TouchPhase.Ended:
                         Debug.Log("Touch Phase Ended.");
                         break;
                 }
             } 
         }
             
     }
         
 }
 
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
1
Best Answer

Answer by TimBur · Jan 15, 2021 at 05:07 AM

I think your 'if' is a little vague. Physics.Raycast returns true as long as it hits something. So your code makes the object rotate any time that the user touches something - even if that something is a different object.

Because both objects have the same code, both objects rotate any time that the user touches something - even if that something is a different object. So any time the user touches something, everything rotates.

You need to make the 'if' more specific. If the object that is touched is this object, then you rotate this object. Else, if some other object is hit, you do nothing.

Try revising your raycast and if like this:

 RaycastHit HitInfo;
 bool HitSomething;
 HitSomething = Physics.Raycast(ray, out HitInfo))
 if (HitSomething && HitInfo.collider.gameObject == this.gameObject)
     // Then do the rotation stuff
Comment
Add comment · Show 4 · 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 JianZhang969 · Jan 15, 2021 at 04:45 PM 0
Share

Thank you for the explanation, that makes sense, didn't know you could assign the physics.raycast to something else. One more thing, for the the HitInfo.collider.gameObject, if I'm reading the official documentation right, it means that if the the ray hits the collision box of that particular object, only then will it fulfil one of the if condition requirements.

Also, for the other if statement requirement, should I put Hitsomething = true? or is that redundant.

avatar image TimBur JianZhang969 · Jan 15, 2021 at 05:38 PM 0
Share

For your first question, maybe sort of. You're asking about HitInfo.collider.gameObject, which is really just a number. The if doesn't look at the number, but at the result of comparisons of numbers. So you have to look at the expression as a whole.

I'm going to take a side trip into program$$anonymous$$g languages, apologies if you already know this. GameObjects live in computer memory. Each one resides at an address in memory, a location on a chip. The address is a really long number. That expression you mentioned, "HitInfo.collider.gameObject" is a reference-type variable. It references the GameObject that was hit by the Raycast, and it holds a numerical address - the address for the location of that GameObject.

Similarly, "this.gameObject" is also reference-type variable, and it holds the numerical address of the GameObject that the script is attached to.

The expression "HitInfo.collider.gameObject == this.gameObject" compares those two numbers. It looks to see if the number-address of the thing the Raycast hit is the same as the number-address of the GameObject that the script is attached to. So, yes, it means what you said - if the ray hits the collision box of the object that the script is attached to, then the second half of the if will be true.

Yes. Just what you said. "HitSomething == true" would be redundant. 'If' statements require that the thing in parentheses boil down to a true/false. It doesn't matter how you get there. If the thing is already a true/false (as HitSomething is) then you don't need to do anything more, as the if already has what it needs.

avatar image JianZhang969 TimBur · Jan 17, 2021 at 05:30 AM 0
Share

Ah okay, thank you for the explanation, I'm new to this so this has helped a lot in understanding the code.

Show more comments
avatar image
0

Answer by Klepzy · Jan 14, 2021 at 08:08 PM

For the object that you dont want to rotate:

  • Add a rigidbody component in the inspector

  • lock the x, y, and z rotation, and also on the position

hopefully this helps

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

241 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

Related Questions

define touch area 2 Answers

Save different game object's datas using the same script? 3 Answers

[C#] How to make this code toggleable 3 Answers

Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer

Playing an animation attached to another object via C# script? 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