Problem with rotations for moving between one rotating object to the other [C#]
Hello, I’ve been struggling with a problem recently. I’m making a game that is mainly about jumping from one rotating circular object to the other. The problem is the rotation. I want the player to fly to the other object and then invert himself so that it will look like he’s standing on that object. The player automatically gets parented by the “planet” so i was trying to use Transform.localRotation like so: private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Platforms") { transform.SetParent(collision.transform); //setting the parent transform.localRotation = Quaternion.identity; //trying to nullify the rotation, based on the parents rotation }
A snippet of code, where the rotation and parenting happens. When the player is moved again, the player object unparents itself. The result: http://prntscr.com/f2ican
The rotation seems to be random, so it must be based on the parent’s rotation. The player has a rigidbody attached to it. Thank you for any and all feedback :).
Answer by jdean300 · May 02, 2017 at 03:27 PM
I'm gonna guess and say that this is because the circles in the picture you gave have a rotation value. Being circles, you can't really see it, but they have one none the less. So the circle is rotated some angle, let's say 45 degrees. When you set to localRotation to Quaternion.identity, that has the same effect as giving the player the rotation of the parent - in this case the 45 degrees.
What you could do is get the vector from the center of the circle to the player and then align your characters up vector with it:
Vector3 circleNormal = transform.position - circle.transform.position;
Quaternion rot = Quaternion.FromToRotation(Vector3.up, circleNormal);
transform.rotation = rot;
I love you, worked almost perfectly! I just had to change both of the "Vector3" to "Vector2" and it worked :D current code: transform.SetParent(collision.transform); Vector2 circleNormal = transform.position - collision.transform.position; Quaternion rot = Quaternion.FromToRotation(Vector2.up, circleNormal); transform.rotation = rot;
There is another question, ill post it here, since it's also related to rotations (i think). There is this gap, when the sprite has been rotated, ex.: http://prntscr.com/f3a7v0 Object's pivot is set to bottom middle, as such: http://prntscr.com/f3a8kp
That the circle normal that we got earlier, normalize it, and multiply by the circle radius. That should be the players position.
transform.position = circlePosition + circleNormal.normalize * circleRadius;
i hope I'm getting the radius of the circle correctly, but I'm getting an error saying: "Operator "*" cannot be applied to operands of type 'method group' and float" The bit of code, that does the calculations:
Vector2 circleNormal = transform.position - collision.transform.position;
Quaternion rot = Quaternion.FromToRotation(Vector2.up, circleNormal);
transform.rotation = rot;
float rad = collision.GetComponent<CircleCollider2D>().radius;
transform.position = circleNormal.Normalize * rad;
Ahh, you that's because circleNormal.Normalize
is trying to call the Normalize method, which means you need parenthesis: circleNormal.Normalize()
. But, that will change the circleNormal vector to be length one. Unless you need that normalized value again, you should probably just use circleNormal.normalized
which keeps the vector the same but give you the normalized one.
Just a point of caution, I don't think collision.GetComponent<CircleCollider2D>().radius
is going to give you the correct radius unless the scale of the circle object is (1,1). If that is not the case, and the circle is scaled uniformly, you could just do collision.GetComponent<CircleCollider2D>().radius * transform.localScale.x
No errors, but the player is now further from the circle and when he moves to another circle, the distance between the player and the circle instantly becomes enormous (it seems that the further the platform is from the first circle, the bigger the gap is), like so: http://prntscr.com/f3cxyo ( this is quite a bit away from the start, the gap is so huge, the player is out of bounds completely). This could mean, that some of the variables do not reset... here is the full script, maybe you could see where.
Ahh, there may have been a mistake in what I told you earlier. transform.position = circleNormal.normalized * rad;
should be transform.position = collider.transform.position + circleNormal.normalized * rad;
.
Additionally, rb.$$anonymous$$ovePosition(transform.position += transform.up * Time.deltaTime * speed);
seems strange. +=
is going to change the value of transform.position
but then the $$anonymous$$ovePosition method does that anyways. You should probably just use +
It says that the sign +
is ambiguous on operands of type Vector 3 and Vector 2. the collision.transform.position
is a Vector 3 and the circleNormal.normalized
is a vector 2.
You can make it a vector 3: new Vector3(circleNormal.x, circleNormal.y, 0f).normalized
Your answer
![](https://koobas.hobune.stream/wayback/20220612121126im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
vehicle/gameobject moving/listing/rotating when made parent of main character (child) game object 1 Answer
Cinemachine movement glitch when player is unparented from a moving platform 0 Answers
Rotating a cube on local axis c# 4 Answers
Disable rotation on swipe (Gear VR) 1 Answer
Rotate turret with multiple barrels towards a target and fire 2 Answers