Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Incremental movement...

Discussion in 'Scripting' started by purplexis, Oct 11, 2006.

  1. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    Ok, here's a question that may sound odd:

    I want to be able to move a character fixed amounts, i.e., move him like one might move on a gameboard. Every time the user hits an arrow key, the character moves a fixed distance then comes to an immediate halt.

    All the scripts in the hints and tips use the key to apply a vector of force to the character, so I'm struggling with how to do this. Any hints?
     
  2. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,271
  3. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    Hmm.

    And here I was trying to use SimpleMove.
     
  4. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,271
    I guess you're using the character controller? SimpleMove seems to be a way to approximate physical velocity without using a rigidbody etc. It's great for "real movement," but you want to have your character just "pop" from one place to another, right? In that case, just change the position of its Transform.

    You may not need the character controller if movement is all you wanted it for.
     
  5. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    Yeah, I got it to work nicely with transform.translate. Now my issue (which is in another thread) is that I need the character to not be allowed out of the level - since transform.translate seems to be sort of teleporting, and a collider wont block it.
     
  6. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,271
    I would suggest casting a ray using Physics.Raycast and checking to see how far away the collider it hits is.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Either that, or check the X/Z position, and if it's less than or greater than the bounds of the level, disallow the move.

    --Eric
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    If you are using the character controller already then you should use:
    controller.Move ();
    Which takes an offset in meters and not a velocity like SimpleMove.
     
  9. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    Ahh. Ok.

    Will this sort of movement recognize colliders between the character and its destination? So that I can simply throw a ring/box around my level to stop the character from being able to leave it?
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    the Move function and the SimpleMove function both handle collisions.
    The SimpleMove function takes velocity, the Move function an absolute movement. The SimpleMove function also automatically handles gravity for you, where the Move function simply attempts to move the character with the given movement vector performing collision detection on the way.,
     
  11. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,271
    As usual with Unity: it's easier than it seems!
     
  12. purplexis

    purplexis

    Joined:
    Jan 29, 2006
    Posts:
    71
    Indeed.

    The awesomeness of Unity is not to be underestimated. Thanks Joachim!