Rotate Away?

Discussion in 'How Can I...?' started by Roberto P, Jul 30, 2020.

  1. Roberto P

    Roberto P Boxer

    Joined:
    Apr 28, 2016
    Messages:
    42
    Likes Received:
    29
    Hi guys,
    I'm just wondering if anyone is able to edit the 'Rotate Towards' node to be 'Rotate Away'?
    The code is as follows - I'm not a programmer so I have no idea where to start ;)
    Any help would be much appreciated and hopefully others might find this useful as well.
    Thanks,
    Roberto

    Code:
    //
    let enabled = false;
    let phys;
    let targetType;
    let cam;
    let ent;
    let smooth;
    let targetEnt;
    let taName;
    
    function init() {
        cam = this.scene().camera();
        ent = this.entity();
        smooth = this.attribute('Smooth');
        smooth = 1 - Math.max(0, Math.min(0.99, smooth));
    
        phys = this.entity().physics();
        if (phys && phys.type() == 'kDynamic') {
            // warning('"Rotate Towards" node does not work with Dynamic physics.')
            // enabled = false;
            // phys = null;
        } else {
    
        }
    }
    
    function start() {
        targetType = this.attribute('Target');
        if (targetType == 'kInputEnt') {
            taName = this.attribute('Target Asset').name();
            targetEnt = this.scene().findFirst(taName);
        }
    }
    
    function update(dt) {
        if (!enabled) return;
        let ep = ent.position();
        if (targetType == 'kCam') {
            if (cam.projection() == 'kOrthogonal') {
                let ray = cam.screenCenterRay();
                let qTarg = Quaternion.lookAt(ep, ep.sub(ray.direction));
    
                // //HARD
                // ent.setRotationQuat(qTarg);
    
                // //SMOOTH
                let qOrig = ent.rotationQuat();
                ent.setRotationQuat(
                    Quaternion.slerp(qOrig, qTarg, smooth)
                );
    
            } else { //kPerspective
                let cwp = cam.worldPosition();
                let qTarg = Quaternion.lookAt(ep, cwp);
    
                // //HARD
                // ent.setRotationQuat(qTarg);
    
                // //SMOOTH
                let qOrig = ent.rotationQuat();
                ent.setRotationQuat(
                    Quaternion.slerp(qOrig, qTarg, smooth)
                );
            }
        } else { //kInputEnt
            if (targetEnt == null || targetEnt.isRemoved()) {
                targetEnt = this.scene().findFirst(taName);
                if (targetEnt == null) return;
            }
            let tp = targetEnt.position();
            let qTarg = Quaternion.lookAt(ep, tp);
    
            let qOrig = ent.rotationQuat();
            ent.setRotationQuat(
                Quaternion.slerp(qOrig, qTarg, smooth)
            );
        }
    }
    
    function signal(name, value) {
        enabled = value;
    }
    
     
  2. Pheartok

    Pheartok Boxer

    Joined:
    Jul 20, 2020
    Messages:
    7
    Likes Received:
    1
    I'm not a programmer either..however, in may instances in buildbox,making it a negative number seems to work sometimes.
    In this case maybe a negative number would make it go away instead of towards..?
     
  3. Roberto P

    Roberto P Boxer

    Joined:
    Apr 28, 2016
    Messages:
    42
    Likes Received:
    29
    Hi Pheartok, thanks for the suggestion. I'm actually using this idea currently, but was wondering if it's actually the same as rotating away, as I was getting strange results such as objects moving towards the player much faster than they were moving away.
     
  4. volcank

    volcank Serious Boxer

    Joined:
    Oct 8, 2015
    Messages:
    794
    Likes Received:
    391
    Hi could anyone solve this issue. I want my zombie characters to rotate towards the player and chase him but sadly the enemies are rotating away lol. so weird.
     
  5. Elite Games

    Elite Games Avid Boxer

    Joined:
    Dec 18, 2019
    Messages:
    184
    Likes Received:
    84

Share This Page