Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ZackaryVS · Jun 19, 2014 at 12:49 AM · 2draycastaitopdownavoidance

Raycast AI - Avoiding Obstacles in 2D Topview (Using Velocity)

I need help with a script I've been working on. I got the 2D enemy to follow my character, but I'm stuck at trying to make him avoid things with Raycasts. I don't want him to rotate, because I'm doing a top-down view in 2D. I just want him to change his velocity on the X or Y axis when he approaches a wall. Now, I did write a partial script of him doing that, but I'm too embarrassed to post it because it kinda sucks. (If you want to see it, I can post it in a comment if necessary.) So I will just post the script that makes him follow the player without avoiding things.

Any help would be appreciated.

Thanks

My Script in UnityScript:

 #pragma strict
 //Stuck boolean used for raycast avoiding
 //obstacles, I left it here so you can see
 //below how I was using it.
 var stuck : boolean;
 //Storing player's transform into this variable.
 var player : Transform;
 
 //Distance between player and enemy. 
 //May be useful for figuring out if 
 //he should walk up or down if stuck 
 //at X axis or Y axis. 
 var xDistance : float;
 var yDistance : float;
 
 var moveSpeed : float;
 
 var anim : Animator;
 //Separating X and Y velocities to tell which
 //direction the enemy should go.
 var xVelocity : float;
 var yVelocity : float;
 
 //Boolean for telling the animator when to play
 //idle animation.
 var isIdle : boolean;
 
 //When the enemy is alerted, follow the player
 //(haven't coded that yet, but that's not what I need help with)
 var follow : boolean;
 
 function Start () {
     anim = GetComponent(Animator);
     player = GameObject.FindGameObjectWithTag("Player").transform;
     follow = true;
 }
 
 function FixedUpdate () {
     
     //If both X and Y velocities are at 0
     //set Idle animation to true.    
     if(xVelocity == 0 && yVelocity == 0)
         isIdle = true;
     else
         isIdle = false;
     
     anim.SetBool("isIdle", isIdle);
     
     //Calculating distance from enemy and player.
     xDistance = transform.position.x - player.position.x;
     yDistance = transform.position.y - player.position.y;
 
 
     if(follow){    
     
         //Setting boolean operations to the position of the enemy.
         //Makes it easier to make if statements for where the enemy
         //should follow the player.
         var right : boolean;
         var left : boolean;
         var up : boolean;
         var down : boolean;
 
         if(transform.position.x > player.position.x)
             right = true;
         else
             right = false;
             
         if(transform.position.x < player.position.x)
             left = true;
         else
             left = false;
             
         if(transform.position.y > player.position.y)
             up = true;
         else
             up = false;
             
         if(transform.position.y < player.position.y)
             down = true;
         else
             down = false;
 
     //Following the player.
     if(right && !stuck)
         xVelocity = -moveSpeed;        
     
     if(left && !stuck)
         xVelocity = moveSpeed;
 
     //It bugs out when the player 
     //and enemy is on the same X and Y position.
     if(Mathf.Round(transform.position.x) == 
     Mathf.Round(player.transform.position.x))
         xVelocity = 0f;
         
     if(down && !stuck)
         yVelocity = moveSpeed;
     
     if(up && !stuck)
         yVelocity = -moveSpeed;
     
     //Fixing the wierd thing again for Y position.
     if(Mathf.Round(transform.position.y) == 
     Mathf.Round(player.position.y) && !stuck)
         yVelocity = 0f;
 }
     //Setting the velocities for the animator.    
     anim.SetFloat("xVelocity", xVelocity);
     anim.SetFloat("yVelocity", yVelocity);
     //Using the X and Y velocity variables above to set
     //rigidbody velocity. 
     rigidbody2D.velocity = Vector2(xVelocity, yVelocity);
 }
 
Comment
Add comment · Show 1
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 OliverParkinson · Jan 20, 2017 at 01:30 AM 0
Share

Yeah I'm trying to do the same thing too. (I know this is an old question, but I would just like to see what other people have done to try solving the same problem.) I'm writing it in C#, but I have no idea what i'm doing.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baron_of_Hell · Jul 29, 2014 at 06:54 PM

Could you show the avoid collision one?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I make an AI that avoids obstacles WITHOUT using NavMesh? 3 Answers

Detect tag with 2d raycasting 1 Answer

Unity 2D Top-Down Raycast length/distance and direction problem 0 Answers

2D Field of View 2 Answers

Raycast Steering Problem 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