Solved Change direction : Entity/Projectile - Fishing Hook

Discussion in 'Development' started by benda95280, Jun 22, 2020.

  1. benda95280

    benda95280 Spider Jockey

    Messages:
    45
    GitHub:
    benda95280
    Hello,

    I've found something new for me today, playing with Vector (not yet my BFF).
    I'm trying to change where the projectile will be 'send', code is as follow :

    PHP:
        public function onClickAir(Player $playerVector3 $directionVector): bool{
                
    $session Fishing::getInstance()->getSessionById($player->getId());
                if(
    $session instanceof Session){
                    if(!
    $session->fishing){
                      
                        
    $nbt Entity::createBaseNBT($player->add(0$player->getEyeHeight(), 0), $directionVector$player->yaw$player->pitch);

                        
    /** @var FishingHook $projectile */
                        
    $projectile Entity::createEntity($this->getProjectileEntityType(), $player->getLevel(), $nbt$player);
                        if(
    $projectile !== null){
                            
    $projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce()));
                        }
    I've try to do that (Don't know what i was doing here, but no change) :
    [Idea is it to add 30°/-30°, like when i try to fish ...]
    PHP:
                            $randomRotation floor(rand()/getrandmax()*30-30) ;
                            
    $randomRotationRadian $randomRotation * (M_PI/180);
                            
    $cos cos($randomRotationRadian);
                            
    $sin sin($randomRotationRadian);
                            
    $newX = (10000*($directionVector->$cos $directionVector->$sin))/10000;
                            
    $newZ = (10000*($directionVector->$sin $directionVector->$cos))/10000;
                            
    var_dump($newX);
                            
    var_dump($newZ);
                            
    $directionVector->add($newX0$newZ);
    Thanks :)
     
  2. Sesamo

    Sesamo Silverfish

    Messages:
    18
    GitHub:
    Lowzer
    Hook player?
     
  3. benda95280

    benda95280 Spider Jockey

    Messages:
    45
    GitHub:
    benda95280
    I've been able to found a solution :

    PHP:
                            $projectile->setMotion($projectile->getMotion()->multiply($throwForce));

                            
    $degreeToRand 30/(FishingLevel::getFishingLevel($player) == FishingLevel::getFishingLevel($player));
                            
    $randomRotation floor(rand()/getrandmax()*($degreeToRand*2)-$degreeToRand) ;
                            
    $randomRotationRadian $randomRotation * (M_PI/180);
                            
    $hookMotion $projectile->getMotion();
                            
    $theta deg2rad($randomRotation);
                            
    $cos cos($theta);
                            
    $sin sin($theta);
                            
    $px $hookMotion->$cos $hookMotion->$sin;
                            
    $pz $hookMotion->$sin $hookMotion->$cos;
                            
    $projectile->setMotion(new Vector3($px$hookMotion->y$pz));
    Seems to be OK.
    I think my mistake was trying to add instead of set it
    It's easier to work on it and understand after creation, using the motion.
     

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.