Search Unity

How to Spawn Object at another Object's Specific x and z location but not it's y

Discussion in 'Scripting' started by zarvin, Jul 26, 2018.

  1. zarvin

    zarvin

    Joined:
    Mar 28, 2017
    Posts:
    14
    Heyo, so i'm making a vr game currently and i'm trying to make an ability that when you press certain buttons on the remote it will spawn a fissure that that launches out across the terrain from the current remote location. so essentially instantiate an object at the remote's specific X and Z coords but setting the Y cord to 0 or if you have a better idea on how to do this that would be great haha. Currently i'm using this as a place holder to test to make sure it actually works since i have no idea where to start for this kind of instantiation,
    Code (csharp):
    1.  
    2. public void ifRightTriggerAndRightGripPress() {
    3.         if (FissureCoolDown <= 0f) {
    4.             Instantiate(Fissure, RightRemote.transform.position, RightRemote.transform.rotation);
    5.             FissureCoolDown = 5f;
    6.             SpellCounter = SpellCounter + 1;
    7.         }
    8.         print("Both Right Grip and Trigger Pressed");
    9.         //Fissure
    10.     }
    11.  
    if you have any ideas on how to do this that would be great thanks in advance.
     
  2. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    I've never used VR, but assuming RightRemote.transform.position works...

    Code (CSharp):
    1. public void ifRightTriggerAndRightGripPress() {
    2.         if (FissureCoolDown <= 0f) {
    3.             Vector3 pos = new Vector3(RightRemote.transform.position.x, 0, RightRemote.transform.position.z);
    4.             Instantiate(Fissure, pos, RightRemote.transform.rotation);
    5.             FissureCoolDown = 5f;
    6.             SpellCounter = SpellCounter + 1;
    7.         }
    8.         print("Both Right Grip and Trigger Pressed");
    9.         //Fissure
    10.     }
    11.  
     
  3. zarvin

    zarvin

    Joined:
    Mar 28, 2017
    Posts:
    14
    sorry for such a late reply I had to go to work, thanks though that worked perfectly now if i wanted to do the same thing for the rotation what would the syntax be for that?
     
  4. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    Rotations are strange, its not like there is a 'ground level', so I dont understand what you mean. You can always do
    RightRemote.transform.rotation to access the rotation, however, this is a Quaternion (complex mathematical 4-dimensional thing) and you never want to meddle with the individual components.

    Now, if what you want is set any of the axis rotations (lets say, X to zero), you want to do euler angles:

    Code (CSharp):
    1. Vector3 eAngles = RightRemove.transform.eulerAngles;
    2. Vector3 modifiedEAngles = new Vector3(0, eAngles.y, eAngles.z);
    3. Quaternion finalRotation = Quaternion.Identity;
    4. finalRotation.eulerAngles = modifiedEAngles;
    5.  
    And use "finalRotation" instead of "RightRemote.transform.rotation" when instatiating.