Sorry, I’m back here again. I feel really annoyed at myself because my problem’s solution must be so simple and yet I can’t figure it out. I’ve tried looking online but nothing works. I’m obviously doing something wrong.
I want to move one object’s position and another object’s rotation when a Boolean is set to true (the Boolean toggles between true and false using Raycast and mouse click). However, when I click the object, nothing happens, which is annoying, obviously. As the script is attached to the parent of the child objects I want to move, I have to get a reference to the child objects in the script, which I tried and failed to do. It would be good to get this script working, and I know the solution is probably really easy, but I can’t think straight today. Could you take a look at my script and screenshots and see if I’m missing anything or should do it differently? I really appreciate it.
public class CDTrayAnimate : MonoBehaviour {
public GameObject cdDriveTray;
public GameObject cdDriveFlap;
public Vector3 trayPositionOpen;
public Vector3 trayPositionClosed;
public RaycastHit hit;
bool isOpen;
public Transform cdTray;
public Transform cdFlap;
// Use this for initialization
void Start () {
hit = new RaycastHit ();
//GameObject cdDriveTray = transform.Find ("tray").gameObject;
//GameObject cdDriveFlap = transform.Find ("disk tray flap").gameObject;
cdTray = transform.Find ("tray");
cdFlap = transform.Find ("disk tray flap");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f)){
SwingUp();
TrayPosition();
}
}
}
void SwingUp()
{
if (isOpen) {
Quaternion newRotation = Quaternion.AngleAxis (90, Vector3.up);
//cdDriveFlap.transform.rotation = newRotation;
cdFlap.transform.rotation = newRotation;
}
}
void TrayPosition()
{
if (isOpen) {
//cdDriveTray.transform.position = trayPositionOpen;
cdTray.transform.position = trayPositionOpen;
}
}
public void OnClick()
{
isOpen = !isOpen;
}
}
if you put a debug.log(“I hit something”);
does it print?
if (Physics.Raycast(ray, out hit)){
Debug.Log("I hit something");
SwingUp();
TrayPosition();
}
if it doesn’t print then verify you have a collides on the object you clicking on. and that you have it on a layer that is not ignoring raycasts. i would also remove 100f. this is limiting the distance of the ray.
My object has a collider, and I used Debug.Log and it does show up in the Update() method. I tried changing transform.Find to transform.FindChild but no difference. I tried finding the child objects by doing “parent object/child object” (as an example) but it came up with an error when I clicked on the object saying there wasn’t an instance of an object or something like that.
Well, I’ve found something interesting. The object that moves position does indeed move position when clicked - however it moves to a completely different place to the one I set. Hmmm. Why is it doing that?
Glancing at this thread, if you’re attempting to move a child gameobject and setting a value that looked familiar in the inspector, try to adjust its localPosition instead of position.
All I can really say is that you didn’t type it wrong lol.
How did you come up with these Vector3 variables? Did you move it how you want in the editor, and then assign that value ?
What I did was in the editor, I moved the object to the target position I wanted then made a note of the xyz variables, and then used this for the Vector3 variables. I moved the object back to its default position afterwards.
Ya, that makes sense & is what I thought you would have done. And this is a child gameobject , right?
The values you wrote down would have been local position(s), (unless of course you unparented it first).
For fun, you could (just for a sanity check) write a test script that sets the localPosition to the Vector3 that you want in Start(), put it on the cdTray and run the game once and see what happens?
Sure. I mean if you try that suggestion you can at least see what happens. It should set it to the position that you want. If it does, then there must be some other issue? This was kind of a means to check that
@methos5k thanks for helping with this. I got caught up this weekend and wasn’t able to follow up.
@Purple-Night-Owl_1 So this should like an easy one. you have the two positions. public Vector3 trayPositionOpen; and public Vector3 trayPositionClosed; I don’t know the scale of the objects or where the center points are. but all you need to do is Play the scene. Grab the CD tray, write down it’s trayPositionClosed transform postion. then manualy move the tray to the trayPositionOpen position. and write it down. then stop the scene. then put in the numbers you wrote down.
change the code the use transform.localPosition
I don’t see any code that is using the Onclick function. when does this get use? If you have it set in an event trigger. that might be why it’s not moving. put the OnClick in the raycast and take it out of the event trigger. at least for testing.
public void OnClick()
{
isOpen = !isOpen;
}
if (Physics.Raycast(ray, out hit)){
Debug.Log("I hit something");
SwingUp();
OnClick();
TrayPosition();
}