• 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 wisien92 · May 10, 2014 at 01:54 PM · raycasthitlooking

Raycast - enemy does not look forward but into the same point ...

I have no idea why the emeny sends raycast into the same targetPosition .....

here is the code :

     //Send raycast to imitate enemy sight
 void SendRaycast() {
     //Start and end point of raycasts
     Vector3 startPosition = transform.position;
     Vector3 targetPosition = Vector3.zero;

     //Start and end angle of raycasts
     int startAngle = (int)(-angle * 0.5F);  // half the angle to the Left of the forward
     int finishAngle = (int)(angle * 0.5F); // ..... and to the Right

     int inc = (int)( angle / 10 );

     //raycast that we are going to 'send'
     RaycastHit hit;

     // step through and find each target point
     for(int i = startAngle; i < finishAngle; i += inc ) // Angle from forward
     {
         targetPosition = (Quaternion.Euler( 0, i, 0 ) * transform.forward ).normalized * 5.0f;
         //targetPosition = transform.forward;

         // linecast between points
         if ( Physics.Linecast( startPosition, targetPosition, out hit ) )
         {
             if(hit.collider.gameObject.tag == "Player") {
                 chase = true;
                 Debug.Log("See Player");
             }
         }            
         // to show ray just for testing
         Debug.DrawLine( startPosition, targetPosition, Color.green );
     } 
 }


what i struggle with

problem.png (22.8 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
Best Answer

Answer by robertbu · May 10, 2014 at 02:07 PM

A LineCast() takes two positions. You are supply it with a position and a direction. I believe you want a Raycast instead, which does take a position and a direction.

    if ( Physics.Raycast( startPosition, targetPosition, out hit ) )

In the unlikely event you really do want a Linecast(), you'd have to decide on a distance in front. Then you could add just before the Linecast:

 targetPosition = startPosition + targetPosition * someDistance;

    
Comment
Add comment · Show 3 · 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 wisien92 · May 10, 2014 at 02:15 PM 0
Share

but still when he moves the drawlines indicate that the targetPosition isn't changing but the startPosition is

avatar image wisien92 · May 10, 2014 at 02:21 PM 0
Share

i added image showing my problem

avatar image wisien92 · May 10, 2014 at 02:47 PM 0
Share

yeah my bad - raycast works :P

avatar image
0

Answer by Hachley · May 10, 2014 at 02:36 PM

I have something an old project of mine, where the enemy was facing the player and later chased him. Here's what I used for the enemy to smoothly turn at the target and face him:

 var target : Transform;
 var damp = 6.0;

 function lookAtTarget() {
     var rotation = Quaternion.LookRotation(target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damp); //turn smoothly
 }

It's JS though. Hope it helps.

Comment
Add comment · Show 1 · 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 wisien92 · May 10, 2014 at 02:40 PM 0
Share

yeah i am working now on a little bit more complicated ... i want the enemy to chase the player and look at him when he sees him ... that why i wanted to send raycast/linecast across angle

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Can someone modify this for me? 0 Answers

Help with LineRenderer 1 Answer

NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer

Debug.Drawline position is slightly over the clicked position 0 Answers

Damage animation with ragdoll 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges