Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Shabalaka · Feb 14, 2018 at 10:51 AM · drawray

draw ray weird outcome.

Hi Guys,

I have been trying to do this for a couple of days now and cant seem to get this to work so went back to the drawing board and realised that the "enemy" does move towards the player when player is in range but when the enemy attempts to shoot the player the ray just shoots down through the ground(See attached image)

 ![using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class EnemyAI : MonoBehaviour {
 
     Transform target;
 
     public float aggro_radius = 250f;
 
     NavMeshAgent mesh_agent;
 
     public GameObject mesh_carrier;
 
     public LineRenderer laser;
 
     public float laser_shoot_intervals;
 
     public float laser_damage;
 
     public GameObject enemy_ship;
 
     public bool in_range = false;
 
     // Use this for initialization
     void Start () {
 
         target = PlayerManager.instance.player.transform;
         mesh_agent = mesh_carrier.GetComponent<NavMeshAgent> ();
         laser.useWorldSpace = true;
 
 
 
             
     }
     
     // Update is called once per frame
     void Update () {
 
         float distance = Vector3.Distance (target.position, transform.position);
 
         if (distance <= aggro_radius) {
 
 
             Ray Laser = new Ray (laser.transform.position, target.position);
 
 
             Debug.DrawRay (transform.position, target.position);
             Debug.Log ("Drawing Ray from " + transform.position + " to " + target.position);
 
 
         } 
 
 
     }
 
 
 
 
     void OnDrawGizmosSelected()
     {
 
 
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere (transform.position, aggro_radius);
 
 
 
     }
 
 
     void checkForCollision(Ray _laserRay)
     {
         RaycastHit hit;
 
         if (Physics.Raycast (_laserRay, out hit, 100F)) {
             if (hit.rigidbody) {
                 Debug.Log ("We hit " + hit.collider.tag);
 
             }
 
         }
 
 
 
 
     }
         
 
 }][1]
 


[1]: /storage/temp/111279-raydraw.jpg

As you can see the raycast is drawing below the ground, Am not sure why this is when it is receivng the same values as mesh_agent.SetDestination() which seems to work fine.

Many Thanks!!

raydraw.jpg (347.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Zodiarc · Feb 14, 2018 at 11:41 AM

From the Docs (https://docs.unity3d.com/ScriptReference/Ray.html):

You give the constructor wrong parameters. Your assumption is that the second argument is the target position, but it's not. It's the direction.

Change it to

 Ray laser = new Ray(laser.transform.position, (laser.transform.position - target.position))

and see if it works (do the same in the Debug.DrawRay call). If it goes the opposite direction just swap it to (target.position - laser.transform.position)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shabalaka · Feb 14, 2018 at 10:56 PM 0
Share

Thanks for the reply I have tried to use

         Ray laser = new Ray (laser.transform.position, (laser.transform.position - target.position));

but i get an error

 Assets/EnemyAI.cs(46,31): error CS1061: Type `UnityEngine.Ray' does not contain a definition for `transform' and no extension method `transform' of type `UnityEngine.Ray' could be found. Are you missing an assembly reference?

Any ideas why it doesnt seem to recognise a transform.position in the Ray declaration??

Also when i put the following

 Debug.DrawRay(laser.transform.position, (laser.transform.position - target.position));

The Line Renderer laser laser.transform.position (transform.position appears in red writing like it,s an error or something)

thanks again!

avatar image Bunny83 Shabalaka · Feb 14, 2018 at 11:10 PM 0
Share

In your original code you already have a class member called "laser" and you named your ray "Laser" (note that capital "L"). Zodiarc accidentally named his Ray "laser" while it should be "Laser".


Though it would be better to use a more descriptive variable name. Since you use the ray only inside the method you usually call it just "ray".

avatar image Bunny83 · Feb 14, 2018 at 11:06 PM 0
Share

It goes in the wrong direction. Basic vector math: The direction vector from origin to target is always "origin - target". Or generally "tip/head $$anonymous$$us tail".

avatar image Zodiarc Bunny83 · Feb 15, 2018 at 03:07 PM 0
Share

I never remember that so I always try it out and flip it if necessary.

avatar image
0

Answer by Shabbalaka · Feb 15, 2018 at 08:33 AM

Many thanks for the replies!

Was silly not to notice the laser declaration... This is only my 1st 3d project in Unity so am trying to learn. :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class EnemyAI : MonoBehaviour {
 
     Transform target;
 
     public float aggro_radius = 10f;
 
     NavMeshAgent mesh_agent;
 
     public GameObject mesh_carrier;
 
     public LineRenderer laser;
 
 
     // Use this for initialization
     void Start () {
 
         target = PlayerManager.instance.player.transform;
 
         mesh_agent = mesh_carrier.GetComponent<NavMeshAgent> ();
 
 
         //Start The Coroutine for firing at the player.
         StartCoroutine (fire ());
 
 
     }
     
     // Update is called once per frame
     void Update () {
 
         float distance = Vector3.Distance (target.position, transform.position);
 
         if (distance <= aggro_radius) {
 
 
             mesh_agent.SetDestination (target.position);
 
         
 
 
         }
 
 
     }
 
 
 
 
     void OnDrawGizmosSelected()
     {
 
 
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere (transform.position, aggro_radius);
 
 
 
     }
 
 
 
     IEnumerator fire()
     {
 
 
 
         Ray laser_ray= new Ray (transform.position, ( target.position - laser.transform.position));
 
         //This goes from the ship object position to the target and then back again.
         Debug.DrawRay (laser.transform.position, ( target.position - laser.transform.position));
 
         laser.SetPosition (0, transform.position);
     
         laser.SetPosition (1, new Vector3(Random.Range(target.position.x,target.position.x + 100f),target.position.y,target.position.z));
         //Did we hit the player??
         checkForCollision (laser_ray);
 
         //Wait for 1 second before running the laser script again and regenerating another random X location to fire the laser.
         yield return new WaitForSeconds (1);
 
 
 
 
 
     }
 
     void checkForCollision(Ray arg_laserRay)
     {
         RaycastHit hit;
 
         if (Physics.Raycast (arg_laserRay, out hit, 100F)) {
             if (hit.rigidbody) {
                 Debug.Log ("We hit " + hit.collider.tag);
 
 
             }
 
         }
     }
 
 }


I am finding it really hard to implement CoRoutines and use them as required, As you can see I am looking to make the enemy fire at the player and then adjust the enemys fire trajectory otherwise the enemy would have an instant "lock-on" effect on the player.

If someone can point out where this is going wrong it would be muchly appreciated!

Example : Player comes too close to enemy, enemy then faces towards the player and starts firing every 1 second or a "variable" duration.

1st laser fire goes to the left of the player and misses. 2nd laser fire goes to the right of player and misses. 3rd one manages to hit the player.

Hope this gives enough infomration as to what im trying to do ! : )

Many thanks!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast not drawing 1 Answer

LayerMask, DrawRay and DrawLine acting weird... 1 Answer

Call an exorcist. 1 Answer

Rotation with Raycasting 1 Answer

Raycast doesn't work properly? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges