Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Scripts to do standard things ?

Discussion in 'Editor & General Support' started by thefinn, Jan 12, 2015.

  1. thefinn

    thefinn

    Joined:
    Jan 2, 2015
    Posts:
    16
    I'm kind of frustrated with the "stealth" tutorial in that it has you create a movement script (for instance) where you use the wasd keys to move around. However, it doesn't show you how to make them move the character forward, left, back, right... instead they move n.w.s.e all while it gets you to create a camera view which constantly changes angles based on how visible the character is.

    I am wondering if there's a good area to find scripts that can be used for moving a bipedal character around - which is pretty generic and I'm sure people just "have stuff for this".

    I'm still learning sure, but I don't want to get bogged down too much on physics and graphics and things of that nature. The idea I have is on the far end of the "hard slog" scale in terms of work and I'd like to really get my teeth into some of the UI and tables elements I have in mind.

    Any examples of a camera controller where the player can control the camera view would also be great.

    Any help appreciated.
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    Moving a character relative to the camera is indeed fairly simple with a little 3D math. All you need to do is convert the input direction into camera space. That sounds nasty to begin with, but as you get practice with your 3D math it's pretty straightforward.

    A simple way to do this is to multiply your input by your Camera's Transform's forward/up/right instead of using the input directly. To make this work nicely you'll want to flatten them (clip out the axis you're not using, so typically setting y to 0) and then re-normalize them.

    So, for moving foward you might have something like this:
    Code (csharp):
    1. Vector3 flattenedForwards = Camera.main.transform.forward; // Camera's forward axis
    2. flattenedForwards.y = 0; // Remove the axis we're not moving on
    3. flattenedForwards = flattenedForwards.normalized; // Re-normalize so it doesn't mess with our input magnitude
    4.  
    5. // Multiply our forward input by that calculated forward vector. Note that this
    6. // is only the forwards component of the movement. You'll need to do similar
    7. // with the right component and then add them together.
    8. Vector3 forwardsComponent = Input.GetAxis("Vertical") * flattenedForwards;
    Unity has an example of an orbit camera that's pretty nice to learn from in the Standard Assets package. Combining that with the above should get you to a good start.
     
  3. thefinn

    thefinn

    Joined:
    Jan 2, 2015
    Posts:
    16
    Yeah camera relativity I already have - it's in the Stealth tutorial.

    Probably best if you knew what was in it before trying to help, as it's hard to explain.

    If you check out the script - instead of hitting w for forward you hit w for say "north" or X axis... So that stays the same no matter what direction you're facing.

    Then the camera moves relative to the player but also changes camera angle dependent upon a raycast check to make sure walls aren't in the way... This then pans the angle of the camera - which can completely change the direction your relative "north" even is - frankly it's an awful first example and I'm sure I'm not the only one who's come across it.

    I'm after instead an example of a script where "w" moves the player forwards instead of "north" as I'm unsure of the math involved - or even if it's necessary.

    As an aside an example of a camera that can pan via command from the player would be great - ala MMO style or 3rd person shooter style with the mouse.

    Just like an example someone ran across where someone implemented it in a Free download or something would be awesome.

    Thanks for any help anyhow.
     
    Last edited: Jan 12, 2015
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    Well then that's not "north" at all, is it? :p North is a direction relative to the world frame, so it stays the same regardless of the direction your camera is pointing. (Though if we get nitpicky it would change dependent on position if you were walkting too near the world's poles.)

    Anyway, you want to do the same thing as my solution above, just instead of using the camera's Transform you want to use the player object's Transform. It'll get more complex, though, as you'll also need to provide a way for the player to turn... and that'll probably bring you right back to camera-relative controls.

    The existing system that you describe is pretty standard for stealth games where the player doesn't control the camera, so that's probably why they went with it. Making it feel good takes a lot of game- and level-specific work, though.

    Unless they've been changed really recently there's one in the Standard Assets. Orbit.js, if I remember correctly. I've used it as a starting point for my own cameras a number of times.
     
  5. thefinn

    thefinn

    Joined:
    Jan 2, 2015
    Posts:
    16
    Sweet thanks will have a look.
     
  6. thefinn

    thefinn

    Joined:
    Jan 2, 2015
    Posts:
    16
    I am not asking for support for the scripts.

    I am asking for new scripts that do what I want.

    Why move the post ?