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 Supershandy · Feb 11, 2014 at 02:29 PM · javascriptvector3aidirectionlookat

How to get object to face direction it's flying rather than looking at the target

I wrote some of my own AI code using some help from a source on the internet, It works fine including with the floating origin solution for large game worlds, problem is however is that the AI will always face the target rather than face the direction in which it is travelling.

I have provided a picture to explain what i'm trying to do.

alt text

And the code that is being used, I have only done it up to the Seek behavior as all the behaviors use the same basic template.

 #pragma strict
 
 //Positional Values
 
 var position : Vector3;
 var desiredVelocity : Vector3;
 var velocity : Vector3;
 var steering : Vector3;
 var avoidance : Vector3;
 var direction : Vector3;
 var targetFuturePosition : Vector3;
 
 //Speed, Mass and Force Values
 
 var mass : float;
 var maxVelocity : float;
 var maxForce : float;
 var speedx : float;
 var speedy : float;
 var speedz : float;
 var distance : float;
 var slowingRadius : float;
 
 //Target Holders
 
 var target : Transform;
 
 //Decision Holders
 
 var avoidObject : boolean = false;
 
 function OnTriggerStay (collider : Collider)
 
 {
     avoidObject = true;
     Avoid(collider.gameObject);
 }    
 
 function OnTriggerExit ()
 
 {
     avoidObject = false;
 }
 
 function Avoid (obstacle : GameObject)
 
 {    
     avoidance = (transform.position - obstacle.transform.position).normalized * maxVelocity;;
     steering = avoidance - velocity;
     steering = steering * maxForce;
     steering = steering / mass;
     velocity = (velocity + steering).normalized * maxVelocity;
     position = transform.position + velocity;
     direction = transform.position + velocity;
     speedx = velocity.x;
     speedy = velocity.y;
     speedz = velocity.z;
     transform.Translate (speedx * Time.deltaTime, speedy * Time.deltaTime, speedz * Time.deltaTime);
 }
 
 
 function Seek () 
 
 {
     desiredVelocity = (target.position - transform.position).normalized * maxVelocity;
     steering = desiredVelocity - velocity;
     steering = steering * maxForce;
     steering = steering / mass;
     velocity = (velocity + steering).normalized * maxVelocity;
     position = transform.position + velocity;
     direction = transform.position + desiredVelocity;
     speedx = velocity.x;
     speedy = velocity.y;
     speedz = velocity.z;
     transform.Translate (speedx * Time.deltaTime, speedy * Time.deltaTime, speedz * Time.deltaTime);
 }

and this code is to make the object look in the direction the AI brain is facing, i.e, which direction it's looking at.

 #pragma strict
 
 var brain : Transform;
 var brainDirection : Vector3;
 
 function Start () 
 
 {
 
 }
 
 function Update () 
 
 {
     //brainDirection = brain.GetComponent(Seek).direction;
     brainDirection = brain.GetComponent(Behaviours).direction;
     transform.LookAt(brainDirection);
 }

Any solutions?

aiproblems.jpg (53.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

Answer by whydoidoit · Feb 11, 2014 at 02:36 PM

If brainDirection is a direction then use Quaternion.LookRotation which takes a direction - LookAt looks at a place in the world.

    transform.rotation = Quaternion.LookRotation(brainDirection);
Comment
Add comment · Show 2 · 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 Supershandy · Feb 11, 2014 at 02:38 PM 0
Share

I tried that, but it did exactly the same thing

avatar image whydoidoit · Feb 11, 2014 at 02:40 PM 0
Share

Well it did something different but had the same effect I guess :)

Better Debug.Log the brainDirections being returned.

avatar image
0

Answer by Gnometech · Feb 11, 2014 at 02:41 PM

It seems that you are confusing direction vectors with target vectors.

If I read your code correctly, brainDirection is a directional vector that you imagine as attached to your AI. If you call "transform.lookAt" with this vector as a parameter, unity will intepret it as an absolute vector instead and point the object there.

Solution: Add the direction to your current location and this will be your target to look at.

 function Update () 
  
 {
     //brainDirection = brain.GetComponent(Seek).direction;
     brainDirection = brain.GetComponent(Behaviours).direction;
     transform.LookAt(brainDirection + transform.position);
 }
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 Supershandy · Feb 11, 2014 at 03:04 PM 0
Share

Hmm, still has the same problem, though I have to change it to a $$anonymous$$us to get it to face the correct direction as the rear of the ship approaches the player.

i'll keep working at it

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

Operator cannot be used with left hand side of type.... 1 Answer

[Solved] Formation System - Rotate Vector3 Positions around a point? 2 Answers

Random direction with Mouse Click... 2 Answers

Vector3.SignedAngle wrong direction when crossing the 0 point 1 Answer

How to stop transform movement at a certain point? 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