Rotating a parent gameobject - c#

I know this question is a duplicate but I’ve tried all the other solutions and can’t find a way to work this out. I have a bunch of cubes set up in unity in a circle, and my goal is to rotate them collectively when someone holds the mouse down and then snap to a certain position. Since there are 20 cubes, I need it to go to the nearest 18 degrees (360/20) so that there is a cube in the same place as in the original circle, just that the whole circle is turned.
I have gotten the cubes to rotate together but when I try to snap them to the nearest 18 degrees it snaps to a seemingly random spot. I have tried using different methods and I don’t know where I am going wrong! Please help!
This is my code controlling the rotation and then trying to snap it after the user has let go (the circle is the parent of all the cubes):
GameObject circle;
void Update(){
if (circle != null) { //if the circle has been assigned, rotate it
circle.transform.Rotate(cam.up, -Input.GetAxis("Mouse X") * rotationSpeed, Space.World);
}
else if (Input.GetMouseButtonDown(0)) { //if clicked, assign the circle
circle = this.GameObject.transform.parent;
}
if (circle != null && Input.GetMouseButtonUp(0)) { //supposed to snap to the right spot, this is the part that isn't working
var rotate = circle.transform.rotation.y;
var newRotate = Mathf.Round((rotate * 360) / 18) * 18;
if (newRotate > 360) { newRotate -= 360; }
else if (newRotate < 0) { newRotate += 360; }
circle.transform.eulerAngles = new Vector3(0, newRotate, 0);
}
}

Many things seems weird.
In the code you show, you assign circle but you never use it. why do you need it?
Are you rotating the parent of these 20 cubes, because that is what you should do but it is not clear in your code
You use the y value of a quaternion when you do layer.transform.rotation.y. Try layer.transform.eulerAngles.y.

Related

How to control an object with a finger in Unity3D?

I'm new to programming. I am writing a simple game (I think many people know it under the name "Ping Pong") for smartphones. A game for two people. I need the screen to be divided into two areas. Everyone within their area should be able to control the object with their finger along the x axis. Accordingly, the object changes its position only along the x axis.
here's my game :)
I was able to write this script myself:
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) * new Vector2(1, 0);
Vector2 mousePosition2 = mousePosition + new Vector2(1, -4.74f);
transformPosition1.transform.position = mousePosition2;
This script changes the position of the object relative to the x-axis of the mouse. But I do not know how to limit its area and divide it into two players. Thank you in advance for your help. I apologize for my English. Please do not use complex language constructions in the response.
One way would be putting one image to top half of the screen and other to bottom half and assign EventSystem to handle dragging.
Other way is to calculate the mouse position to see if it is within the top half or bottom half.
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float y = mousePosition .y;
if (y > Screen.height / 2 && y < Screen.height)// top half {
// move the top side player
}
if (y > 0 && y < Screen.height / 2)// bottom half {
// move the bottom side player
}

How do i rotate an object multiple times? [duplicate]

