I have been trying to figure this out for quite some time now, and I’m banging my head over it. Here is a picture to show you what I’m trying to do:
The red block is supposed to rotate around the black ball using the left and right arrow keys. When it hits the black line, it is supposed to stop rotating. I have been trying this for some time, but I can’t quite figure it out. Any help?
Try using Transform.RotateAround(). Give it the position of the circle, Vector3.up (I’m assuming) as the axis, and the rate of rotation. To control the direction, assign a negative value to either the rate of rotation (the third parameter in the function) or the axis (second parameter). Either one will get opposite results based on positeve or negative values. So, test the input. If it is left arrow, make the rate of rotation positive, and if it is the right arrow, make it negative (might be backwards on that). Only thing left is to test for collision between the block and the line. When there is collision, either don’t execute the code that rotates, or execute it with a 0 rate of rotation.
I probably should have said this too:
When I press a arrow key (left or right), it will move until you let go. This does not necessarily mean you press the button, and it stops when you hit the line. The thing you explained above makes it rotate non-stop.
EDIT:
I fixed it. I just put the Input.GetAxis(“Horizontal”) in the place of the rotation angle. Thank you for your help ![]()
Yes, and you can multiply that Input.GetAxis value by a desired rotation speed to control how fast it will rotate:
Input.GetAxis("Horizontal")*desiredRotationSpeed*Time.deltaTime