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 wicked208 · Dec 17, 2010 at 08:09 PM · positionairelative

need rotate when change position relative to char

hi there, i am making a 2d game and i have a basic ai, the enemy follow me and atack me when in short range, i need my enemy rotate when he es in x local negative relative to char, i try this code. if (transform.InverseTransformPoint(engineTransform.position).x > 0.0) { // right } else { // left }

but dosnt work very well when the enemy its to close to player.

this is my full code for ai. var target : Transform; var movespeed :int; var rotationSpeed :int; var dist : float; var spoted :int = 5; var atackRange :float = 1.5;

function Start (){ target = GameObject.FindWithTag("Player").transform;

}

function Update () { //dibuja una linea entre el objetivo y el enemigo, tambien se puede poner target.position en vez de target.transform.position, ya que el target es de tipo transform Debug.DrawLine(target.transform.position, transform.position, Color.blue); dist = Vector3.Distance( target.transform.position, transform.position);//target.position.magnitude - transform.position.magnitude ;

if (transform.InverseTransformPoint(target.position).x < 0) { // left Debug.Log (" estoy a la izquierda"); // i am in the left position

}

if (dist < spoted && dist > atackRange ){ // mueve al target //movimiento local , global seria Vector3.forward animation.Play ("run"); Debug.Log ("estoy a la derecha"); // i am in right position //var movimiento = transform.Translate(transform.forward movespeed Time.deltaTime, Space.World); var movimiento = target.transform.position - transform.position; movimiento.Normalize(); var mover = movimiento * movespeed;

 transform.position = transform.position + ( mover * Time.deltaTime);

} else if (dist < atackRange){

 animation.Play ("attack");

} else { animation.Play ("idle");

}

} thanks for help.

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 wicked208 · Dec 18, 2010 at 11:37 AM

if(target.position.x > transform.position.x) Debug.Log ("i am in left"); else if(target.position.x < transform.position.x) Debug.Log ("i am in right"); else Debug.Log ("same place, i am dead.");

this is the code, someone help me in other forum.

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

No one has followed this question yet.

Related Questions

GameObject's local position without parent 1 Answer

Find Distance on Single, Relative Axis? 1 Answer

Rotate object towards a target on only y axis? 5 Answers

Is it possible to move relative position of GUI? 0 Answers

blocks being placed in an odd way 0 Answers


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