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 Silentones · Apr 08, 2019 at 05:00 AM · rotationraycastunity 2denemypatrol

How to rotate the raycast on a patrolling enemy? (In front of the enemy at all times)

I am having trouble rotating my raycast (LineOfSite) on my enemy player. So when the enemy is patrolling the area. The player is able to steps on the ray cast, behind it because it set it up to point out left, and make them stop moving. Is it possible to flip the raycast like you flip the player? I tried transforming.Left and localscale and i end up getting errors. NOTE: I am trying to keep the line in front of the enemy's face

Code: https://hatebin.com/scrbhqbkrc

Youtube: Video

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Patrol : MonoBehaviour
 {
     public float speed;
     public float raylength, LosDis;
     private Transform TPlayer;
     private bool movingLeft = true;
     public Transform detection;
 
 
     // Start is called before the first frame update
     void Start()
     {
         Physics2D.queriesStartInColliders = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         patrolling();
         LineofSight();
     }
 
     public void patrolling()
     {
         transform.Translate(Vector2.left * speed * Time.deltaTime);
 
         RaycastHit2D ground = Physics2D.Raycast(detection.position, Vector2.down, raylength);
         if (ground.collider == false)
         {
             if (movingLeft == true)
             {
                 transform.eulerAngles = new Vector3(0, -180, 0);
                 movingLeft = false;
             }
             else
             {
                 transform.eulerAngles = new Vector3(0, 0, 0);
                 movingLeft = true;
             }
         }
     }
 
     public void LineofSight()
     {
         RaycastHit2D enemyspoted = Physics2D.Raycast(transform.position, transform.forward, LosDis);
         if (enemyspoted.collider != null) {
             print("Player hit!");
             Debug.DrawLine(transform.position, enemyspoted.point, Color.red);
             TPlayer = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
             transform.position = Vector2.MoveTowards(transform.position, TPlayer.position, speed * Time.deltaTime);
             
         }else {
             Debug.DrawLine(transform.position, transform.position + Vector3.left * LosDis, Color.green);
         }
 
 
     }
 
 
 }//patrol
 


Comment
Add comment · Show 5
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 Casiell · Apr 08, 2019 at 07:11 AM 0
Share

First question is, why do you draw your line with different parameters than your raycast? Also your code looks good, I don't really see the problem, maybe double check if you are using the correct axis?

The easiest workaround would be to use directions in world space depending on your movingLeft bool, but that's ugly, your current solution is a lot better (if it worked)

avatar image Silentones Casiell · Apr 10, 2019 at 04:40 AM 0
Share

I have two different raycas. 1patrol and 2. the Line of Sight for the enemy. The 2nd one is what i am having trouble with.

avatar image xxmariofer · Apr 08, 2019 at 07:54 AM 0
Share

your code seems fine just change Vector3.left to transform.forward in the last drawline method, can you try debug.log transform.forward and make sure its actually changing? you are NEVER changing LosDis value to negative righ?

avatar image Silentones xxmariofer · Apr 10, 2019 at 04:38 AM 0
Share

When I use tranform.forward my debug line disappears and it also doesn't activate the chase when the player collides into the line of sight. Also I am not changing my LosDis value

avatar image xxmariofer Silentones · Apr 10, 2019 at 07:31 AM 0
Share

that makes no sense, whats printing if you debug.log transform.forward?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Luvarv · Apr 10, 2019 at 07:50 AM

I am guessing this is a side scroller. You might be able to use something like:

 new Vector 2 (dir, 0)

instead of:

 transform.forward

and set "dir" to -1 when looking left and to 1 when looking right.

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 xxmariofer · Apr 10, 2019 at 08:36 AM 0
Share

it makes no sense his error, it cant be a side collider since when patrolling when changing the direction he is changing the transform.rotation by 180*, so transform.forward should be affected to, or i am missing something?

avatar image Silentones xxmariofer · Apr 11, 2019 at 12:16 AM 0
Share

I'm sorry it is a 2D platform game. I should have specified that in the beginning.

avatar image Silentones · Apr 11, 2019 at 05:32 AM 0
Share

That gave me an Error

 public void LineofSight()
     {
         RaycastHit2D enemyspoted = Physics2D.Raycast(transform.position, new Vector2(Dir, 0), LosDis);
         if (enemyspoted.collider != null)
         {
             Debug.DrawLine(transform.position, enemyspoted.point, Color.red);
 
             if (enemyspoted.collider.CompareTag("Player")) {
                 TPlayer = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
                 transform.position = Vector2.$$anonymous$$oveTowards(transform.position, TPlayer.position, speed * Time.deltaTime);
             }
 
         }
         else
         {
             Debug.DrawLine(transform.position, transform.position + Vector3.left * LosDis, Color.green);
         }
 
 
     }

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

192 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 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 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

Enemy stops patrolling when player comes close 2 Answers

My raycasts rotate weirdly around their 2DboxCollider, they try to match the starting position of the collider *instead* of matching the new position when rotating 1 Answer

Y rotation of object based on center of a sphere? 0 Answers

Raycast gun 2 Answers

Infinite Raycast? 1 Answer


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