setVelocity - Vector

Discussion in 'Plugin Development' started by ServerfromMinecraft, Sep 19, 2012.

Thread Status:
Not open for further replies.
  1. Hi!

    With setVelocity i can make that a zombie move to a location. but how? If i make setVelocity(loc.toVector()); its not work...
     
  2. Try something like new Vector(1, 0, 0);
    You have to give directions, not coordinates. The example above should move the mob on the X axis. If you want to move backwards just use negative values.
     
  3. Thanks! And, if i make: new Vector(5,0,5) its move diagnonaly 5blocks?
     
  4. I'm really not sure about the numbers, I never really worked with vectors. Maybe 0.0005 is more than enough, maybe you need 50000, no idea. Just try&error? ;) But yes, if you set two values it moves diagonally.
     
  5. Ok! And another question: how i can get the direction from a location to antoher location? because i have one location (a spawnpoint) and i would that the entitys move to the another point - but for that i need the direction from loc1 -> loc2 ...
     
  6. Offline

    desht

    I know that a vertical velocity of about 0.37 is equivalent to a normal player jump. Assuming horizontal velocities are the same (I imagine they would be), then a velocity of (5,0,5) will send your zombie southwest at extreme speed :)

    For getting the direction from location A to location B, this should work (untested):
    PHP:
    Vector dir locB.toVector().subtract(locA.toVector()).normalize();
    // dir is now a unit vector pointing from A to B
    // set "speed" to how fast you want the entity at A to move towards B
    entity.setVelocity(dir.multiply(speed));
     
  7. Offline

    Zidkon

    Position 1 = X1, Y1, Z1
    Position 2 = X2, Y2, Z2

    As far as I remember (maybe you should try looking at vector in maths) direction vector from 1 to 2 is:
    Position 2 - Position 1 = X2-X1, Y2-Y1, Z2-Z1

    You have a vector that contains a direction but the magnitude is high, I'm not sure how this Vector thing no the code will work, but I can say the magnitude of the vector will give the zombie the speed, not the direction, but I don't know what relation have the magnitude itself with the speed :p

    Yes that probably will work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  8. Offline

    desht

    Yep, you have it - you just need to normalise the vector to get a unit vector in the right direction, which you can then multiply with your desired speed - see my code snippet above.

    Edit: simultaneous post, pretty much? :)
     
    Zidkon likes this.
  9. Offline

    Zidkon

    Hahahah, while I was doing the first post u made urs, then I replied :p

    Bro, do you know the speed thing? I mean what values are the ones for normal speeding and stuff? thats an interesting thing I would like to check out ;)
     
  10. Thanks! But the entity, on spawning, not move to position b - just one block, in the direction to b, not more :/
     
  11. Offline

    ernierock

    kinda late but it may help you, you need to multiply it to set speed.
     
Thread Status:
Not open for further replies.

Share This Page