最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Shot cool down in Three.js and CANNON.js - Stack Overflow

programmeradmin0浏览0评论

its been a while since I worked on this project but I have just come back to it and turns out its broken. I did some testing and I was able to pin point the problem but I have no clue how to fix it. The problem is something to do with the cooldown

  let lastShotTime = 0; // Tracks the last time a bullet was shot
  const shootCooldown = 1000; // Cooldown time in milliseconds

  if (testclick && gunn) { 
    const currentTime = Date.now();

    // Check if enough time has passed since the last shot
     if (currentTime - lastShotTime >= shootCooldown) {
    // Create the bullet
    let bullit = new THREE.Mesh(
      new THREE.SphereGeometry(0.05, 8, 8),
      new THREE.MeshBasicMaterial({ color: 0xffffff })
    );

    bullit.position.copy(swordPosi);
    bullit.alive = true;

    // Set bullet velocity and scale it for slower movement
    bullit.velocity = new THREE.Vector3(0, 0, -1);
    bullit.velocity.applyQuaternion(camera.quaternion);
    bullit.velocity.multiplyScalar(0.2); // Slower speed

    // Set a timer to mark the bullet as dead
    setTimeout(function() {
      bullit.alive = false;
      scene.remove(bullit);
    }, 1000);

    // Add the bullet to the bullets array and scene
    bulits.push(bullit);
    scene.add(bullit);

     Update the last shot time
      lastShotTime = currentTime;
    }
  }
  // Update bullet positions
  for (let index = 0; index < bulits.length; index += 1) {
    if (bulits[index] === undefined) continue;
    if (bulits[index].alive == false) {
      bulits.splice(index, 1);
      continue;
    }

    bulits[index].position.add(bulits[index].velocity);
  }
  
  if (clock.elapsedTime - lastResetTime >= resetInterval) {
    lastResetTime = clock.elapsedTime;
  }

its been a while since I worked on this project but I have just come back to it and turns out its broken. I did some testing and I was able to pin point the problem but I have no clue how to fix it. The problem is something to do with the cooldown

  let lastShotTime = 0; // Tracks the last time a bullet was shot
  const shootCooldown = 1000; // Cooldown time in milliseconds

  if (testclick && gunn) { 
    const currentTime = Date.now();

    // Check if enough time has passed since the last shot
     if (currentTime - lastShotTime >= shootCooldown) {
    // Create the bullet
    let bullit = new THREE.Mesh(
      new THREE.SphereGeometry(0.05, 8, 8),
      new THREE.MeshBasicMaterial({ color: 0xffffff })
    );

    bullit.position.copy(swordPosi);
    bullit.alive = true;

    // Set bullet velocity and scale it for slower movement
    bullit.velocity = new THREE.Vector3(0, 0, -1);
    bullit.velocity.applyQuaternion(camera.quaternion);
    bullit.velocity.multiplyScalar(0.2); // Slower speed

    // Set a timer to mark the bullet as dead
    setTimeout(function() {
      bullit.alive = false;
      scene.remove(bullit);
    }, 1000);

    // Add the bullet to the bullets array and scene
    bulits.push(bullit);
    scene.add(bullit);

     Update the last shot time
      lastShotTime = currentTime;
    }
  }
  // Update bullet positions
  for (let index = 0; index < bulits.length; index += 1) {
    if (bulits[index] === undefined) continue;
    if (bulits[index].alive == false) {
      bulits.splice(index, 1);
      continue;
    }

    bulits[index].position.add(bulits[index].velocity);
  }
  
  if (clock.elapsedTime - lastResetTime >= resetInterval) {
    lastResetTime = clock.elapsedTime;
  }

Share Improve this question asked Mar 31 at 20:04 Blaze BranhamBlaze Branham 786 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

i think you did not comment out here

 Update the last shot time

For some reason, "Update" is likely being treated as a function call, but then the parser hits "the".

or maybe javascript sees "Update" as initialized variable name and expected "=" as next token but sees "the"

Update = "Something here"

while it expects "=", it sees "the" which is not valid

发布评论

评论列表(0)

  1. 暂无评论