This question already has answers here:
Rotate GameObject over time
(2 answers)
Closed 5 years ago.
So I am trying to rotate an object on key press (if I press "a" rotate left by 90 degree, if I press "d" rotate right by 90 degree).
I can't make it to work, I have tried a few things but I ended up not solving the problem. As you will see in the next example I only figure out how to rotate TO 90 degree, not more or less, I actually want to rotate the object BY 90 degrees, from whichever degree was currently on. For example:
from 90 degree to 180 degree
from 23 degree to 113 degree and so on...
here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModuleBehaviour : MonoBehaviour {
public bool rotating = false;
private void Update()
{
if (rotating)
{
RotatePlatform();
}
}
private void RotatePlatform()
{
Vector3 target = new Vector3(0,0,90);
if (transform.eulerAngles.z > 90)
{
transform.rotation = Quaternion.Euler(target);
}
transform.Rotate(target * Time.deltaTime);
}
}
(Don't bother about the rotating variable, I am changing it in another script, it get's true whenever I press the "d" key. I only need to know how to rotate in the right direction, after that I will figure it out how to apply for left rotation)
Thank you;
This will do what you want:
private IEnumerator RotatePlatform(float dir) {
Vector3 target = new Vector3(0, 0, (transform.eulerAngles.z + dir + 360) % 360);
while(Mathf.Abs(transform.eulerAngles.z - target.z) >= Mathf.Abs(dir * Time.deltaTime * 2)) {
transform.Rotate(new Vector3(0,0,dir) * Time.deltaTime);
yield return null;
}
transform.eulerAngles = target;
rotating = false;
}
The key things to take away from this is that I encapsulated the "make it animate" code into a coroutine so that the object rotates to the desired direction from any starting rotation to any rotation in either direction. The reason this works is that the coroutine's local variables hold their state between iterations (the Unity scheduled task system will halt executing the code on a yield instruction and resume at a later time: in this case, null means it will resume on the next frame). This way we can store the starting rotation and target rotation without having to do anything crazy.
It does this by getting the current rotation, adding the rotation amount, then making that value lie within 0-360: transform.eulerAngles.z + dir + 360) % 360. transform.eulerAngles.z will already be [0,360], subtracting 90 from that would leave [-90,270], then by adding 360 we insure that the value will always be greater than 0 (without affecting the resulting angle), then % 360 effectively subtracts off any excess quantities of 360.
The Mathf.Abs(transform.eulerAngles.z - target.z) >= Mathf.Abs(dir * Time.deltaTime * 2) statement checks to see if the "distance we have yet to rotate" is "greater than the amount we're going to rotate by" with some buffer: we don't want to overshoot and have our "how far do we need to go" be 358 degrees!
The other important change was that we want to use new Vector3(0,0,dir) rather than target as our value to rotate by so that the speed remains constant.
The last 2 instructions make sure the rotation actually achieves the desired value and, of course, toggle back the bool tracking whether or not pressing keys does anything (we only want 1 instance of our coroutine).
Then here's the Update() method I used to control it:
private void Update() {
if(!rotating) {
if(Input.GetKeyDown(KeyCode.D)) {
rotating = true;
StartCoroutine(RotatePlatform(90));
}
if(Input.GetKeyDown(KeyCode.A)) {
rotating = true;
StartCoroutine(RotatePlatform(-90));
}
}
}
To rotate in the other direction, subtract 90.
void Update()
{
if (Input.GetKeyDown(KeyCode.D))
transform.Rotate(0, 0, transform.rotation.z + 90);
}

How to rotate a parent object while rotating a child object to a certain angle using coroutines

