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
1
Question by Rotarus · Jun 24, 2012 at 03:15 PM · pushpushing

Player push Object on mouse click

Hello! I have a small problem with pushing Objects from my FPS. I want my player to have three possibilities: drag objects (already enable in Unity), attract objects and push them (I mean moving objects from camera on mouse click).

For attracting objects I used Vector3.Lerp and used player position as the end point. That works fine, so I decided just to put "-" to make a pushing. But it didn't work - the object is moving in a strange direction, but not from my player.

Here is the CODE:

 var target : GameObject;
 target = GameObject.Find ("Player");
 var PushOn = 0;
 var smooth = 1.0;
 
 function OnMouseOver() {
 if (PushOn == 1){
 gameObject.tag = "PushFromPlayer";
 }
 }
 
 function Update () {
 if (Input.GetMouseButtonDown(1) && PushOn == 0){
 PushOn = 1;
 }
 if (Input.GetMouseButtonUp(1) && PushOn == 1){
 PushOn = 0;
 }
 
 if (gameObject.tag == "PushFromPlayer"){
 transform.position = Vector3.Lerp(transform.position,-target.transform.position,Time.deltaTime*smooth);
 }
 }

Maybe I am wrong using this method for pushing objects and there is a simpler one? Thank you very much!

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 Dev Solo · Jun 24, 2012 at 06:54 PM 0
Share

You want this to react with physics right?

avatar image Rotarus · Jun 24, 2012 at 10:36 PM 0
Share

Yes. With Rigidbodies, for which this script is attached.

1 Reply

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

Answer by Tim-Michels · Jun 25, 2012 at 07:56 AM

First of all, when you want your objects to react with physics, you shouldn't set the position of the objects manually. You should use AddForce with the correct direction vector.

The direction vector is pretty easy to calculate:

Vector3 dir = transform.position - target.transform.position;

You should normalize this vector to use it further on:

dir.Normalize();

With this new normalized vector, you have the direction vector between your target and the player. You can now use it to add force in both directions, giving you the possibility to push or pull the object.

This will push the object away from the player:

transform.rigidbody.AddForce(dir forceIntensity Time.deltaTime);

This will pull the object towards the player: (notice it's just the negative direction vector)

transform.rigidbody.AddForce( - dir forceIntensity Time.deltaTime);

You should experiment more with this stuff, it's pretty easy once you get the hang of it ;)

Cheers

Comment
Add comment · Show 5 · 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 Rotarus · Jun 25, 2012 at 02:12 PM 0
Share

Tim $$anonymous$$ichels, thank you very much for a such detailed answer! But I have an error while inserting it into my code. $$anonymous$$y code is written in Javascript. I insert "Vector3 dir = transform.position - target.transform.position;" and "dir.Normalize();" in function Update (), and it gives an error: ';' expected. Insert a semicolon at the end.

avatar image Tim-Michels · Jun 25, 2012 at 02:18 PM 0
Share

replace:

Vector3 dir = transform.position - target.transform.position;

with:

var dir : Vector3 = transform.position - target.transform.position;

avatar image Rotarus · Jun 25, 2012 at 02:59 PM 0
Share

Thank you, I changed and there's no errors now! but nothing happens with my rigidbody... I created a variable for forceIntensity and tried to set it to 1.0, 100, 1000... But nothing...

avatar image Rotarus · Jun 25, 2012 at 03:26 PM 0
Share

WOW! I removed "* Time.deltaTime" and everything works fine now!! I made it, cause I thought, that there might be some problems with time in my whole project... I also changed my code for mousebutton this way:

if (Input.Get$$anonymous$$ouseButton(1)){ transform.rigidbody.AddForce(dir * forceIntensity); }

Tim $$anonymous$$ichels, THAN$$anonymous$$ YOU VERY $$anonymous$$UCH!!!

avatar image Rotarus · Jun 25, 2012 at 03:47 PM 0
Share

WOR$$anonymous$$S GREAT!!!

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

Pushing an object with parented origin HELP 0 Answers

Cannot push Unity project to Github repo 1 Answer

Pushable blocks - dynamic puzzle 0 Answers

Rigidbody triggers are pushing a CharacterController.. 3 Answers

how to end your game 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