- Home /
Rotational Gravity - Camera and Force problems
I'm trying to emulate the rotational gravity that would exist on a cylindrical space station as such
I am attempting to do so buy this method...
find the player z, then draw line from the center of cylinder with the same z to the player. Apply force in that direction then rotate player with the cylindrical center defined as up
public class rotationalGravity : MonoBehaviour {
public GameObject player;
Vector3 middlePoint;
Vector3 playerV;
Vector3 playerPos;
Vector3 forceDirection;
Rigidbody playerRidge;
void Start () {
playerV = player.transform.position;
middlePoint = new Vector3 (0, 0, playerV[2]); // sets middle point to have same z as player
playerRidge = player.AddComponent<Rigidbody>(); // causes player to shoot up for some reason
}
void Update () {
playerPos = new Vector3 (playerV [0], playerV [1], playerV [2]);
middlePoint.z = playerV [2];
forceDirection = middlePoint + playerPos; // gets direction to apply force?
//Physics.gravity = forceDirection;
player.transform.Rotate (Vector3.forward * Time.deltaTime); // another way i was trying to rotate player
playerRidge.transform.forward = Physics.gravity; // trying to rotate player around the center of cylinder
playerRidge.AddForce(-forceDirection * Time.deltaTime); // supposed to apply force to player in the direction from the middle
When it runs the player(which is the default FPS Controller) just floats up and passes through everything.
The camera rotation is way off too. Do I need to make changes to the FPS Controller to get the camera and controls to rotate correctly? And what am I doing wrong in trying to apply the force to the player? This project is being made to learn a lot of the ropes.
EDIT: I'd rather implement a system of this style rather than rotate the cylinder as down the line there will be multiple enemies and potential a second player.
The FPS controller will not handle arbitrary rotations well. Consider rotating your camera and cylinder rather than the character.
If you were not trying to integrate this into the FPS controller then I could certainly help you with this, but as robertbu says this adds many complications...
@ $$anonymous$$rSoad if I went with robertbus idea, how would you suggest doing it?
you could try rotating the gravity itself within "FPS" Controller, that's way you can have enemies pursuing/Ambushing you. then doing a check to make sure the up direction is the opposite direction of gravity....(then snapping or very quickly rotating the character until it is)
Ever played Ratchet & Clank series? they have these scenarios quite often, but even more complex, you might be able to learn a bit how they work by testing it...I've learnt a lot form just testing other games features then replicating their behavior in unity.
you basically need to rewrite FPS controller/Character $$anonymous$$otor to allow it, as Robertbu said, it currently won't handle them well... (if at all)
As for rotating the cylinder, that causes potential problems such as viewing another section of the level while inside it.
Sorry can't help with that, I would write my own character controller script from scratch, starting with basic movement, then applying the cylindrical gravity code, then working through the rest of what the FPS controller does and implementing that in a way that works with my gravity character controller...
(Edit : What Fornoreason1000 says about playing games and trying to recreate their mechanics is spot on! This is a great way to learn. I would not rotate the cylinder as you will learn far more the other way, also rotating the cylinder can make the enemy scripting harder. If you get it working for your player(as is) then much of your player script will be transferable to the enemy scripts.
Are you going to have more than just one cylinder section and if so how do you plan on linking them? )
Answer by MrSoad · Nov 01, 2014 at 12:59 AM
Ok, if you are going to go with my way then start simple! Then build upon these working foundations.
For some info on how to align your player rotation for your current player position in relation to your gravitational pull/push vector position take a look at my answer to this :
http://answers.unity3d.com/questions/819101/self-levelling-a-gameobject.html
You will be pushing away rather than pulling toward!!! This script shows you how to keep the player object correctly aligned in terms of rotation for you current position in relation to the gravitational position vector. You will still need to apply your gravity force in the right direction for current position in relation to your gravity point position.
For the curved connecting sections it will probably be best to model the curve connections in a 3D package such as Blender. You will put the anchor point of the connection mesh section at the point where you want the gravity calculations to adhere to. Then use a trigger script to switch between the cylinder section central line gravity point and the connection(semi spherical) gravity point.
Once you have this working you can then start to reintroduce the complex FPS controller attributes.
As stated above, if done this way then apart from the enemy object AI(relating to intelligent movement) the basic scripts that you write here for gravity etc will be directly transferable to your enemy objects.
Hope this helps :)