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 EmpI · Jun 30, 2014 at 09:34 PM · homing

Homing Enemies issue (2D)

So i'm trying to make enemies in the form of projectiles that chase the player. The script I have does what I want it to do, the enemies DO follow the player, but they move in ways I don't want them to. More specifically, the game is supposed to be 2D on the X-Y Plane yet to chase the player the projectiles often loop-de-loop into the z-axis. I don't want that, I want them bound to the X-Y plane.

Here is the code (Java)

 var target : Transform; 
 var moveSpeed = 4;
 var rotationSpeed = 1;
 var myTransform : Transform;
 
 function Awake()
 {
 myTransform = transform;
 }
 
 function Start()
 {
 target = GameObject.FindWithTag("Player").transform;
 myTransform.rotation = Quaternion.LookRotation(target.position - myTransform.position);
 }
 
 function FixedUpdate()
 {
 var lookDir = target.position - myTransform.position;
 //lookDir.z = 0;
 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }

I know myTransform.forward sends the projectiles into the z-axis, but changing it to right or up causes the projectiles to behave incredibly oddly. The same thing happens when I try to implement the lookDir.z code you see commented out.

Also one last thing: Is there a way to change the movement of the projectile in such a way that the Physics2D material I have attached to it bounces off the rigidbody2D of the player?

So any help? What seems to be the problem?

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 Kiwasi · Jul 02, 2014 at 04:32 AM

Add the line

 transfom.position = new Vector3(transform.x, transform.y, 0);

This will constrain your object to stay on the plane.

As an aside, why are you caching transform in myTransform?

Comment
Add comment · Show 7 · 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 EmpI · Jul 02, 2014 at 05:23 PM 0
Share

No reason I suppose, other than I saw it on a tutorial.

So I added the line, and I haven't noticed any change whatsoever. I added it directly below the-

 transform.position += transform.forward * moveSpeed * Time.deltaTime;

-line. Should I have added it somewhere else?

Furthermore, I get this in the console. NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) Ho$$anonymous$$gJava.FixedUpdate () (at Assets/Ho$$anonymous$$gJava.js:23)

This only happens while the new code is in the game. When I comment it out, nothing appears in the console.

It looks as though the game is searching for a reference, but I have a target object designated, so I don't know what's going on.

Help?

avatar image Kiwasi · Jul 02, 2014 at 07:33 PM 0
Share

This is the correct place to add it.

What scripting language are you using? In JavaScript I believe you need to drop the new.

avatar image EmpI · Jul 02, 2014 at 08:12 PM 0
Share

I am using Javascript. Removing the "new" from your line of code doesn't change anything again, unfortunately.

Here's what the whole code looks like right now var target : Transform; var moveSpeed = 4; var rotationSpeed = 1;

 function Start()
 {
 target = GameObject.FindWithTag("Player").transform;
 transform.rotation = Quaternion.LookRotation(target.position - transform.position);
 }
 
 function FixedUpdate()
 {
 var lookDir = target.position - transform.position;
 //lookDir.z = 0;
 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
 transform.position += transform.forward * moveSpeed * Time.deltaTime;
 transform.position = Vector3(target.transform.y, target.transform.x, 0);
 }
 

Again, no change. The ball homes in on my player, but I want it bound to the X-Y plane.

avatar image Kiwasi · Jul 02, 2014 at 10:45 PM 0
Share

You should be using transform.y, not target.transform.y. You also have the x and y reversed. Try replace line 13 with:

 transfom.position = Vector3(transform.x, transform.y, 0);

avatar image EmpI · Jul 03, 2014 at 12:00 AM 0
Share

Doesn't do anything, again. All the changes on the above code are a result of me experimenting with it to try and get a different result other than "no change."

Thanks again for your help, by the way.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

missile homing script issues 1 Answer

Homing missle with raycast 1 Answer

Homing Missile Target From Parent - Help 1 Answer

Homing mechanic, help! 1 Answer

Quaternion based slerp to make homing projectile doesn't turn 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