Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by noahbald · Mar 13, 2016 at 08:27 AM · transformtransform.position

How to set an object's position a certain distance away from a target at the same angle?

I'm trying to have an object stay 3 units away as the characters moves closer towards it so it's like it gets pushed.

 if (Vector2.Distance(transform.position, player.transform.position) < 3)
                 {
                     transform.position = ((player.transform.position - transform.position).normalized * 3) + player.transform.position;
                 }

This is what I've written but it doesn't work. As you walk into the radius the object will go to the other side rather than being 'pushed'.

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

Answer by ZefanS · Mar 14, 2016 at 06:48 AM

The key to making sure the object is pushed away from the player is to calculate the vector between the object and the player. With that you know which direction to move the object.

alt text Given two points A and B, the vector AB that goes from A to B is calculated as: AB = (B - A)

or written out:

AB = (u2 - u1, v2 - v1)

Once we know this vector there are a few ways we can proceed. I've thrown together a couple different scripts here. The first uses Transform.Translate() and the second uses Rigidbody.AddForce(). They are written for 3D but it should be trivial to convert them to handle 2D if necessary.

Script 1:

 #pragma strict
 
 public var player : GameObject;
 public var distance : float;
 public var pushStrength : float;
 
 function Update()
 {
     //Calculate the distance between the object and the player
     var dist = Vector3.Distance(transform.position, player.transform.position);
 
     if (dist < distance)
     {
         //Calculate the vector between the object and the player
         var dir : Vector3 = transform.position - player.transform.position;
         //Cancel out the vertical difference
         dir.y = 0;
         //Translate the object in the direction of the vector
         transform.Translate(dir.normalized * pushStrength);
     }
 }
 

Script 2:

 #pragma strict
 
 @script RequireComponent(Rigidbody);
 
 public var player : GameObject;
 public var distance : float;
 public var pushStrength : float;
 
 private var rb : Rigidbody;
 
 function Start()
 {
     rb = GetComponent.<Rigidbody>();
 }
 
 function Update()
 {
     //Calculate the distance between the object and the player
     var dist = Vector3.Distance(transform.position, player.transform.position);
 
     if (dist < distance)
     {
         //Calculate the vector between the object and the player
         var dir : Vector3 = transform.position - player.transform.position;
         //Cancel out the vertical difference
         dir.y = 0;
         //Translate the object in the direction of the vector
         rb.AddForce(dir.normalized * pushStrength);
     }
 }

You should attach one of these scripts to the object to be pushed. Remember to edit the values in the inspector.

Hope this helps!


vectorab.png (8.6 kB)
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

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

Related Questions

transform.position not applying to my camera. 1 Answer

What's the difference between transform.position + offset and transform.TransformPoint(offset) 1 Answer

Replicate local movement between objects 1 Answer

How to get the position of a new Instantiated prefab 0 Answers

How do I make two objects share two of the same transform values? 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