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 jimjames · Mar 01, 2015 at 08:13 PM · transformvector3lookat

Change a Transform to Aim to the center of a Collider. Not (0,0,0)

Hi: I am having trouble with figuring out how to change the set transform of a public transform to move towards the center of an object and not it's (0,0,0). I would like not to add another gameobject.

Code Ex

 public Transform target;
 public GameObject missleSmall;
 
 void OnFire()
 {
     missleSmall.GetComponent<missleSmall>().myTarget = target;
 }

"missleSmall" has a script that looksAt "myTarget" and moves forward.

This works fine in script. The problem is that the "missleSmall" goes towards the (0,0,0) of "myTarget" the feet. What I would like to do is have the "missleSmall" go towards the center of the target, the body.

This is what I tried to do:

 TR.currentTarget.localPosition = new Vector3(0,1,0);

 missleSmall.GetComponent<missleSmall>().myTarget = target;


As the target size is (2,2,2) the center of it would be 1 on the Y axis. But the "target" gets moved to the world coordinates. Just wondering if there is a way to do this without adding more gameobjects to the enemy?

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
1
Best Answer

Answer by neonblitzer · Mar 01, 2015 at 08:51 PM

Short of adding a "target position" child GameObject to the enemy (which sounds the easiest solution from my POV), you should store a Vector3 offset from the enemy's base position to the desired target position, for example in the enemy's MonoBehaviour as a public field.

 public class Enemy : MonoBehaviour {
     public Vector3 targetOffset; // set this to (0, 1, 0) in the editor
     ...
 }

 public class missleSmall : MonoBehaviour {
     public Enemy myTarget;

     public void Update() {
         transform.LookAt(myTarget.transform.position + myTarget.targetOffset);
         ...
     }
 }

 public class MyPlayer : MonoBehaviour {
     void OnFire() {
         missleSmall.GetComponent<missleSmall>().myTarget = target.GetComponent<Enemy>();
     }
 }

Alternatively, you could create a separate reusable MonoBehaviour just to handle the offset, something like:

 public class OffsetContainer : MonoBehaviour {
     public Vector3 offset;
 }

...add that to your enemy, and retrieve that component in your code.

PS. Class names usually start with an uppercase letter, so missleSmall -> MissleSmall. :)

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 jimjames · Mar 01, 2015 at 09:10 PM 0
Share

The project started out in JS which is the reason some of my classes start in lower case. Still converting everything to C#. I decided to go with a target position empty gameobject in the spot where I would like the projectiles to move towards, due to enemies being different sizes. Thank You

avatar image
0

Answer by sparkzbarca · Mar 01, 2015 at 08:35 PM

so there are two ways to solve this.

Either you open the model in a modelling program and do a origin to center mass etc to move the origin of the model to it's chest

or

you add a variable called target offset to the target that tells objects targeting it it's center relative to it's origin.

the thing is right now your targeting a game object not a position according to your code. presumably at some point in the missilesmall script you actually use target.position. your going to want to use target.position + target offset to aim correctly. however creating an empty game object isn't an issue, it's literally a point in space. The only time it's going to be an issue is if you use find commands to search all the game objects. Get 5000 empty objects to search and you'll have a problem. i understand your desire to have a "clean" scene.

I suggest doing the first one, an objects position should be it's center not it's feet. This can end up skewing with scaling, rotation, position (as you've just seen) unity has boxes and such origin'd to there center, keep with the standard.

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 sparkzbarca · Mar 01, 2015 at 08:42 PM 0
Share

moving the models origin is super easy by the way.

download blender, a free modelling program. open the model press T to bring up the transform menu. origin to center of mass.

Control + A to apply

save the model

viola! it'll reimport to unity with the proper position now in the center of the model.

avatar image jimjames · Mar 01, 2015 at 08:49 PM 0
Share

So what I am understanding from your post is to in Ex.2 is to make a empty gameobject in the center of the enemy for the missle to target? The Ex.1 is not an option due to other complications.

What do you mean by offset variable? Is that a vector3 or a object?

I would like a offset vector3 variable to add an amount to make the missle to aim alittle higher.

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

21 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 avatar image

Related Questions

What is the algorithm to assign to transform.right? 1 Answer

How to use x axis for transform.LookAt 3 Answers

Rigidbody to follow player around 1 Answer

transform.LookAt boundaries 1 Answer

How to prevent Z axis rotation ? 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