Important method is missing: LookAt

Hello,

Every time I creating a new game or demo I am writing the same method all over again. This method is very important and I think it shoul be added to the core engine (maybe to cc.Vec?).

    public static LookAt(startPos: cc.Vec3, endPos: cc.Vec3): IRotation {
        const direction = endPos.sub(startPos);
        const directionNormalized = direction.normalize();
        const radianAngle = Math.atan2(directionNormalized.y, directionNormalized.x);
        const angle = cc.misc.radiansToDegrees(radianAngle);

        return {
            direction: direction,
            directionNormalized: directionNormalized,
            radianAngle: radianAngle,
            angle: angle
        }
    }

What I did here is to rotate the node to look at its direction movement.
An important one… Please consider to add it… :slight_smile:

1 Like

Node has a lookAt function, is that helps you?

Its not working…

@ccclass
const {ccclass, property, help} = cc._decorator;

@ccclass
@help(http://HELP.ME)
export default class Rotate extends cc.Component {

    @property(cc.Node)
    public arrow: cc.Node = null;

    @property(cc.Node)
    public target: cc.Node = null;

    start () {
        
    }

    update(dt: number) {
        this.arrow.lookAt(cc.v3(this.target.position.x, this.target.position.y, 0));
    }
}

Another strange thing is that node.position is cc.Vec2 while lookAt parameter requests for cc.Vec3.
So I had to also transform v2 to v3 which is not a wise thing to do.

Is your node a 3D node? node.lookAt will change node’s 3D rotation, so it may not work with a 2D node.

I am using 2d. But it should work in 2d space too.
If not, a method should be added.

Thanks for your advice, we will consider.

1 Like