Search Unity

Changing Physics Direction

Discussion in 'Scripting' started by flamespirit919, Aug 19, 2014.

  1. flamespirit919

    flamespirit919

    Joined:
    Aug 18, 2014
    Posts:
    4
    So I'm planning to make a platformer where you have to rotate the level (kind of like Fez) along the Z-axis to get to certain places and solve puzzles. I want the player to be able to rotate the level with the arrow keys and I need some help doing this. I was hoping that when you press the right arrow key gravity would move -90 degrees along with the camera (and the opposite for the left, respectively). The character should also keep their momentum when they jump and auto-rotate so their feet stay on the ground. It doesn't matter if the code is for 2D or 3D, but I prefer it to be 2D. I'm not an expert (more of a beginner/intermediate) at Javascript or Unity, so it would be greatly appreciated if you could provide me some code. If you have any other ways that can accomplish this I will gladly accept it. There is also a chance that I might need help with making sure the character moves in the right direction when the gravity is changed. Thanks ahead of time.
     
  2. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    http://docs.unity3d.com/ScriptReference/Physics-gravity.html

    But why? You have to change gravity, rotate camera, make your character movement relative - so it knows where's up and where's down etc.

    Wouldn't it be far easier to rotate the entire level around the point of the center of camera? Everything else stays the same and you're done.

    EDIT:
    Tip: Parent all GameObjects that your level is composed of to a single GO and then it's just one line of code to rotate it.
     
    Last edited: Aug 20, 2014
  3. flamespirit919

    flamespirit919

    Joined:
    Aug 18, 2014
    Posts:
    4
    Oh thanks, I didn't think you could rotate the whole level. That was my first option, but then I thought that the other option was more feasible. I made a test level with a ball in a box and I can get it to rotate, but I don't know how to get it to rotate around the ball. If you could help me with getting it to rotate around the ball it would be much appreciated. I put this script on the box and level object and won't work. Here's what I got:
    Code (JavaScript):
    1. function Update ()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.RightArrow))(
    4.         transform.Rotate(0, 0, 90));
    5.     else if (Input.GetKeyDown(KeyCode.LeftArrow))(
    6.         transform.Rotate(0, 0, -90));
    7. }
     
  4. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    This code probably works but cube and sphere are symmetric like that. If you rotate them by 90 degrees then what you get is identical to what you started with and so you can't tell if anything happened at all unless you examined them in inspector.

    To rotate the cube around the sphere is not very use full in this case because there are multiple ways you can do that and what we are looking for is more specific: we want to rotate the level in a way so that the level remains parallel to camera. More over we don't want to rotate the level around it self but rather around the point where the camera is looking - were the player happens to be in the level at this moment.

    Use RotateAround to tell to rotate around specific axis (direction of camera) going through specific point (camera center):
    http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotateLevel : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private Camera gameCam = null;
    8.  
    9.     // Update is called once per frame
    10.     void Update ()
    11.     {
    12.         if (gameCam != null)
    13.         {
    14.             if (Input.GetKeyDown(KeyCode.RightArrow))
    15.                 transform.RotateAround(gameCam.transform.position, gameCam.transform.forward, 90f);
    16.             if (Input.GetKeyDown(KeyCode.LeftArrow))
    17.                 transform.RotateAround(gameCam.transform.position, gameCam.transform.forward, -90f);
    18.         }
    19.         else
    20.             Debug.Log("Set camera please!");
    21.     }
    22. }
     
  5. flamespirit919

    flamespirit919

    Joined:
    Aug 18, 2014
    Posts:
    4
    When I try to attach this script to the box it doesn't attach it and it says that the script does not exist, but it appears in the assets tab. Then I tried doing this with Javascript, but I could only get it to rotate very slowly until a certain point.
     
  6. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    I have actually tested it and it worked just fine.
    Try to find what is the simplest way to reproduce this problem. Start with new project and create only a cube GO and try attaching the script and see if it works. If it doesn't then provide some more details: what is the error message exactly, some screenshots perhaps.
     
  7. flamespirit919

    flamespirit919

    Joined:
    Aug 18, 2014
    Posts:
    4
    Thanks for the help. I didn't name the script RotateLevel and it didn't match the class name so it didn't recognize the script. I really appreciate your help.