Search Unity

Spawn prefab at game objects location

Discussion in 'Scripting' started by beelzeboss, Jan 30, 2015.

  1. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    Wondering if anyone can tell me the best way of approaching this?

    Basically I want to spawn a prefab at a game objects position/rotation (this would have a specific tag) on a mouse button press but while the mouse is held down I would Also need to rotate around it's y axis at 90 degree increments then when let go the object is placed.

    So far I have it spawning the prefab at 0,0,0 I have no idea where to go from here to get the clicked objects transform and rotation.

    I should also add my code knowledge is very limited as I am from an art background.

    Any help is greatly appreciated.

    cheers

    Code (JavaScript):
    1.  var arrowMarker : GameObject;
    2. function Update ()
    3. {
    4.      if ( Input.GetMouseButtonDown(0))
    5.      {
    6.          var hit : RaycastHit;
    7.          var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    8.        
    9.          if (Physics.Raycast (ray, hit, 100.0))
    10.          {
    11. UnityEngine.Object.Instantiate(arrowMarker,transform.position,Quaternion.identity);
    12. }
    13. }
    14. }
     
  2. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    You can instantiate at hit.point instead of transform.position.
     
  3. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    cheers is there any way to take the transform and rotation of the object which is hit as I want the prefab to sit in the same spot
     
  4. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    beelzeboss likes this.
  5. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    using hit.transform gives me this error

    BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.GameObject, UnityEngine.Transform, UnityEngine.Quaternion)' was found.

    any thoughts on why?
     
  6. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    You are passing in a transform as 2 parameter, it needs to be a vector3, so either hit.point or hit.transform.position :)
     
    beelzeboss likes this.
  7. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    That's working great thanks for the help going to see if I can work out how to get it to take the rotation if not I will be back for some more help. Really appreciate this thanks.
     
  8. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    got the orientation working using turned out to be quite easy using FromToRotation with Vector3.up

    Code (JavaScript):
    1. UnityEngine.Object.Instantiate(arrowMarker,hit.transform.position, Quaternion.FromToRotation(Vector3.up, hit.normal));
    just got to get the drag snap rotate working I assume the best way to do this would be with a new function on mouse drag
     
  9. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    I am really struggling to get the rotation working here does anyone have any advice to get this to work.

    What I need is when the mouse is pressed it spawns my game object in the correct location which is working thanks to Ramsdals help, Then while the button is still down it rotates at 90 degree increments when the mouse is dragged left or right until released this is the bit I can not get my head around. I have been looking at the OnMouseDrag function but can't seem to find a solution

    This is how my current code looks there is nothing here for the rotation as like I said struggling to find anything that works

    Code (JavaScript):
    1. var arrowMarker : GameObject;
    2.  
    3. function Update ()
    4. {
    5.  
    6.      if ( Input.GetMouseButtonDown(0))
    7.      {
    8.          var hit : RaycastHit;
    9.          var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    10.      
    11.          if (Physics.Raycast (ray, hit, 100.0))
    12.              {
    13.                 UnityEngine.Object.Instantiate(arrowMarker,hit.transform.position, Quaternion.FromToRotation(Vector3.up, hit.normal));
    14.  
    15.             }
    16.         }
    17. }
     
  10. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    still having issues with this currently getting this error when I click the game object is put down the newObject(Clone) is added in the inspector but I get no rotation and the error below I have also posted my current script

    NullReferenceException: Object reference not set to an instance of an object
    Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
    PlayerGuideArrows.Update () (at Assets/Scripts_Custom/PlayerGuideArrows.js:23)

    Code (JavaScript):
    1. var arrowMarker : GameObject;
    2. var newObject : GameObject;
    3. var yDeg : float = 90;
    4. var roatationSpeed : float = 5;
    5.  
    6.  
    7. function Update ()
    8. {
    9.     if ( Input.GetMouseButton(0))
    10.      {
    11.          var hit : RaycastHit;
    12.          var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    13.          var newRotation = Quaternion.Euler(0, yDeg, 0);
    14.        
    15.      
    16.          if (Physics.Raycast (ray, hit, 100.0))
    17.             {
    18.                 newObject = Instantiate(arrowMarker, hit.transform.position, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    19.                 }
    20.          
    21.      {
    22.                 yDeg += Input.GetAxis ("Mouse Y");
    23.                 newObject.rotation = Quaternion.Lerp(newObject.transform.rotation, newRotation, Time.deltatTime * roatationSpeed);
    24.  
    25.             }
    26.         }
    27. }
     
  11. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Why is there a stray open bracket ( { ) on line 21?
    Also if the raycast doesn't hit an object newObject is never assigned so it outputs a null reference.
    Try adding

    Code (csharp):
    1.  
    2. if ( newObject != null )
    3. {
    4. yDeg += Input.GetAxis ("Mouse Y");
    5. newObject.rotation = Quaternion.Lerp(newObject.transform.rotation, newRotation, Time.deltatTime * roatationSpeed);
    6. }
    7.  
     
  12. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    cheers for pointing that out cleaned it up

    still get the same error with your tweak
     
  13. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    really close with this now after a lot of help. So the script is basically working the only issue I have now is the new game objects are not orienting correctly to the tiles when initially put down.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ArrowTest : MonoBehaviour {
    5.  
    6.     public GameObject arrowMarker;
    7.     public GameObject newObject;
    8.     public float yDeg = 90.0f;
    9.     public float rotationSpeed = 1000.0f;
    10.  
    11.  
    12.  
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.    
    17.  
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             RaycastHit hit;
    22.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    23.             if (Physics.Raycast(ray, out hit, 100.0f))
    24.             {
    25.                 newObject = Instantiate(arrowMarker, hit.transform.position, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    26.             }
    27.         }
    28.         if (Input.GetMouseButton(0))
    29.         {
    30.             yDeg += Input.GetAxis ("Mouse X");
    31.             var newRotation = Quaternion.Euler(0, Mathf.Round(yDeg / 90.0f) * 90.0f, 0);
    32.             newObject.transform.rotation = Quaternion.Lerp(newObject.transform.rotation, newRotation, Time.deltaTime * rotationSpeed);
    33.         }
    34.     }
    35. }
     
  14. beelzeboss

    beelzeboss

    Joined:
    Feb 28, 2013
    Posts:
    22
    Looks like I am doing something wrong in the last line. It looks like I need to try and isolate the Y axis somehow
     
    Last edited: Feb 4, 2015