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 /
avatar image
0
Question by Eth14 · Apr 02, 2017 at 07:03 PM · physicsfirst person

When the player clicks on an object, how do I make that object be forced away from them in the opposite direction?

Hi, as the title says, I want to force a game object away from the player in the way they are facing. I am using an FPS controller if this matters. This is probably a simple thing, but I'm new to Unity, and I can't figure out how to do it. I don't mind whether it is JavaScript or C#.

Another little side-question, is there a way to limit the distance the player can click on the object from?

Thanks in advance! :)

Comment
Add comment · Show 3
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 RobAnthem · Apr 02, 2017 at 09:07 PM 1
Share

If you have a reference to the player object, you can move the other object with a myriad of ways. If it has a rigidbody, you can add force, if not, you can move it other ways. Here is a quick example.

 private Rigidbody body;
 public float force;
 public float maxDistance;
 void OnEnable()
 {
     if (body == null)
     {
         body = GetComponent<Rigidbody>();
     }
 }
 void On$$anonymous$$ouseDown()
 {
     if (Vector3.Distance(transform.position, Game$$anonymous$$anager.PlayerObject.transform.position) < maxDistance)
     {
         body.AddForce(Game$$anonymous$$anager.PlayerObject.transform.forward * force);
     }
 }

avatar image dCalle RobAnthem · Apr 02, 2017 at 09:54 PM 0
Share

oh, ok didn't know On$$anonymous$$ouseDown exists.... looks good ;-)

avatar image Eth14 RobAnthem · Apr 03, 2017 at 04:42 PM 0
Share

Hi! I couldn't get this to work. I'm getting the error 'The name 'Game$$anonymous$$anager' does not exist in the current context' Here is the code: using UnityEngine; using System.Collections;

 public class $$anonymous$$oveObject : $$anonymous$$onoBehaviour {
 
 private Rigidbody body;
 public float force;
 public float maxDistance;
 void OnEnable()
 {
     if (body == null)
     {
         body = GetComponent<Rigidbody>();
     }
 }
 void On$$anonymous$$ouseDown()
 {
     if (Vector3.Distance(transform.position, Game$$anonymous$$anager.PlayerObject.transform.position) < maxDistance)
     {
         body.AddForce(Game$$anonymous$$anager.PlayerObject.transform.forward * force);
     }
 }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Apr 02, 2017 at 08:58 PM

you might want to check out this link:

https://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html

if you are new here, there is a simple math pricipal to get direction between two points

you simply subtract the two Vector3 positions and the resulting vector3 represents direction!

you would simply use the result for the velocity or position of the object to be moved. (pry want to normalize it and multiply by a speed variable first)

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

Answer by dCalle · Apr 02, 2017 at 09:22 PM

  1. the thing you want to push needs a collider and a rigidbody attached.

  2. you want to read and use the Script that is used here

  3. Understand every character in the Script

  4. Adapt the Script to this:

     RaycastHit hit;
     Ray ray = camera.ScreenPointToRay(Input.mousePosition);
     if (Physics.Raycast(ray, out hit)) {
        Rigidbody rigid = hit.rigidbody;
        rigid.AddForce(Camera.main.transform.forward * XXX);
     }
    
  5. Remember the object will always move in the Direction the Camera is looking

  6. if you want to limit the Player Reach, check this out and find maxDistance ;-)

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 Eth14 · Apr 03, 2017 at 04:11 PM 0
Share

Hi! It unfortunately doesn't do anything. Here is the code, it doesn't give any errors, it just doesn't do anything. using UnityEngine; using System.Collections;

 public class PushObject : $$anonymous$$onoBehaviour {
     public Camera camera;
 
     void Start(){
         RaycastHit hit;
         Ray ray = camera.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit)) {
             Transform objectHit = hit.transform;
 
             Rigidbody rigid = hit.rigidbody;
             rigid.AddForce(Camera.main.transform.forward * 100);
         }
         }
     }
 

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

First person skydiver help 1 Answer

2D 360 degress platformer example needed 0 Answers

Some Ideas please? 1 Answer

Simple Crate pushing.. 2 Answers

Navmesh Agent velocity VS physics 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