I have a character who is able to move along flat (plane) and round (Not just spheres) surfaces based on WASD inputs and can move towards the direction that the camera is facing. But I am unable to rotate the player in the moving direction when the player is on the sphere.
Unfortunately my laptop is having problems and I can’t record a video of my problem, but I found a thread on gamedev.net where they had a similar problem without any following solutions. This is the link to the video that they had, which is the same issue that I’m having (rotationProblem - YouTube ‘around the 1:30 mark’)
I also tried the solution given in the forum - Problem to rotate player on a sphere/planet by mouse click , but it didn’t fix my problem.
I’d start from some tutorials, see how they attack the problems.
This guy makes really good stuff:
EDIT: I just pulled down the source from this, the code and the assets, and put it all together and it all worked perfectly. You must make a “Player” tag and flag the planet, and make a “Ground” layer (not tag!) and set the ground mask. Flawless work this Sebastian guy does!
Hi Kurt, I’ve actually followed this tutorial and some other tutorials and they work fine, my player is able to walk around the planet with the correct orientation and moves in the direction the third-person camera is facing. The problem arises when I try to rotate the player in the direction the camera is facing/player is moving. I hadn’t realized that while on a sphere, rotating the player on the .y axis, affects the .x and .z rotations to change as well.
So far I didn’t have any luck finding any tutorials for player movement on spheres using a third-person camera controller with free look enabled.
I’m assuming your character’s local Y axis pointing away perpendicular from the sphere’s surface. If that’s true, you can rotate your character around that using the localRotation. This way you don’t have to calculate the rest. Of course this may not work in some situations I guess.
Hey @ , That is correct, the characters local Y axis is always (and needs to be) pointing away perpendicular from the sphere’s surface. I will try what you’ve mentioned and see if I have any luck with that.
I’m running that Sebastian Lague spherical thing above and this is exactly what it does: the local +Y of the character is always away from planet center, driven by the .Attract() method on the GravityAttractor when the player calls it.
Yes @Kurt-Dekker , I do not have a problem with the gravity. The character gets attracted to the planets surface in the correct orientation. But the problem problem is that when the character is moving/when an input is given, I try to rotate the character to face the direction that the cine-machine free look camera is facing. When I do that, the characters rotation goes nuts (Most times not even facing the center of the planet) and goes back to normal when I’m not pressing any keys. This video shows the problem that I’m having (rotationProblem - YouTube)
Not sure what interface(s) you’re using in Cinemachine, but it looks to me like you are not telling Cinemachine of the new up direction.
Outside of Cinemachine, the transform.LookAt(position); API actually implies something else: Every time you call the above, you are actually doing:
transformm.LookAt( position, Vector3.up);
So they handily assume that 99% of people don’t need that second argument (because most games are flat world) and supply it when you don’t.
But you CAN give LookUp() a different “up” as its second argument and it will handle it.
Cinemachine will have exactly the same thing, I’m just not sure how to tell it “hey, this is my new UP direction,” but I am confident it exists in there somewhere. It might require enabling some other “Free look” mode for instance. I’m just not familiar enough with Cinemachine.
I had no idea that transform.position takes a second argument, I look into it. This is my first time using Cinemachine actually. And I think what you mean by the ‘Up direction’ of cinemachine is the “World Up Override” in the cinemachine right? If so, I’ve given the characters transform for the field.
I managed to identify the forwards direction that the character should be facing using another empty game object (lets call it lookAt) which rotates around to look at the forwards direction of the Cinemachine camera. On the positive side, the .z axis of lookAt always points in the direction I need to be moving, but the .x and .y axis rotates around unpredictably.
Is there a way I can rotate the character only on the .y axis, to face the .x axis direction of lookAt, ignoring the other two axis of lookAt?
Most of what I’ve tried makes the character rotate around on all axis. I tried making another empty game object and placed it at the position of the character with a slight offset in the .y axis towards the lookAt direction. Then I used the transform.LookAt() function to make the character look at that object. This did solve the rotation…kinda, but the character movement is jittery and the character tilts a bit from the direction of gravity while moving.