Trying To Give Throw able Object Knockback

Discussion in 'Programming' started by SoftwenOP, Oct 7, 2019.

  1. SoftwenOP

    SoftwenOP Member

    Joined:
    May 7, 2019
    Posts:
    6
    I'm currently messing around with a project in GMS 1.4. I've successfully implemented a mechanic which allows the player to pick up and throw objects. However, I've experienced difficulties getting the object to have proper knockback, or a bounce feature when colliding with an object while being thrown. I've tried various methods to no avail. If anything does happen, the object either goes into the opposite direction, but doesn't stop, or stops, but goes in the incorrect direction. This is the movement code of the object, held within the step event:

    ///Follow Player
    if Stick = true {
    x = obj_player.x;
    y = obj_player.y;
    }

    //Be Thrown
    if Dir1 = true {
    move_towards_point(x, y+Spd, Spd);
    }
    if Dir2 = true {
    move_towards_point(x, y-Spd, Spd);
    }
    if Dir3 = true {
    move_towards_point(x+Spd, y, Spd);
    }
    if Dir4 = true {
    move_towards_point(x-Spd, y, Spd);
    }

    This is the code which allows the player to throw the object:

    ///Throw
    if place_meeting(x, y, obj_rock) {
    var ID;
    ID = instance_nearest(x, y, obj_rock);
    if Facing = 1 && ID.Stick = true {
    ID.Dir1 = true;
    ID.Spd = 6;
    ID.Stick = false;
    }
    if Facing = 2 && ID.Stick = true {
    ID.Dir2 = true;
    ID.Spd = 6;
    ID.Stick = false;
    }
    if Facing = 3 && ID.Stick = true{
    ID.Dir3 = true;
    ID.Spd = 6;
    ID.Stick = false;
    }
    if Facing = 4 && ID.Stick = true {
    ID.Dir4 = true;
    ID.Spd = 6;
    ID.Stick = false;
    }
    }

    The main method I tried was setting Spd to equal -6 upon collision with a static object, then while Spd is below zero, use the lerp function to try and slow it down until Spd is equal to zero. I'm open to any suggestions, even if it involves changing this code.
     
  2. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    424
    If I understand this correctly you would do this to make it bounce

    Code:
    Dir1 = !Dir1
    Dir2 = !Dir2
    Dir3 = !Dir3
    Dir4 = !Dir4
    to make it slow down you could do spd *= 0.5
     
  3. SoftwenOP

    SoftwenOP Member

    Joined:
    May 7, 2019
    Posts:
    6
    This has gotten me closer than before. Though the issue is that it will really only bounce if thrown to the right, and the object will continue to go into the opposite direction if picked up and thrown again. Maybe a state machine would be best?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice