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 Evil-Lemons · Apr 25, 2014 at 10:25 PM · positionboo

Check for empty space relative to an object

I have an enemy AI. Right now, the AI simply makes the enemy move towards the player. The problem with it is that the center of the player is taller than the enemy, causing the enemy to float in mid-air. Here is my enemy AI. Yes, this is Boo:

 import UnityEngine
 
 class EnemyAI (MonoBehaviour): 
     public target as Transform
     
     private myTransform as Transform
     
     public rotateSpeed as int
     public moveSpeed as int
     public maxDistance as single
     
     private defaultYPos as single
     
     
     def Awake():
         myTransform = transform
 
     def Start ():
         go as GameObject = GameObject.FindGameObjectWithTag('Player')
         target = go.transform
         
         defaultYPos = transform.position.y
         
         rotateSpeed = 5
         moveSpeed = 3
         maxDistance = 2.3
         
     def Update ():
         Debug.DrawLine (target.position, myTransform.position, Color.yellow);
 
         
         //Look at the target
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), (rotateSpeed * Time.deltaTime))
         
         //If it isn't too close
         if Vector3.Distance(target.position, myTransform.position) > maxDistance:
             //Move towards the target
             myTransform.position += ((myTransform.forward * moveSpeed) * Time.deltaTime)
         
         if transform.position.y != defaultYPos:
             transform.position.y = defaultYPos

As a temporary fix, I locked the y position. Since I can't do that for ever, my thought is to check "If the unit under myself is free, move down. Else, don't."

So, how would you check for empty space relative to itself?

Since I highly doubt anyone knows Boo, you can answer in C#. I know basic C# syntax, so I can convert it over. Thanks in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by getyour411 · Apr 25, 2014 at 10:51 PM

I do it like this (basically a Y work around too) and it's working fine for me so far

 Vector3 lookDir = (myTarget.position - myTransform.position);
 lookDir.y = 0f;
 myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (lookDir), rotationSpeed * Time.deltaTime);

I don't do that in Update() though since I don't need it calculated every frame (except when the player is in range)

Comment
Add comment · Show 5 · 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 Evil-Lemons · Apr 25, 2014 at 11:28 PM 0
Share

I see where you're going with that, and it worked. But it still raises one problem: What if the y value needs to change, as in if the player goes up a slant? $$anonymous$$aybe you could change lookDir.y/transform.position to something based on the player?

avatar image getyour411 · Apr 25, 2014 at 11:29 PM 0
Share

I've been play testing my game for some time and it's never been a problem visually for me and my test terrain is covered with hills and valleys. If you wanted to get fancy, assu$$anonymous$$g the enemy has a neck bone you could grab a reference to that in your script and rotate that.

avatar image Evil-Lemons · Apr 25, 2014 at 11:37 PM 0
Share

I'm talking about the transform.position. Say the player moves up a hill, the enemy wants to move up as well. $$anonymous$$y idea is that every frame, it checks if there is open space below it, similar to an isGrounded check. If there is open space, it would move down. But how would you check for open space below itself?

avatar image getyour411 · Apr 25, 2014 at 11:41 PM 0
Share

Just noticed you are using transform.position to move. I'll wreck your day by telling you don't do that I suppose, but you'll want to invest the time in that otherwise you'll have all kinds of issues.

Do some searching on Enemy movement with rigidbody or charactercontroller.

avatar image Evil-Lemons · Apr 25, 2014 at 11:45 PM 0
Share

Aha, I just tried a rigidbody and it worked. Ill look into the alternatives too. It works, but it is pretty glitchy. Thanks!

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

20 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

Related Questions

Camera rotation around player while following. 6 Answers

How to access position of prefab in the Game Object field? 1 Answer

transform.SetPositionAndRotation(0, 0, 0),SetPositionAndRotaion 3 Answers

GameObject position and orientation in the 3D environment. 1 Answer

How to make a gameObject Transform Position move in a circle while making its Transform Position move forward? 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