- Home /
I'm trying to use find and if statements how do I do this?,Im trying to use if statements with find how do I properly do this?
Currently I have one game object thats being translated and another that's being rotated. My goal is to start rotating said object once the other object has reached a certain location using if and find statements. Below is what I have currently what should I put inside the if statement so that when my translate object reaches a certain location my object starts rotating? Thanks!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class scale : MonoBehaviour { GameObject Move; public float speed = 5f; Vector3 temp; // Start is called before the first frame update void Start() { Move = GameObject.FindWithTag ("Move"); }
// Update is called once per frame
void FixedUpdate()
{
if (position.x.Move < -4)
{
temp = transform.localScale;
temp.x += Time.deltaTime;
transform.localScale = temp;
}
}
} ,I'm very new to unity and currently, I have one game object being translated and another being rotated. My goal is to start the rotation of the second object once the first has reached a certain position using an if statement. This is what I currently have and I don't know what I'm supposed to put inside the if statement in order to say something like if the position of the object "Move" is <-5 then I can start rotating the other object. Thanks in advance!
public class scale : MonoBehaviour { GameObject Move; public float speed = 5f; Vector3 temp; // Start is called before the first frame update void Start() { Move = GameObject.FindWithTag ("Move"); }
// Update is called once per frame
void FixedUpdate()
{
if (position.x.Move < -4)
{
temp = transform.localScale;
temp.x += Time.deltaTime;
transform.localScale = temp;
}
}
}
I would look into C# program$$anonymous$$g more before going on, make a text game to get a good handle on the language, then co$$anonymous$$g back to unity will be easy.
Answer by unity_ek98vnTRplGj8Q · Feb 04, 2020 at 05:23 AM
I agree with the above comment, you do seem to be lacking some serious fundamental understandings about C# itself. Perhaps following some unity tutorials will help. I am, however, a firm believer in just trying things out, so here is the script I would use to rotate an object once the object tagged with "Move" is past a certain point, with plenty (maybe too many) comments to explain why I am doing what I am doing
//General note on C# convention - classes and functions should be capitalized
//Variables should not be capitalized
//While it seems like it doesn't matter now its a really good habit to get into because it makes your code much easier to read
public class Scale : MonoBehaviour {
GameObject move;
public float speed = 5f;
// Start is called before the first frame update
void Start () {
//Good job calling this in the start function, it is much more efficient than calling it every update frame
//Make sure you are using the correct function name however - FindWithTag is not what you want
move = GameObject.FindObjectWithTag ("Move");
}
// Update is called once per frame
// Use update here. FixedUpdate is used for physics calculations, and while not technically incorrect will just make it more complicated for you
void Update () {
//I have an object called move and I want to read its position
//In other words I want the transform that belongs to move
//Then I want the position that belongs to the transform
//Then I want the x component of that position
//This will set xValue equal to the position along the x axis of the move object
float xValue = move.transform.position.x;
//Now we can check if that value is less than our threshold value
if(xValue < -4){
//localScale contains information about the SIZE of the object
//if you want to rotate, use rotation
//in our case, instead of accessing the rotation directly we can use Transform.Rotate
//lets first figure out how much we want to rotate around each axis
Vector3 rotationAroundEachAxis = new Vector3(speed*Time.deltaTime, 0, 0);
//Multiplying speed by Time.deltaTime is good here because we will get a constant speed regardless of the frame rate we are getting
//This rotation is just a rotation around the x axis. Notice how the y and z values are zero
//Now we just need to feed these values into the function
//Notice we are calling the Rotate method that belongs to the transform component attached to this game object
transform.Rotate(rotationAroundEachAxis);
}
}
}
Thank you so much for putting in the time and effort to help me! However, I'm still having trouble I inputted the find commands you suggested to however my if statement either still triggers no matter what or doesn't trigger at all.
$$anonymous$$aybe some screenshots will help me, can you post some showing your scene and the inspector for both of your objects
Your answer
Follow this Question
Related Questions
GameObjects Array Problem 2 Answers
Resource counter doesn't reset on each iteration 0 Answers
AI cannot find closest Target 0 Answers