Search Unity

Unity3D Move step by step

Discussion in 'Scripting' started by Showoffz, Feb 12, 2016.

  1. Showoffz

    Showoffz

    Joined:
    Feb 12, 2016
    Posts:
    8
    I am trying to make my locomotion system in Unity that I will later be used for Oculus.

    Right now I created a object with camera and a rigidbody called "rb".

    // Perform movement
    void PerformeMovement(){

    if(velocity !=Vector3.zero){
    rb.position =(rb.position + velocity);
    }

    }


    I calculate velocity in my other class. I am trying to do 2 things..

    1. Make the movement feel more like teleporting. Right now it's smoothly moving to a new position.
    2. Make the rigidbody stop for a second or two after the step. So that holding down will not feel like running.
    Any API hints? Not looking for someone to write it for me... Or mby links to similar locomotion systems?

    Main Goal is to make the movement more incremented than smooth and so I could bypass conflicts that cause nausea.

    EDIT!

    Right now I updated my code to >

    void PerformeMovement(){
    if(velocity !=Vector3.zero){
    if(timer %60==1){
    rb.MovePosition(rb.position + velocity *20);
    }
    }
    }

    It works kinda like I want but how can i deal with collisions now? If the step is long enough it will just step through the object. If the step is too small it gets stuck and then randomly flies away...
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    ok, first, dont use rigidbody.MovePosition. You might as well just use transform.Translate. This really doesn't work well.

    Code (csharp):
    1.  
    2.     void PerformeMovement()
    3.     {
    4.         rb.velocity = velocity;
    5.     }
    6.  
    I dont know what the timer is for, but I have a feeling that it probably is not needed.
     
  3. Showoffz

    Showoffz

    Joined:
    Feb 12, 2016
    Posts:
    8
    The "timer" is added to make the movement feel more like step by step movement.
    For better understanding ->

    here is the code :
    http://pastebin.com/1VzKpmqn
    http://pastebin.com/bWUXCY0r

    This controller behaves similar to my needs but It has some collision problems - walking through walls or flying off to the distance.

    The solution is basic and most likely horrible but I had only 2 days to make it work without any previous C# or Unity experience.
     
  4. Showoffz

    Showoffz

    Joined:
    Feb 12, 2016
    Posts:
    8

    Can't get how velocity can help me...
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    a rigidbody is something that is updated every fixed update. it takes the velocity and angularVelocity and figures out where the object should be next. By setting the velocity, then you set your person or object to move for you. Furthermore, it does this smoothly with the physics engine.

    If all you want is to move an object by steps, then rigidbody is NOT the way to go, so you dont want to use it at all. For that, you use transform.Translate.

    I will give you two real world problems....

    1) I want to have a character be able to push blocks, and move things around, or basically interact with other things in a physical sense; I will use Rigidbody for this.

    2) I dont care if we are interactive, I just want to move things in steps. This would be turn based block movement, or any type of movement that would not require interaction; For this, we use a CharacterController

    Rigidbodies move using physics (i.e. force and velocity) and CharacterControllers move using numbers. (i.e. controller.SimpleMove(Input.GetAxis("Horizontal") * speed, 0, 0);