I have a semicircle like game object which I made by putting two arcs in an empty game object (SCircle) and rotating the 15° (for left arc) and -15° (for right arc) as seen below.
SCircle has an Orientation enum with two valuesLeft (rotates SCircle to 45°) and Right (rotates SCircle to -45°) as seen in the image below.
I use the following coroutine to move SCircle between orientations.
IEnumerator RotateLeftOrRight(Vector3 byAngles, float inTime)
{
Quaternion fromAngle = gameObject.transform.rotation ;
Quaternion toAngle = Quaternion.Euler (transform.eulerAngles);
if (circOrientation == Orientation.Left) {
toAngle = Quaternion.Euler (gameObject.transform.eulerAngles - byAngles);
circOrientation = Orientation.Right;
}
else if (circOrientation == Orientation.Right) {
toAngle = Quaternion.Euler (gameObject.transform.eulerAngles + byAngles);
circOrientation = Orientation.Left;
}
for(float t = 0f ; t <= 1f ; t += Time.deltaTime/inTime)
{
gameObject.transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t) ;
yield return null ;
}
gameObject.transform.rotation = Quaternion.Lerp(fromAngle, toAngle, 1);
}
I also used a very similar coroutine to move the individual arcs by 30° (in opposite directions) from say, Orientation Left, as seen below in the coroutine and image:
IEnumerator RotateArc(GameObject arcGO, Vector3 byAngles, float inTime)
{
Quaternion fromAngle = arcGO.transform.rotation ;
Quaternion toAngle = Quaternion.Euler (arcGO.transform.eulerAngles);
if (rightArc.arcOrientation == Arc.Orientation.Down) {
toAngle = Quaternion.Euler (arcGO.transform.eulerAngles + byAngles);
rightArc.arcOrientation = Arc.Orientation.Up;
}
else if (rightArc.arcOrientation == Arc.Orientation.Down) {
toAngle = Quaternion.Euler (arcGO.transform.eulerAngles - byAngles);
rightArc.arcOrientation = Arc.Orientation.Up;
}
for(float t = 0f ; t <= 1f ; t += Time.deltaTime/inTime)
{
arcGO.transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t) ;
yield return null ;
}
arcGO.transform.rotation = Quaternion.Lerp(fromAngle, toAngle, 1);
}
Since SCircle Coroutine is activated by a mouse click, I have the case where the individual arcs coroutine is run and before it is complete the parent SCircle coroutine is also run. In this case the arcs end up moving from Left to A, which is not the behavior I need. I would want the behavior of them ending up at B when moving from the Left. Likewise, from B, when the SCircle coroutine is run while the arcs coroutine is in progress the orientation will return to the Left.
Please note that the blue arrow represents the movement of the left Arc, the red represents the right Arc and the black represents movement of SCircle - the parent object.
I believe the issue you're having is caused by the fact that as you rotate the individual arcs, what they're rotating around is also rotating itself.
In other words, your SCircle's axes are rotating, and your arcs are using those same axes to determine their own rotations. So if you look at your example A, you tell it to rotate in world space by -90, then immediately tell to rotate in world space by 30. To solve this, you want to get the arcs to rotate locally, not in world space.
I think you can solve this by setting up this structure:
Empty GameObject SCircle
------Empty gameObject BlueArcCenter -> position 0,0,0
-----------blue arc -> change position here to make it offset from zero ex:(radius, 0, 0)
------Empty gameObject RedArcCenter -> position (0,0,0)
-----------red arc -> change position here to make it offset from zero ex:(-radius, 0, 0)
Now alter your code so that:
To rotate the blue and red arcs individually you rotate their arcCenters,
To rotate the whole configuration, rotate SCircle. This will in turn rotate both ArcCenters, which will in turn rotate both the arcs irrespective of how they are oriented relative to each other, or whether they are rotating themselves.
Note:
Also, I've read that people are using Quaternions a lot less these days since they don't really make sense when you read the code. You might want to change your code to normal rotations to make it more readable.
If you have the setup listed above, use something like:
to spin circle:
thisSCircle.transform.Rotate(0, -90, 0, Space.Self);
to rotate arcs around their common center:
blueArcCenter.transform.Rotate(0, -30, 0, Space.Self)
redArcCemter.transform.Rotate(0, 30, 0, Space.Self)
(you'd need to add lerps in and stuff but this should get you started).

Show object in front of the player always

I have stuck in this simple problem but unable to understand that why i am unable to control it.
I have these line of code which is displaying my canvas object in front of my player(camRotationToWatch object name in code) at certain rotation of the player.
if (camRotationToWatch.transform.localEulerAngles.x >= navigationCanvasXMinmumLimit && camRotationToWatch.transform.localEulerAngles.x <= navigationCanvasXMaximumLimit)
{
if (!navCanvasHasDisplay)
{
navigationCanvas.SetActive(true);
//Debug.Log(camRotationToWatch.transform.forward);
Vector3 navCanvas = camRotationToWatch.transform.position + camRotationToWatch.transform.forward * navCanvasDisplayDistanceFromCam;
navCanvas = new Vector3(navCanvas.x, 2f, navCanvas.z);
navigationCanvas.transform.position = new Vector3(navCanvas.x, navCanvas.y, navCanvas.z);
navigationCanvas.transform.rotation = camRotationToWatch.transform.rotation;
navCanvasHasDisplay = true;
}
}
else
{
//navigationCanvas.SetActive(false);
if (locationPanel.activeSelf == false && infoPanel.activeSelf == false) {
navigationCanvas.SetActive(false);
navCanvasHasDisplay = false;
}
}
This code is actually work fine when camRotationToWatch object rotate from down to up and Canvas show at correct position but as I try to to rotate camRotationToWatch from up to down it display(active) Canvas at very top position. How can I restrict canvas to show at same position (No matter player rotate from up to down or down to up) but display on front of the player object?
Kinda hard trying to figure out what exactly you want to do. But this did what I think you where trying to do
public GameObject follow; // The object you want to rotate around
public float distance = 2; // Distance to keep from object
private void Update() {
Vector3 forward = follow.transform.forward;
forward.y = 0; // This will result in Vector3.Zero if looking straight up or down. Carefull
transform.position = follow.transform.position + forward * distance;
transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
}
I believe your "unexpected behavior" is due to the use of euler angles since they are not always entirely predictable. Try using Quaternions or Vector3.Angle() when possible.
If you want to limit the angle (say... if looking down or up more than 45° disable the object) you could do the following:
if (Vector3.Angle(forward, follow.transform.forward) > maxAngle) { ... }
This probably isn't a complete answer but something that might help. This line:
Vector3 navCanvas = camRotationToWatch.transform.position + camRotationToWatch.transform.forward * navCanvasDisplayDistanceFromCam;
You are creating a position at a fixed distance from camRotationToWatch. But if that object is looking up or down, that position is not horizontally at navCanvasDisplayDistanceFromCam. If it's looking straight up, then this position is in fact directly above.
So when you do this to set a fixed vertical height:
navCanvas = new Vector3(navCanvas.x, 2f, navCanvas.z);
you aren't getting the distance from camRotationToWatch that you think you are.
Instead of using camRotationToWatch.transform.forward, create a vector from it and zero out the Y component, and normalize before using it to offset the position. (You will need to watch out for zero length vectors with that though).
Whether that fixes your problem or not, it's too hard to guess but it should help improve your results some.
EDIT: Here is an example of how you can avoid the issue with the canvas being too close:
Vector3 camForward = camRotationToWatch.transform.forward;
camForward.y = 0;
if (camForward.magnitude == 0)
{
//TODO: you'll need to decide how to deal with a straight up or straight down vector
}
camForward.Normalize();
//Note: now you have a vector lying on the horizontal plane, pointing in
//the direction of camRotationToWatch
Vector3 navCanvas = camRotationToWatch.transform.position + camForward *
navCanvasDisplayDistanceFromCam;
//Note: if you want the canvas to be at the player's height (or some offset from),
//use the player's y
navCanvas = new Vector3(navCanvas.x, Player.transform.y, navCanvas.z);
navigationCanvas.transform.position = navCanvas;
Again, this might not fix all your issues but will help to ensure your canvas lies at a set distance horizontally from the Player and will also compensate for the player's up and down motion.

mouse picking a rotating sprite in an Xna game

I am working on a simple game where you click on square sprites before they disappear. I decided to get fancy and make the squares rotate. Now, when I click on the squares, they don't always respond to the click. I think that I need to rotate the click position around the center of the rectangle(square) but I am not sure how to do this. Here is my code for the mouse click:
if ((mouse.LeftButton == ButtonState.Pressed) &&
(currentSquare.Contains(mouse.X , mouse.Y )))
And here is the rotation logic:
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
RotationAngle += elapsed;
float circle = MathHelper.Pi * 2;
RotationAngle = RotationAngle % circle;
I am new to Xna and programming in general, so any help is appreciated.
Thanks a lot,
Bill
So you're trying to determine if a point is in a rectangle, but when the rectangle is rotated?
The Contains() method will only work if the current rotation is 0 (I guess currentSquare is a rectangle representing the image position without rotation?).
What you will have to do is do the opposite rotation of the image on the mouse coordinates (the mouse coordinates should rotate around the origin of your image), then calculate if the new position is within currentSquare. You should be able to do all of this using vectors.
(Untested)
bool MouseWithinRotatedRectangle(Rectangle area, Vector2 tmp_mousePosition, float angleRotation)
{
Vector2 mousePosition = tmp_mousePosition - currentSquare.Origin;
float mouseOriginalAngle = (float)Math.Atan(mousePosition.Y / mousePosition.X);
mousePosition = new Vector2((float)(Math.Cos(-angleRotation + mouseOriginalAngle) * mousePosition.Length()),
(float)(Math.Sin(-angleRotation + mouseOriginalAngle) * , mousePosition.Length()));
return area.Contains(mousePosition);
}
If you dont need pixel pefect detection you can create bounding sphere for each piece like this.
var PieceSphere = new BoundingSphere()
{
Center =new Vector3(new Vector2(Position.X + Width/2, Position.Y + Height/2), 0f),
Radius = Width / 2
};
Then create another bounding sphere around mouse pointer.For position use mouse coordinates and for radius 1f. Because mouse pointer will be moving it will change its coordinates so you have to also update the sphere's center on each update.
Checking for clicks would be realy simple then.
foreach( Piece p in AllPieces )
{
if ((mouse.LeftButton == ButtonState.Pressed) && p.BoundingSphere.Intersects(MouseBoundingSphere))
{
//Do stuff
}
}
If you are lazy like me you could just do a circular distance check.
Assuming mouse and box.center are Vector2
#gets us C^2 according to the pythagorean Theorem
var radius = (box.width / 2).squared() + (box.height / 2).square
#distance check
(mouse - box.center).LengthSquared() < radius
Not perfectly accurate but the user would have a hard time noticing and inaccuracies that leave a hitbox slightly too large are always forgiven. Not to mention the check is incredibly fast just calculate the radius when the square is created.

Categories

Resources