Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Getting Components or Quaternion stuff problem :P

Discussion in 'Scripting' started by Zackhanzo, Jul 21, 2014.

  1. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Hi guys i dont wanna make a large post so:

    I have a Player who has a pistol. Inside the pistol i have a gameobject which is tagged as Laser where i have all the shooting thing (raycast, effects blabla)

    Now i want on my enemy script a script to get the hit point to add a texture of blood to the enemy.... So... on the enemy script i have something like this:
    Code (CSharp):
    1.  
    2.  public Texture Blood;
    3.   GameObject takingLaser;
    4. void Start () {
    5.   takingLaser = GameObject.FindGameObjectWithTag ("Laser");
    6.     }
    7. public void TakeDamage(float amt){
    8.         laser laserRay = takingLaser.GetComponent<laser> ();
    9.         Instantiate (Blood, laserRay.hit.point, Quaternion.FromToRotation (Vector3.up, laserRay.hit.normal));
    Im pretty sure that Start void is ok. And 1º sentence of TakeDamage too. In the Instantiate sentence:

    i have the texture Blood, checked
    I have the laserRay.hit.point checked

    But i think i have wrong the Quaternion Stuff, because i want to acced to the Quaternion of the other Script, isnt it? But i dont have a clue how to do it :(
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Your Quaternion math looks good to me. hit.normal will be the outward facing direction (in world space) of the collider the raycast hits - you shouldn't need to access the other object's rotation at all, in that context. hit.normal has the information you need.

    Your problem is that you're instantiating a Texture. And while you can instantiate a Texture, it's not going to do what you're trying to do here. A Texture is just a reference to an image; it has no "shape" in the real world, so to speak. What you need to do is create a prefab for your blood splatter (probably using the Quad primitive), and assign it to Blood (which will have to be changed to a GameObject, rather than a Texture type).
     
    Zackhanzo likes this.
  3. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Ya! Thanks again @StarManta. The worst of all? Blood Text isnt the typical blood spat, i have already put the bloodspat on my laser script, its like a reference of the hit when the bloodspat goes off. I dont know why i did that if i put the bloodspat properly

    Ill try changing that to see if it works fine
     
  4. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Works fine! But not doing the things that i wanted :p. I mean with the bloodspat that i did yesterday, its dinamic so, after 0.4 seg ill destroy it. But the point of doing this was... Well you shoot, you see the bloodspat during 0.4 and when the bloodspat finish you can see how the enemy has the blood mark on his chest... i thought that if i instantiated the gameobject (blood) it will follow my enemy movement, and it will be permanent until its dead but its not, just instantiated de gameobject on the hit.point.

    Anyone knows how to do this, or how to "look" for a tutorial of this?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    To make the object "stick" to the thing you hit, parent the blood splat to the hit.gameObject.transform like so:
    Code (csharp):
    1.        
    2. GameObject splat = (GameObject)Instantiate (Blood, laserRay.hit.point, Quaternion.FromToRotation (Vector3.up, laserRay.hit.normal));
    3. splat.transform.parent = laserRay.hit.transform;
    4.  
     
    Zackhanzo likes this.