Greetings.
I’m trying to have my player change position relative to pivot point.
(pivot point sounds like the easiest way to flip the player the way I want.)
This is how my player acts at the moment. His pivot point is placed
at the red circle, and he rotates around it with the script at the bottom.
Player image 1
I’m trying to have him stay in position, while flipping the sprite and pivot point,
making him rotate in the position relative to the rotation he already is at:
Player image 2
This is the movement script I have now, and it does not work. Most likely it does
not work for obvious reasons, but I can’t seem to figure out why he doesn’t respond to key clicks.
Is there a way to “easily” achieve the kind of movement I’m after, or is pivot changing a lot harder than I’m hoping? Is it done through something entirely different than pivot?
Ideally you should have your pivot on the rotation point of your character, or in this case in the middle of the bug.
Pivot point is not something you can “flip” without flipping the entire sprite.
If all you wish is to have that bug follow a circle, then create a parent gameobject for this sprite and offset your sprite gameobject by the radius. Then you can rotate the parent gameobject and flip the sprite separately.
I wish to have my bug-thing circle around a point, and if the player clicks a button, the circle switches over to the other side of the player.
I’m trying to illustrate this here. The red circle is how he now moves, and if the player clicks on space, the bug switches his rotation to be ‘the opposite’ of his placement.
So let’s say if the player clicks while he is at the bottom of the circle, it will now flip and he is at the top.
It sounds to me like you’re almost there already. This sprite is a bit unusual in that you want to rotate it around some point outside of where it’s drawn. Sounds like you’ve got that working already, by just moving the pivot point off to the side, and then simply rotating the sprite. So far, so good.
Now, you want to flip the sprite… but keep the cosmetic position (where it appears to the user to be on screen) to be the same. To do that, you’ll have to actually move the position of the sprite when you flip it. You need to know two things: what direction to move, and how far.
You can get the direction by taking whatever vector points from the pivot point to the bug when it’s not rotated, and applying the current transform. Then, simply scale this by the distance from the pivot point to the center of the bug, times two (since you need to move not only to the bug, but the same distance again on the other side).
Looks like a decent start. I think on line 9 you meant to set clockWise = true, right?
And rather than handling both cases, I recommend starting with just one. If you can get it to flip correctly one way, I’m sure you can then make it go the other.
I’m a little worried about the rotation in line 4. You’re telling it to rotate at 1 degree per second around the X axis, plus 5 degrees per frame around the Z axis. Is that really what you meant?
That is correct. I corrected that clockWise mistake just a minute after posting that.
I’m now trying to fix what I called weird jumping, but I’m not too sure how. I commented out the second case, as well as what was in the first one, to see how Quaternion.Euler works. Seems like that could be a suitable way to go as well, but I’m not sure.
It’s hard to pinpoint exactly what makes the the player behave strange, but i’m quite sure it’s because of how my sprite looks, and where the pivot point is. I’m going to create a color-filled box now, maybe it will help indicate how the transformvector works.
No, I don’t think Quaternion.Euler is useful here. That would just set the rotation to some fixed value, but you actually do need to flip it around.
Oh, and that’s another thing — where’s the code that actually flips the sprite? I expected to see you scaling by -1 in X or some such.
Finally, I’m still concerned about line 4. Do you really mean to rotate 1 degree per sec around X, and 5 degrees per frame (!) around Z? That doesn’t make any sense to me.
Jumps the player that distance to the right, even when the pivot point is “randomly placed.”
That’s why I didn’t understand the jumping in the beginning;
it rotated around the corner, while jumping toward the right.
Since there is no “Vector3.NorthEast” I can only assume I should pivot it on the left, and have the sprite directly on the other side.
I’m not sure. I want the movement to be continuous, making the player always circle around the pivot until space is clicked. Should I rigidbody.addforce or something instead?
OK, you’ve inspired me to try this here. Give me a few minutes. But as for line 4: I think you just want to rotate around Z, so it should look something like transform.Rotate(0, 0, 5 * Time.deltaTime) (which rotates at 5 degrees per second around Z).
using UnityEngine;
public class BugRotateTest : MonoBehaviour {
public float radius = 0.23f;
void Update() {
transform.Rotate(0, 0, 120 * Time.deltaTime * transform.localScale.x);
if (Input.GetKeyDown(KeyCode.Space)) Flip();
}
void Flip() {
transform.position += transform.TransformVector(Vector3.right) * radius * 2;
transform.localScale = new Vector3(-transform.localScale.x, 1, 1);
}
}
Note that I had to take the flip direction (stored in transform.localScale.x) into account when rotating. Otherwise, it’s all pretty much as we discussed.
This is fun — by controlling your timing of the spacebar, you can make this little guy swim around wherever you want. It’s a unique control system. Thank you for posting about it!
Thank you for the help.
And that’s why I was skeptical about asking for help. It sounded like a cool way of moving the player, and I was/is slightly “scared” that other more skilled people will run away with the idea.
No, mine is butter-smooth. I think any jumping would come from an incorrect “radius” value, which determines how far we move the pivot point. Just tweak that until the sprite appears to stay in exactly the same place when he changes direction.
And don’t worry, I certainly won’t steal your idea. It’s a great one, but I have plenty of great ideas of my own.
Yeah, I guess it’s just a “newbie” kind of thing, thinking everybody is after my idea, when probably no one really is. I fixed the skipping, it was the radius. Thank you so much for your help. You helped me get to the point where I might not be able to sleep because I want to work on this.