Search Unity

How to make bottom of Player face to spherical planet in 2D

Discussion in 'Scripting' started by mcarthur03, Jul 20, 2016.

  1. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    Gah, You wouldn't believe how much Ive been through trying to get this to work if i told you... I could jump off a cliff right now im just so frustrated... well, here is what i need to do. Ive been working on a space game for a few months now, and it works in that there are planets. I have gravity set up fine in which the closer you get the more it pulls you (and after a certain distance away you stop getting pulled) I have set up a system in which planets have a collider which is the fallzone (when the player is in this collider, the fall quickly and the game becomes a platformer on the spherical planet) and when the player leaves, they are floating in space, look toward the mouse and the wasd keys controll movement in each direcetion. I have got everything working, except for the system which makes the player rotate to the planet.

    At first, i was trying to get the player to rotate to the planet, that didnt work for some reason.
    Then i tried rotating to the fallzone. This is when i realized whats going on. The player is rotating to the fallzone in which they are in which is stuffing things up.

    So i tried putting a gameobject at the center of the planet as a child, but I cant figure out how to access its position, I have tried accessing the child but am lost, There is one other thing that i have tried as well as rotating to the center. That is rotating to the fallzone using renderer.bounds.center but this has also failed for me.

    If anyone could help me get one of the two methods working i would greatly appreciate it. here is my entire character controller script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharacterController : MonoBehaviour
    5. {
    6.  
    7.     public float Speed = 0f;
    8.     private float movex = 0f;
    9.     private float movey = 0f;
    10.     public Rigidbody2D rb;
    11.     public bool grounded;
    12.     public float jumppower;
    13.     public Vector2 maxaxis;
    14.  
    15.     public float jetspeed;
    16.  
    17.     public Vector2 nearplanet;
    18.     public GameObject nearplanetg;
    19.  
    20.     public static float fuel;
    21.     public float life;
    22.     public float fuelvis;
    23.  
    24.     public GameObject[] lifebar;
    25.     public GameObject[] fuelbar;
    26.     public GameObject gameover;
    27.  
    28.     public bool fallzone;
    29.     public GameObject targplan;
    30.  
    31.  
    32.     public static bool publicfallzone;
    33.  
    34.     public GameObject plancorecheatycheatforthelolz;
    35.  
    36.  
    37.  
    38.     // Use this for initialization
    39.     void Start()
    40.     {
    41.         fuel = 500;
    42.     }
    43.  
    44.     void OnTriggerEnter2D (Collider2D planet)
    45.     {
    46.         if (planet.tag == "fallzone") {
    47.             fallzone = true;
    48.             if (fallzone)
    49.             {
    50.                targplan = planet.gameObject;
    51.            
    52.             }
    53.         }
    54.     }
    55.  
    56.     void OnTriggerStay2D (Collider2D planet)
    57.     {
    58.         targplan = planet.gameObject;
    59.     }
    60.     void OnTriggerExit2D(Collider2D planet)
    61.     {
    62.         if (planet.tag == "fallzone") { fallzone = false; }
    63.     }
    64.     // Update is called once per frame
    65.     void Update()
    66.     {
    67.  
    68.         publicfallzone = fallzone;
    69.  
    70.         if (fallzone) { gravity.maxGravity = gravity.maxGravity * 2;
    71.             Renderer plancorerenderer = targplan.GetComponent( typeof(Renderer)) as Renderer;
    72.             Vector3 plancore = plancorerenderer.bounds.center;    
    73.             transform.rotation = Quaternion.LookRotation(Vector3.forward, plancore - transform.position);
    74.             // transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vectorToTarget, Vector3.forward), 1 * Time.deltaTime);
    75.         }
    76.    
    77.  
    78.         fuelvis = fuel;
    79.         fuel = fuelvis;
    80.    
    81.         grounded = GroundCheck.isground;
    82.         if (fuel <= 0) { gameover.SetActive(true); }
    83.         if (fuel > 0)
    84.         {
    85.             if (fallzone == false)
    86.             {
    87.                 Vector3 mouseScreen = Input.mousePosition;
    88.                 Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
    89.  
    90.                 transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90);
    91.             } else
    92.             {
    93.  
    94.                 Vector3 planpos = targplan.transform.position;
    95.                 Vector3 plan = Camera.main.ScreenToWorldPoint(planpos);
    96.  
    97.                 transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(plan.y - transform.position.y, plan.x - transform.position.x) * Mathf.Rad2Deg);
    98.            
    99.              
    100.             }
    101.             if (grounded == true && fallzone == true)
    102.             {
    103.  
    104.  
    105.  
    106.             }
    107.             else {
    108.                 rb.gravityScale = 0;
    109.  
    110.                 if (Input.GetKey("w")) { rb.AddRelativeForce(new Vector2(0, jetspeed)); fuel -= 0.1f; }
    111.                 else if (Input.GetKey("s")) { rb.AddRelativeForce(new Vector2(0, -jetspeed)); fuel -= 0.1f; }
    112.  
    113.                 if (Input.GetKey("a")) { rb.AddRelativeForce(new Vector2(-jetspeed / 2, 0)); fuel -= 0.1f; }
    114.                 else if (Input.GetKey("d")) { rb.AddRelativeForce(new Vector2(jetspeed / 2, 0)); fuel -= 0.1f; }
    115.             }
    116.  
    117.  
    118.             if (fallzone == true)
    119.             {
    120.  
    121.  
    122.                 if (Input.GetKey("a") && grounded == true) { rb.AddRelativeForce(new Vector2(-jetspeed * 0.5f, 0)); }
    123.                 else if (Input.GetKey("d") && grounded == true) { rb.AddRelativeForce(new Vector2(jetspeed * 0.5f, 0)); }
    124.                 else
    125.                     movex = 0;
    126.                 if (Input.GetKey("w") && grounded == true) { rb.AddRelativeForce(new Vector2(0, jetspeed * jumppower)); gravity.maxGravity = 14.25f; }
    127.                 else { gravity.maxGravity = 50; }
    128.  
    129.  
    130.             }
    131.         }
    132.    
    133.         // else if (Input.GetKey(KeyCode.S))
    134.         //     movey = -1;
    135.     }
    136.     public void stopy ()
    137.     {
    138.         movey = 0;
    139.     }
    140.  
    141.     public void Awake()
    142.     {
    143.  
    144.     }
    145.  
    146.     void FixedUpdate()
    147.     {
    148.         if (grounded == true)
    149.         {
    150.        
    151.         }
    152.    
    153.  
    154.     }
    155.  
    156.  
    157.  
    158. }
    159.  
    The issue is on line 70
     
    Last edited: Jul 20, 2016
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    It should be pretty easy, something like this will work:
    Code (CSharp):
    1.  
    2.         Vector2 gDir = transform.position - planet.position;
    3.  
    4.         transform.up = gDir;
    5.  
    Assuming that the script is placed on the player an planet is the transform for the planet the player is on.
     
  3. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    Just turned my pc off, Will test this tommorrow. Hope it works. By planet the player is on do you mean the fallzone the player is in or the actuall visible planet that the player should be falling too?
     
  4. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    Uh, what i meant to say was, i have multiple planets and the targplan is the fallzone, the planet itself is the fallzones parent. how would i access the fallzones childs transform?

    I have them as seperate objects as not to trigger the grounded trigger.
     
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    I made a little demo project that does a simple spherical gravity which aligns the player with the gravity field of the closest planet. You can find it at; http://gnulix.org/unity/GravityDemo.zip
     
  6. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Planet.position should be the exact center point of the planet. When you subtract it from the players position it will return a distance vector. When you normalize the distance vector you get a direction vector. You can then use the direction vector to apply gravity and/or rotate your player.

    Adding on to PGJ's code.
    Code (csharp):
    1. float gravity = -9.81f;
    2.  
    3. Vector3 direction = (player.transform.position - planet.transform.position).normalized;
    4.  
    5. //apply gravity
    6. player.rigidbody.AddForce(direction * gravity);
    7.  
    8. //rotate
    9. player.rigidbody.MoveRotation(Quaternion.FromToRotation(player.transform.up, direction) * player.rigidbody.rotation);
     
    Last edited: Jul 21, 2016