Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

camera.main.transform.forward

Discussion in 'Scripting' started by kittik, Jul 28, 2015.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Hi,

    I am trying to fire a raycast from the centre of my screen and am experiencing problems. I've attached a video and code.



    ^ Although the video is short and very quick, it should show that the projectile is being shot from my position, but not factoring in any direction.

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0))
    2.         {
    3.             var effectInstance = Instantiate(SomeEffect, Camera.main.transform.position, new Quaternion()) as GameObject;
    4.             var effectSettings = effectInstance.GetComponent<EffectSettings>();
    5.             effectSettings.UseMoveVector = true;
    6.             effectSettings.MoveVector = Camera.main.transform.forward + Camera.main.transform.position; //or you can use effectSettings.MoveVector = Camera.main.transform.forward + Camera.main.transform.position
    7.         }
    Do I need to do anything special to assign the rotation of my camera? That is the only thing I think I am missing here, but I have had someone look at the code and verify that in their opinion everything should be working fine.
     
  2. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    I don't know what library you use for particle effect and how does it work but it seems very suspicious that your MoveVector value is sum of position and direction vectors.

    Try
    Code (csharp):
    1. MoveVector = Camera.main.transform.forward
    or
    Code (csharp):
    1.  
    2. var effectInstance =Instantiate(SomeEffect, Camera.main.transform.position, Camera.main.transform.rotation) as GameObject;
    3.  
     
    kittik likes this.
  3. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Thank you very much mholub, everything works perfectly now. :)