3D怎么实现touch控制物体的移动和旋转?

真行业新人,看了api文档但是不知道怎么弄,觉得要使用getPreviousLocationInView(),但是不知道怎么调到。
代码如下:
import { _decorator, Component, systemEvent, SystemEvent, EventMouse, AnimationComponent, Node, Vec2, Vec3 } from “cc”;
const { ccclass, property } = _decorator;

@ccclass(“playerController”)
export class playerController extends Component {

@property({type:Node})
public touchNode: Node=null;

@property({type:Node})
public rotaTarget: Node=null;

@property({type:false})
private isMoving = false;

@property({type:Vec2})
public startPos: Vec2;

@property
public touchState = 1;

@property({type:Vec2})
public endPos: Vec2;

@property
public moveSpeed = 100;


start() {

}

onLoad() {
    this.touchState = 0;
    this.startPos = cc.v2(0, 0);
    this.endPos = cc.v2(0, 0);
    this.isMoving = false;

    this.node.on(cc.Node.EventType.TOUCH_MOVE, this.TouchCon, this);
}

TouchCon(event) {
    console.log("手指移动了");
    //触摸点的世界坐标
    var pos = new cc.Vec2(event.getLocationX(), event.getLocationY());
    //转换为UI坐标.
    console.log("未转换前的UI坐标:" + pos);

    pos = this.node.getWorldPosition (pos); //这里不知道怎么写,这个写法是错的。
    console.log("转换后的UI坐标:" + pos);
    // var temp= cc.Vec3(pos.x,pos.y,touchNode.position.z);

    //给要移动的物体赋值
    this.touchNode.position = pos;
    //角度范围控制
    if (this.rotaTarget.eulerAngles.y > 360)
        this.rotaTarget.eulerAngles = new Vec3(0, 0, 0);
    //要旋转的角度
    var tempRota = pos.x / 1000;
    //旋转目标物体
    this.rotaTarget.eulerAngles = new Vec3(0, tempRota, 0);
    //只移动x轴,Y轴同理
    //this.weapon.x=pos.x;

    console.log("当前物体的位置:" + this.touchNode.position);
    console.log("当前物体的角度:" + this.rotaTarget.eulerAngles.toString());
}

}

昨天发完,积极尝试,很快就写出来了,有点丢人,所以,自己来填坑哈。
代码如下:
import { _decorator, Component, systemEvent, SystemEvent, EventMouse, AnimationComponent, Node, Vec2, Vec3, SystemEventType, Touch, ColliderComponent } from “cc”;
const { ccclass, property } = _decorator;

@ccclass(“playerController”)
export class playerController extends Component {

@property({ type: Node })
public playerPos: Node = null;

@property({ type: Node })
public rotaTarget: Node = null;

@property({ type: false })
private isMoving = false;

@property({ type: Vec2 })
public startPos: Vec2;

@property
public touchState = 1;

@property({ type: Vec2 })
public endPos: Vec2;

@property
public moveSpeed = 100;

public RotaValue = 0;

start() {


}

onLoad() {
    this.touchState = 0;
    this.startPos = cc.v2(0, 0);
    this.endPos = cc.v2(0, 0);
    this.isMoving = false;

    systemEvent.on(SystemEventType.TOUCH_MOVE, this.TouchController, this);
    //方法一
    systemEvent.on(SystemEventType.TOUCH_END, function () {
        //当touch结束时,马上还原为初始值
        this.RotaValue = 0;
    }, this);

    //方法二
    // systemEvent.on(SystemEventType.TOUCH_END, this.TouchEnd, this);
    //方法三lamda表达式


}

update() {
    //打印小球的位置
    console.log("小球的位置:" + this.playerPos.position);
}

TouchController(event) {
    console.log("手指移动了");
    //获取当前触碰点的世界坐标
    let currentPos: Vec2 = event.getLocation();
    console.log("当前触碰点的世界坐标是:" + currentPos);
    //获取当前触碰点的UI坐标
    let currTouchPos: Vec2 = event.getUILocation();
    console.log("当前触碰点的UI坐标是:" + currTouchPos);

    //获取旋转的方向
    let pos: Vec2 = event.getUIDelta();
    console.log("获取触点距离上一次事件移动的距离对象:" + pos);
    //每次touch都加一个角度
    if (pos.x < 0)//向左
    {
        this.RotaValue -= 10;
    }
    else //向右
    {
        this.RotaValue += 10;
    }

    //旋转目标物体
    this.rotaTarget.eulerAngles = new Vec3(0, this.RotaValue, 0);
    console.log("当前目标物体的欧拉角:" + this.rotaTarget.eulerAngles);
}

TouchEnd(event) {
    this.RotaValue = 0;
}

}

1赞

你这里其实也可以用 event.getDelta() 或者 event.getUIDelta() 来获取与上一次触点的差值,这样就能很快确定是往左旋转还是往右

1赞

大佬,能不能弄个摇杆的出来??

1赞