Search Unity

Quick question of an enemy shootin an object

Discussion in 'Scripting' started by Lufford, Jan 28, 2018.

  1. Lufford

    Lufford

    Joined:
    Jan 28, 2018
    Posts:
    3
    Hi, I just started reading Unity In Action and the first example has something that Im dealing with.
    Here's the thing, there's just an enemy that shoots an object if you are staring at him.

    RaycastHit hit;
    if (Physics.SphereCast(ray, 0.75f,out hit)) {
    GameObject hitObject = hit.transform.gameObject;
    if (hitObject.GetComponent<PlayerCharacter> ()) {
    if (fireball == null) {
    fireball = Instantiate (fireballPreFab) as GameObject;
    fireball.transform.position = transform.TransformPoint (Vector3.forward * 1.5f);
    fireball.transform.rotation = transform.rotation;
    }

    That's the code and the problem is that the fireball instantiates with half of his own body withing the floor and Im sure that this is why the ball dissapears at the very same moment that appears.

    I hope you can help me out guys!
     
  2. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Could you please use code tags :). And when instantiating you should use vector 3 instead of transformPoint
     
  3. Lufford

    Lufford

    Joined:
    Jan 28, 2018
    Posts:
    3
    Ive been using the same code that is in the book, so I'd like to believe that it was tested before.
    But anyway, if I want to instantiate the fireball right in front of the enemy, you say that I should not use transformPoint? I think that the issued Is the y-position is wrong.
    Thx!
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,205
    There's nothing wrong with using TransformPoint since it returns a Vector3.

    Are you sure the origin point of both objects are the same? It sounds like your character's origin is at their feet but the fireball's origin is in the middle (or thereabouts). You might need to add some height to the fireball's position when you spawn it:

    Code (csharp):
    1.  
    2. fireball.transform.position = transform.TransformPoint (Vector3.forward * 1.5f) + new Vector3(0f, 2.0f, 0f); // change 2 to whatever the necessary height is for your character
    3.  
    Also, please use code tags going forward.
     
    Lufford likes this.
  5. Lufford

    Lufford

    Joined:
    Jan 28, 2018
    Posts:
    3
    It worked!! Thank you very much GroZZleR.
     
    GroZZleR likes this.
unityunity