Search Unity

Measure acceleration?

Discussion in 'Scripting' started by thesimi, Nov 30, 2011.

  1. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    hi:D

    is there any way to measure the acceleration of an object so I can trigger an event according to the objects movement

    for example: if acceleration in the y-axis is more then 0 (moving up), set a var to true, but if acceleration in y is less then 0 (moving down), the var is false

    this would really help me out:D

    thanks in advance
     
  2. Dabeh

    Dabeh

    Joined:
    Oct 26, 2011
    Posts:
    1,614
    Three Vector3 variables.
    Swapping between them, first one is the third last update. Second is the second update. Third is the newest update.
    Update them based on what current frame you're at and you get this:
    Acceleration = (Second - First) - (Third- Second)

    I THINK that should work, not sure. That's off the top of my head.
     
    Last edited: Nov 30, 2011
  3. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    if you use this technique of moving:
    creating a Vector3, and modifying its x, y, z values according to the key pressed (Input.GetAxis()), and moving the player by the Vector, than you can do it: for example:

    PHP:
    var moveDir Vector3.zero;
    var 
    speed 10;
    var 
    jumpHeight 5;
    var 
    gravity 9;
    var 
    controller;

    function 
    Awake()
    {
        
    controller GetComponent(CharacterController);
    }

    function 
    Update()
    {
        
    moveDir.Input.GetAxis("Horizontal") * speed;
        
    moveDir.Input.GetAxis("Vertical") * speed;

        if(
    Input.GetKeyDown("space")  controller.isGrounded)
        {
            
    moveDir.jumpHeight;
        }

        
    moveDir.-= gravity Time.deltaTime;

        
    controller.Move(moveDir Time.deltaTime);
    }
    than you can check if the player is going up:
    PHP:
    if(moveDir.0)
    or check if its going forward:
    PHP:
    if(moveDir.0)
    or down:
    PHP:
    if(moveDir.0)
    left:
    PHP:
    if(moveDir.0)
    right:
    PHP:
    if(moveDir.0)
    back:
    PHP:
    if(moveDir.0)
    hope it helps.
     
  4. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    wow, thanks a lot for the help!:D

    hope you both have a nice day :D