Rotating an object.

 if (Input.GetMouseButton(1)) {
		var RotV : float = 12 * Input.GetAxis ("Mouse X");
		var RotH : float = 12 * Input.GetAxis ("Mouse Y");
		
        if (Physics.Raycast(ray, hit, 20.0)) {
		if (hit.collider.gameObject.tag == "OriLS") {
		WasHit = 1;
		//	else if (hit.collider.gameObject.tag == "OriRS") {
		//	WasHit = 2;
		//	}
		}
		
		//Even though MouseButtonUp doesn't work for right click, this rotation command only works when the above right click is Down.
		//However, this is only executed if the above code collides. This is fine for a 6 sided room, with colliding walls, however if used outdoors
		//it may not work correctly. If any distant objects are within 20 (raycast distance) then it should work, but if not, then it may not work right.
		//If no colliders are found around the object, the cursor is restricted to the OriSide as it is technically a collider.
		//Moreover, we don't want right click to be rotating the object from anywhere in the level just by facing any generic collider.
		if (WasHit == 1) {
			LSide.transform.Rotate(RotH, RotV, 0);
			//	else if (WasHit == 2) {
			//	RSide.transform.Rotate(RotH, RotV, 0);
			//	}
			}
		
		}
	}

Read the // comments in the

 for the description of the problem.

I want to be able to rotate the object with the right click button, but don't want to keep the cursor continuously on this object and having to keep moving the mouse horizontal/vertical and then re-positioning the cursor on the object every time and rinse repeating to have the object turn.

Currently, the rotation is set up rather generically, that regardless which collider it hits, the object still turns. This means you can turn that one object from anywhere in the level by hitting and moving right click (once the WasHit is checked from the object tag and is "1").

I tried placing the the code like this, but it didn't rotate at all.
[code]
 if (Physics.Raycast(ray, hit, 20.0)) {
		if (hit.collider.gameObject.tag == "OriLS") {
		WasHit = 1;
		if (WasHit == 1) {
			LSide.transform.Rotate(RotH, RotV, 0);
			//	else if (WasHit == 2) {
			//	RSide.transform.Rotate(RotH, RotV, 0);
			//	}
			}
		}
}

Technically this bracket change would require the tag check to keep checking for rotation, but again that would keep the rotation only on the object. Either way, it didn’t work at all.

I want to be able to right click the object, move the cursor anywhere on the screen, and still only be rotating that one object that was tag checked.

Any pointers?
Cheers.

Bump?

Can you whittle this down to a precise question? The subject is too large. You need to capture the attention of fellow devs with a simple scenario. Is your problem something related to detecting mouse clicks or are you having trouble causing a rotation in a certain way?

I scanned this but it is late and I need to be spoon-fed if you want me to bite :wink:

Urgh, it’s not too hard to read.

  1. Currently right clicking an object rotates it only when mouse is on that object.
  2. I want the right click to click that object, then move the cursor off the object and still be able to rotate it if right button is held. When right button is released, you have to re-click the object to be able to rotate it.

So you want to click an object and drag to rotate it until released?

That should be your pattern then, not detecting hits each frame, if that is what you are doing (you didn’t post where the code is placed did you?)

  1. Detect click to select based on hit
  2. If dragging then activate a ‘drag mode’ and rotate based on mouse motion
  3. Stop when released.

… And it IS hard to read. The tabulation is all over the place. I suggest putting brackets on their own lines and starting all variables with a lower case letter (only functions and types are uppercase.) in the last code, it looks like you set a variable to 1 then immediately test it to see if it is 1, then you check to see if it is 2…or maybe that is commented out…

It’s placed in Update function.

Yes, I want to be able to press right click and hit the object tagged, then keep rotating towards the mouse cursor anywhere on screen while right mouse button is still being pressed and I don’t want it to keep checking the tag while it’s rotating.

As for tabulation, it’s fine understanding it in UniScite (possibly in monodevelop too?), but on the code tags in the forums it is a little hard.

Yes, I am currently right clicking the object, checks the tag, and if it is that tag then set a variable to 1. The 2 variable is for a second object, and is currently commented out til I sort out the first object. Then, if that variable is 1 and right click is still being pressed, to still rotate regardless on screen the cursor is. But, the problem is that it doesn’t matter what the ray collides with, it no longer checks the tag and if I put the if (WasHit == 1); within the tag check, it won’t work.

you can use void OnMouseOver(){} to see if the mouse is over the collider of the object you want to rotate, then inside you can check if the mouse button is pressed, set a variable true, then in update rotate based on the movement of the mouse, while that variable is true. when Input.GetMouseButtonUp you can set it to false.

Format the code you paste here. We are volunteering our time to help so you should take the time to post clear code and questions.

You misunderstood my post. Don’t use update. Take a step back from the code and design the pattern first. You only need to do something every frame if the object is ‘selected’. So, detect a click, if it is the object you want, and the mouse is dragging, start a coroutine to handle the rotation. Have the co-routine quit when the mouse is released.

Use layers so you only cast a ray against the objects you need. This is much faster than tag-checking. Only check tags against the minimum number of objects possible.

By the way.EZGUI makes this kind of thing MUCH easier because you can make a 3D object a ‘button’ and EZGUI handles all the mouse events so you have a much simpler job to perform based on pointer input. Also, it doesn’t care if you are using a mouse or a finger.