- Home /
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;
}
}
}
}
}
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
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.
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.
Ah okay, thank you for the explanation, I'm new to this so this has helped a lot in understanding the code.
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
Your answer
Follow this Question
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