Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

Making custom projectile launcher?

So I've gone through and done Holograms 240 and was able to use their projectile launcher which was made for that project. I'm trying to adapt the holographic academy script "Projectile Launcher" to shoot a projectile prefab at an enemy hologram in a shooting range application I'm working on. I would love to be able to shoot in a similar fashion to how it functions on Roboraid, minus the laser! I currently have OnSelect sent from the Gesture Manager trigger Destroy(gameobject) in my project and is working as a place holder for what I really want. How do I use OnSelect (the tap gesture) to instantiate a projectile prefab aimed at the target my gaze is focused on?

Here is the C# script I have right now which is supposed to shoot a projectile (and doesn't):

using UnityEngine;
using System.Collections;
using System;

public class ShootOnSelect : MonoBehaviour
{
public Rigidbody projectile;
public double Speed = 3;

public void OnSelect()
    {
         Instantiate(projectile, transform.position, transform.rotation);
    projectile.AddForce(Vector3.up * 2000, ForceMode.Impulse);

    }

Please let me know what suggestions you more experienced Hololens users might have, I'm really frustrated at this point!

Best Answer

Answers

  • When you want to be able to shoot, you need to tell GestureManager to redirect OnSelect messages to the GameObject that has the OnSelect script. You do this by:

    GazeManager.Instance.OverrideObject = <....>whateverGameObject.

    The simplest thing to do would be to add an Awake() function to the script with:
    GazeManager.Instance.OverrideObject = this.gameObject.

    but likely you'll have times in your experience where you aren't shooting things.

    ===
    This post provided as-is with no warranties and confers no rights. Using information provided is done at own risk.

    (Daddy, what does 'now formatting drive C:' mean?)

  • @ahillier said:
    In Holograms 240, there are two scripts that should help you out: ProjectileLauncher and ProjectileBehavior. Each projectile (prefab) has a ProjectileBehavior script attached to it in the course.

    ProjectileLauncher spawns the projectile and sets the starting direction to be the forward direction of the camera (which is where the user is looking), while ProjectileBehavior assigns the force and velocity to the newly spawned projectile.

    Below is a shortened form, which combines functionality from the OnSelect, SpawnProjectile, ShootProjectile and LaunchProjectile functions of those classes. This should do what you want (warning: untested code!):

    // Projectile prefab with a RigidBody component
    public GameObject Projectile;
    
    void OnSelect()
    {
        Vector3 projectilePosition = Camera.main.transform.position + Camera.main.transform.forward * 0.85f; 
       
       Vector3 projectileDirection = Camera.main.transform.forward;
    
       ShootProjectile(projectilePosition, projectileDirection);
    }
    
    void ShootProjectile(Vector3 start, Vector3 direction) 
    { 
          GameObject spawnedProjectile = (GameObject)Instantiate(Projectile);
    
          // set the projectile's starting location to be slightly in front of user
          spawnedProjectile.transform.position = start;
    
          // get the RigidBody to apply force to projectile
          Rigidbody rigidBody = spawnedProjectile.GetComponent<Rigidbody>(); 
    
         // apply force to the projectile          
         rigidBody.velocity = 4 * direction;
          
         // make the projectile spin
         rigidBody.angularVelocity = Random.onUnitSphere * 20;
     }
    

    Also, if you don't want gravity to pull your projectile down, be sure to disable it on your prefab's rigidbody component (or make it weaker, so less downward force is applied).

    @ahillier Thank you, this actually worked! There is one problem, however. It instantiates two projectiles for some strange reason, I'm going to do some code scouring to see if I find anything. If you have any suggestions let me know! Otherwise, thank you for your help!

  • Oh! Double check that you only have one instance of GestureManager in your scene. Both GazeManager and GestureManager should be thought of as global components that all objects have access to (we like to make an empty GameObject in our scene called 'Managers' where these will reside). If you have more than one GestureManager in your scene, then you will get more than one tap event fired when you do the select gesture.

  • @ahillier Thank you! That was a simple fix which I didn't think of.

Sign In or Register to comment.