Search Unity

Faux Gravity making my brain spin... Help!

Discussion in 'Scripting' started by podperson, Feb 12, 2008.

  1. Mattolio

    Mattolio

    Joined:
    Apr 19, 2013
    Posts:
    2
    HI

    I'll paste the code which works for me on jumping (although I have it so that holding space makes the player feel like he's got a jetpack on and have some fuel variabl)

    if (Input.GetButton ("Jump") fuel > 0) {

    jumpRestRemaining = jumpRest;
    rigidbody.AddRelativeForce (Vector3.up * jumpSpeed * 100);
    fuel -= 10 * Time.deltaTime;

    if(!audio.isPlaying){
    audio.clip = jetPack;
    audio.Play();

    }
    print (fuel);

    }

    Its allot of fun! I have my character now able to move to different worlds and stuff too. Something I also enjoyed doing was checking that once I was certain distance from the ground, applying new controls which allowed me to boost around in space and explore without gravity affecting me.

    For switching to other planets, I just set up a sphere collider around the planet at a certain distance from it which felt right for the gravity to pull me in, then accessed the component of the gameObject (planet 2) so the game uses its fauxgravityattractor script instead. I have the planets attached to the player too as varibales (public GameObject Planet1; public GameObject Planet2;)

    void OnTriggerEnter (Collider other)
    {
    if (other.tag == "Planet2")
    {
    _FauxGravityAttractor = Planet2.GetComponent<FauxGravityAttractor> ();
    if(!audio.isPlaying)
    {
    audio.clip = gravitySwitch;
    audio.Play();
    }
    }

    To fly around unhindered I just set it to null once I get a certain distance away from the ground:

    If (distancetoGround > 30){

    FauxGravityAttractor = null;
    }

    Possibly all a bit hacky and not ideal for game development, but its fun to play with :)

    The one thing I am having trouble with is making it so that the player moves in the direction of the camera as you hold w. So I have a camera with mouselook and tried doing that, but it goes crazy, even though it sort of works.

    Oh! Something else that is interesting is being able to left click on a planet to make the new one. Note that mouseSelect is set up as a ray variable):

    if (Input.GetMouseButtonDown (0))
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit mouseSelect;
    if (Physics.Raycast(ray, out mouseSelect))
    {
    Debug.Log ("Name = " + mouseSelect.collider.name);
    Debug.Log ("Tag = " + mouseSelect.collider.tag);
    Debug.Log ("Hit Point = " + mouseSelect.point);
    Debug.Log ("Object position = " + mouseSelect.collider.gameObject.transform.position);
    Debug.Log ("--------------");
    if (mouseSelect.collider.tag == "Planet1")
    {
    _FauxGravityAttractor = Planet1.GetComponent<FauxGravityAttractor>();
    }
    if (mouseSelect.collider.tag == "Planet2")
    {
    _FauxGravityAttractor = Planet2.GetComponent<FauxGravityAttractor>();
    }
    }
    }

    Maybe helpful to someone. If anyone has some tips for the camera problem please shout.
     
    Last edited: Jan 26, 2014
  2. EddieChristian

    EddieChristian

    Joined:
    Oct 9, 2012
    Posts:
    725
    I got the first scripts in this post working but I was wondering if anyone has ported them to Unity's 2D Physics/Sprite system yet???
     
  3. doug_hindson

    doug_hindson

    Joined:
    Jun 20, 2013
    Posts:
    1
    Hi guys,

    I'm just getting in to Unity and am currently having some issues with setting this up.
    I've been using the demo version alex.loren posted above (thanks for that!) and tweaking it in order to learn a bit more.
    However I'm now trying to use a first person controller and having no luck, anyone got any ideas?
    Many thanks!
     
  4. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Hi sorry about this I have been using Sebastian Lague's excellent version of this but I would like to know how you could change 1st person to 3rd person view as I tried a switch view camera example from the Speed Tutor but it doesn't seem to work Please help
     
  5. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Oh er sorry but I got it to work
     
  6. popiZz

    popiZz

    Joined:
    May 31, 2017
    Posts:
    2
    Hi everybody,
    Being quite late on that topic and after reading the whole thread, i'm wondering whether someone's still around to help me out.
    I'm having trouble to get my character animation playing correctly when he is either walking on a low poly mesh collider and when going around the world (i.e passing the equator and pretty much being upside down).
    freezeRotation's done, tag's are on as well as layers...i've been playing around with many things but i can't figure it out and as many other before, it's driving me crazy.
    Any ideas, anyone??
    That'd be just amazing!