Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Spiral Staircase

Discussion in 'Scripting' started by theZiggy1, Feb 26, 2020.

  1. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    Hi guys, so I have a character in a game that moves using the following code:

    Code (CSharp):
    1. Vector3 movement = new Vector3(Input.GetAxis("LeftStickVertical"), 0.0f, Input.GetAxis("LeftStickHorizontal"));
    2.  
    3. transform.Translate(movement);
    its a League of legends style view. Im looking to move up a spiral staircase, however I cant figure out how to translate the vector, to go up the staircase, so walking straight in a direction (say right) moves the character always the same way up the spiral. (if you hold right, the character should follow the staircase up the whole way.

    I also have a camera in the middle of the spiral, that follows the player if that helps.

    Code (CSharp):
    1. this.gameObject.transform.LookAt(Player.transform);
    2. this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, Player.transform.position.y + 18, this.gameObject.transform.position.z);
     
    Last edited: Feb 26, 2020
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    There may be a more elegant solution to this, but i would probably cast a ray downwards from a point close to / around the head of your character, towards the ground, in the direction of the player inputs. So the joystick inputs would effectively only move this point around the characters head, and the result of the downwards raycast hitting the ground would be the location to move to.

    Currently you are not setting any y-values when moving. So you needed a way to determine the correct y-coordinate. Downwards raycasts are generally good for this. Since you wanted to go up a spiral staircase however, you cannot start the raycast at an arbitrary height, since then you would hit the stairs above the characters head. Thus my approach was to get the correct y-coordinate to move to from the characters perspective, so to speak.
    Keep in mind i just made this up on the fly and there may be more sophisticated solutions out there.

    You may also use a NavMesh to solve the correct path finding for you, but since you are using joystick inputs, this would still leave you with the problem of finding the correct (y-)location to walk towards. So the above may actually be a decent solution.
     
    Last edited: Feb 26, 2020
  3. theZiggy1

    theZiggy1

    Joined:
    Feb 3, 2019
    Posts:
    14
    updates, mispoke on the view. its similar to league of legends. I should say, i need the controls to match the rotation of the camera.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    to get an axis-unaligned movement system, you multiply your camera rotation Quaternion with ordinary cardinal vectors

    for example if you want to move right, you'd determine the final vector as myVec = Vector3.right
    but this'll only work in absolute terms

    if you want it relative to your camera, integrate it's rotation by doing myVec = camera.transform.rotation * Vector3.right
    now maybe you want it opposite of that, I can't exactly visualize the whole setup, so try various combinations of it
    either Quaternion.Inverse(camera.transform.rotation) * Vector3.right
    or simply negate the whole resulting vector with a minus in front

    [edit]
    oh and if your camera is tilted (probably it is) this won't work as easily as that
    so it would help if you had an original camera angle somewhere (likely the orientation of your player in this case)
    you can recreate the required Quaternion by doing Quaternion.Euler(0f, myAngle, 0f)