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

Problem with raycastnow! (last message)

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

  1. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Hello guys! im having a small problem. I want to get the vale of a public var in another script.

    So following the Api Scripting guide i made:

    Code (CSharp):
    1. RobotMovement robotMovement=GetComponent<RobotMovement>();
    Where RobotMovement is my main script for movement, animations an it is on the root of the player.

    So... to use conditions of it, i made this on the main script:

    Code (CSharp):
    1. //Var
    2. public bool pistolAim = true;
    3. //On update
    4. //...
    5. if (Input.GetMouseButtonDown (1) && pistol==1) {
    6.  
    7.             animador.SetBool ("PistolAim",true);
    8.             pistolAim=true;
    9.             camera3.enabled=true;
    10.             camera1.enabled=false;
    11.             camera2.enabled=false;
    12. }
    13. //...
    So... i want to use this condition value on my new script which is on another gameobject like this

    Code (CSharp):
    1. RobotMovement robotMovement=GetComponent<RobotMovement>();
    2.         if (robotMovement.pistolAim == true) {
    3.                         lineRenderer.enabled = true;
    4.                         lineRenderer.SetPosition (0, _transform.position);
    5.                         lineRenderer.SetPosition (1, _transform.forward);
    6.                 }
    But i think im not understanding that API Scripting guide, because it seems that robotMovement.pistolAim wont have a "true" value on this new script (Of course in the main script will have that value).

    I tried removing that condition, and linerenderer works flawesly.

    Any idea or suggestion? Maybe a tut for this? Because i cant understand that API Scripting reference.

    Thanks!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Just want to make sure I understand correctly:
    So, you have 2 scripts, your "main script" (let's call it MainScript.cs for clarity; the one whose code is in the middle of your post) and RobotMovement.cs, correct? Or is that code from RobotMovement.cs?

    In any case, I think the answer you're looking for is this: your RobotMovement script is its own object, and it keeps track of its values. So if Script A sets robotMovement.pistolAim to true, then Script B will be able to see robotMovement.pistolAim change to true (assuming they're looking at the same RobotMovement object; that is, assuming they're all on the same GameObject, in this case).

    You can see most public variables in the object's Inspector; can you see the Pistol Aim checkbox in RobotMovement become checked when it's supposed to?
     
    Zackhanzo likes this.
  3. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Ehhm.... RobotMovement.CS is my main script. Which is in the root of the Player Gameobject.

    Then i have all the parts of the body, inside the left hand a pistol, and inside the pistol a cube without mesh, to cast the linerender and later set the raycast to shoot (and change the linerender position 1 to the Raycast hit). This second script is called Laser.CS

    So with:
    1. RobotMovement robotMovement=GetComponent<RobotMovement>();
    and then robotMovement.pistolAim==true on my Laser.cs script im trying to get if the value on the main Script (RobotMovement.CS) is true then... cast a linerenderer.

    So, if they are in differents GameObject you cant track values?

    Because i tried to put that cube ( with the linerender) on my main script (as public gameobject) calling it laser and get the linerenderer from it position to forward, but it didnt work either. So im trying this now.

    I thought set that cube (which contains the linerender) on the same root as my RobotMovement Script is, but it wont work, due to the movement of the animations.

    Thanks for help me again. Thanks to you i know how to aim with a RayCast properly! :p

    PS: PistolAim on my main script (RobotMovement.cs) is true when i get that mouse button pressed and false if i get mouse button up.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You can, you just have to work a little harder to get a reference to it. GetComponent will return a component on the same GameObject. GetComponentInChildren will look through all the transform's children until it finds one. GetComponentInParents will do the same, but will traverse upwards in the hierarchy instead (it sounds like this is the one you want).

    You can get references on unrelated objects by making the reference public:
    Code (csharp):
    1.  
    2. public RobotMovement someOtherRobotMovement;
    3.  
    You will then see a slot in the inspector into which you can drag any RobotMovement-containing object in the scene or your project.

    Finally, you can use GetComponent on just about any object you have a reference to. For example, in a collision, you can do this:
    Code (csharp):
    1.  
    2. void OnCollisionEnter(Collision col) {
    3. RobotMovement iJustCollidedWithThisRobot = col.gameObject.GetComponent<RobotMovement>();
    4. if (iJustcollidedWithThisRobot != null) {
    5. //do something with the robot you just collided with
    6. }
    7. }
    8.  
    And with raycasts (since you're doing lasers, I suspect this will come in handy for you shortly):
    Code (csharp):
    1.  
    2. void Update() {
    3. RaycastHit hit;
    4. if (Physics.Raycast(transform.position, Vector3.forward, out hit)) {
    5. EnemyScriptName someEnemy = hit.collider.GetComponent<EnemyScriptName>();
    6. someEnemy.health = someEnemy.health - 5f;
    7. }
    8. }
    9.  
     
    Zackhanzo likes this.
  5. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Thanks. it worked! And thanks for the advices from the colliders and the Raycast. I use the colliders stuff and all ok But just one last question.

    I tried to use that Raycast thing...

    Code (CSharp):
    1. void Fire()
    2.         {
    3.             if(cooldown > 0) {
    4.                 return;
    5.  
    6.                 }
    7.         RaycastHit hit;
    8.  
    9.         //Ray ray = new Ray (_transform.position,_transform.forward);
    10.  
    11.         if (Physics.Raycast (_transform.position,_transform.forward,  out hit, distance) && hit.transform.gameObject.tag == ("Enemy")) {
    12.                  
    13.                     //  Debug.DrawRay
    14.                         Debug.Log (hit.collider.gameObject.name);
    15.  
    16.                         audio.Play ();
    17.      
    18.      
    19.                         HealthEnemy h;
    20.                         h = hit.transform.GetComponent<HealthEnemy> ();
    21.                         h.TakeDamage (damage);
    22.      
    23.      
    24.      
    25.      
    26.                 } else {
    27.  
    28.             //Debug.DrawRay ;
    29.             audio.Play ();
    30.                 }
    31.  
    32.         cooldown = fireRate;
    33.         }
    I worked with Ray parameter too (Ray ray=new Ray(_transform.postion,_transform.forward), but it seems that i cant figure out how to calculate the direction that im using to shoot in the same spot of the linerenderer.

    We got Linerenderer= with 2 positions. We got Raycast with initial position (_transform.position) and direction _transform.forward.

    So i dont understand why im aiming to another side. I used two Debug Rays for seeing where. I did that without seeing your answer, and now im seeing that some things changed but the RayCast thing position and direction are the same parameters, and i dont aim in the same spot as the linerenderer :S . (tried, changing _transform.forward and putting Vector3.forward) Too weird.

    The linerenderer was:

    Code (CSharp):
    1. lineRenderer.SetPosition (0, _transform.position);
    2.                         lineRenderer.SetPosition (1, _transform.forward);
     
    Last edited: Jul 11, 2014
  6. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Right now my code. The current problem is that Raycast doesnt point on the same direction of my linerenderer.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class laser : MonoBehaviour {
    5.  
    6.     //Tons of variables arent implemented yet
    7.  
    8.  
    9.     Color c1  = Color.yellow;
    10.     Color c2 = Color.red;
    11.     LineRenderer lineRenderer ;
    12.     public LayerMask MyLayerMask;
    13.     public float distance=20;
    14.     public float damage=20;
    15.     float cooldown=0;
    16.     public float fireRate=0.5f;
    17.     GameObject gunShot;
    18.     public float bulletSpeed;
    19.     public GameObject bullet = null;
    20.     public GameObject bulletHoleWall;
    21.     public GameObject bulletBlood;
    22.     private Transform _transform;
    23.  
    24.  
    25.  
    26.  
    27.     // Use this for initialization
    28.     void Start () {
    29.         _transform = gameObject.transform;
    30.         lineRenderer = gameObject.GetComponent<LineRenderer>();
    31.  
    32.         lineRenderer.SetColors(c2,c2);
    33.         lineRenderer.SetWidth(0.015F,0.08F);
    34.         lineRenderer.SetVertexCount(100);
    35.         lineRenderer.useWorldSpace = false;
    36.  
    37.  
    38.  
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update () {
    43.  
    44.  
    45.         RobotMovement robotMovement=GetComponentInParent<RobotMovement>();
    46.         if (robotMovement.pistolAim == true) {
    47.                         lineRenderer.enabled = true;
    48.                         lineRenderer.SetPosition (0, _transform.position);
    49.                         lineRenderer.SetPosition (1, _transform.forward);
    50.                      
    51.                 }
    52.         // Just for checking puroposes
    53.         //if (robotMovement.pistolAim == false) {
    54.         //    lineRenderer.enabled=false;
    55.         //        }
    56.  
    57.         cooldown -= Time.deltaTime;
    58.         if (robotMovement.pistolAim==true && robotMovement.pistolShoot==true)
    59.         {
    60.             lineRenderer.enabled=false;
    61.  
    62.             Fire();
    63.         }
    64.  
    65.  
    66.  
    67.  
    68.     }
    69.  
    70.  
    71.  
    72.     void Fire()
    73. //All conditions works fine,cooldown and firerate stuff too, but raycast dont shoot on the same direction as my linerenderer.
    74.         {
    75.             if(cooldown > 0) {
    76.                 return;
    77.  
    78.                 }
    79.         RaycastHit hit;
    80.  
    81.         //Tried with ray, as ray, out hit,distance on the Physics.Raycast stuff, not aiming on the same spot as my linerenderer
    82.         //Ray ray = new Ray (_transform.position ,_transform.forward );
    83.         //Tried with Vector3.forward too as 2nd parameter of the Raycast. Didnt work
    84.  
    85.         if (Physics.Raycast (_transform.position,_transform.forward,  out hit, distance) && hit.transform.gameObject.tag == ("Enemy")) {
    86.                      
    87.                       Debug.DrawRay (_transform.position,_transform.forward,Color.red);
    88.                       Debug.Log (hit.collider.gameObject.name);
    89.                       audio.Play ();
    90.          
    91.                     //Works fine
    92.                     HealthEnemy h;
    93.                     h = hit.transform.GetComponent<HealthEnemy> ();
    94.                     h.TakeDamage (damage);
    95.          
    96.          
    97.          
    98.          
    99.                 } else {
    100.  
    101.             Debug.DrawRay (_transform.position,_transform.forward,Color.green);
    102.             audio.Play ();
    103.                 }
    104.  
    105.         cooldown = fireRate;
    106.         }
    107.  
    108.          
    109.          
    110.          
    111.          
    112.      
    113. }
     
    Last edited: Jul 11, 2014
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    transform.forward gives you a direction vector; LineRenderer uses position vectors, so you're drawing a line from somewhere in the world, to somewhere within 1 unit of the origin.

    Code (csharp):
    1. lineRenderer.SetPosition (0, _transform.position);
    2. lineRenderer.SetPosition (1,_transform.position + _transform.forward * 50f); //it'll go 50 units forward
     
    Last edited: Jul 11, 2014
    Zackhanzo likes this.
  8. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    I think you arent understanding the linerender that im trying to use. If you remember a tps like Resident Evil 4. The pistol has a laser, who aims from the laser point and that line go straight forward until it hits something (i will detect the hit.point with another raycast later on).

    So if i make _transform.position+_transform.forward +50f, ill make a point like a crosshair due to linerenderer.

    But If i put:



    Code (CSharp):
    1. lineRenderer.SetPosition (0, _transform.position);
    2.   lineRenderer.SetPosition (1, _transform.forward);
    I get something like this:

    linerenderer Screen:

    https://imageshack.com/i/nlhb8tj

    And its good to me (i mean the line renderer, not the best position, and not the best width lol).

    So with the linerenderer im drawing a line from point position 0 to position 1 that go all straight.


    But the problem that i have is with the Raycast. I want shoot a ray, who cover through the distance that linerenderer. So... if i think, i have a clear position point called _transform.position, and i have a clear direction that is _transform.forward so i dont have a clue why my Raycast is not following that linerenderer line.

    Raycast screen (_transform.position, transform.forward out hit....)
    DebugRay (_transform.position, _transform.forward);
    https://imageshack.com/i/n1yl22j

    It seems that i have a problem with the direction of the raycast

    Sorry for wasting your time again ^^
     
    Last edited: Jul 11, 2014
  9. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Found the problem and fixed it ! i had to relax the brain just a little. Thanks for all the help @StarManta