• 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 Royall · May 20, 2014 at 11:08 PM · positionobjectnear

Get position near gameobject

Hello,

I am trying to come up with a script that gets the position of a target minus a certain amount of units (lets say 2)...

I came up with:

 var dir = hit.transform.position - transform.position;
 var enemypoint : Vector3 = hit.transform.position + dir.normalized * 2;

It gives the position minus 2 units but it's always at a fixed place from the enemy (let's say always on the left)

I need the position to be 2 units away from the enemy facing the player if you know what I mean... It's hard to explain. This way I can make the player walk to the enemy position minus 2 units so the player is next to the enemy instead of IN the enemy.

Hope someone can help.

Comment
Add comment · Show 2
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 robertbu · May 20, 2014 at 11:11 PM 1
Share

Are these lines on the player? If so, they should do what you are asking. Is the pivot point of the enemy and the player at the center of each object? Could there be camera perspective issues. If you do:

 Debug.DrawRay(hit.transform.position, dir.normalized * 5);

...where does the ray hit?

avatar image Kiwasi · May 21, 2014 at 12:40 AM 0
Share

As I read it this code should give you a point exactly 2 units on the far side of your enemy.

To bring it to the near side of your enemy you simply need to change the + to a - on line 2.

However this does not explain the observed behaviour. Let me know how the ray goes.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Royall · May 21, 2014 at 11:35 AM

In my original code I used: GetComponent(GameController).playerObject.position; Instead of transform.position because the code gets executed in my camera. (Posted like this to give a better understanding). I changed it to target.position and it all works now...

@robertbu Thanks for the debug tip! @BoredMormon Thanks, it works as intended that way!

My final code looks like:

 var dir = hit.transform.position - target.position;
 var enemypoint : Vector3 = hit.transform.position - dir.normalized * 2;
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Why Does My Object Change Position During Update? 0 Answers

GameObject.find and positioning 1 Answer

C#: Changing current position of an object in y. 1 Answer

Remove object based on its position alone 1 Answer

Collide edge of object with the edge of the screen 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges