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 /
This question was closed Jan 29, 2014 at 05:05 AM by AlucardJay for the following reason:

Duplicate Question : http://answers.unity3d.com/questions/623453/stopping-moving-object-going-through-a-wall.html

avatar image
0
Question by a_flux · Jan 09, 2014 at 11:57 AM · meshcolliderboxcollidermovmentforce stop

How to stop my object from going through another object

Hi

I have an object that moves towards another object, a probe that moves towards a cube, I used a script for movement my problem is that my probe doesn't stop when it reaches the cube but it keep going through it and I want it to stop. I've tried box collider, mesh collider rigid-bodies on both objects with no luck so far, my probe is inside a gameObject.

Any tips or guidance on how to stop my object going through the cube is really appreciated.

Comment
Add comment · Show 5
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 tanoshimi · Jan 09, 2014 at 12:00 PM 0
Share

We need to see the script you use for movement.

avatar image a_flux · Jan 09, 2014 at 12:08 PM 0
Share

Here is the script: var smooth : float = 1.0; // Deter$$anonymous$$es how quickly object moves towards position private var targetPosition : Vector3; var speed : float = 60.0;

function Start() { targetPosition = transform.position; }

function Update () {

 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)) {
   

     var playerPlane = new Plane(Vector3.up, transform.position);
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hitdist = 0.0;
    
     if (playerPlane.Raycast (ray, hitdist)) {
         var targetPoint = ray.GetPoint(hitdist);
         targetPosition = ray.GetPoint(hitdist);
         var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
         transform.rotation = targetRotation;
     }
 }

 var dir : Vector3 = targetPosition - transform.position;
 var dist : float = dir.magnitude;
 var move : float = speed * Time.deltaTime;
 if(dist > move){
     transform.position += dir.normalized * move;
 } 
 else {
     transform.position = targetPosition;
 }
 
 transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime; 

}

avatar image a_flux · Jan 09, 2014 at 12:10 PM 0
Share

I want my moving object to stop when it touches the cube. In other words I want my cube to be solid nothing can go through it.

avatar image Invertex · Jan 09, 2014 at 12:12 PM 0
Share

Either use rigidbody.velocity and rigidbody.AddForce to move your character with a rigibody and colliders on it, or use Raycasts to detect if a wall or something is in front of your character, and if true, restrict X axis movement to only move away.

avatar image a_flux · Jan 09, 2014 at 12:57 PM 0
Share

How can I use rigidbody.Addforce? any hints?

2 Replies

  • Sort: 
avatar image
2

Answer by ankush_Kushwaha · Jan 16, 2014 at 01:04 PM

I will suggest you to use add force on rigid body but anyway, If you want to use transform.position that is

if(dist > move){ transform.position += dir.normalized * move; }

then you can use IstriggerEnter() function to detect your collision and when collision occurs you just need to stop your code

if(dist > move){ transform.position += dir.normalized * move; } to execute.

you can put a condition in your if()

if(dist > move && collisionOccured== false){ transform.position += dir.normalized * move; }

and In onTriggerEnter() write OnTriggerEnter(){ collisionOccured = true; } . For triggerEnter your both bodies must have collider on them and atleast one of them should be a rigid body.

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 Professor Snake · Jan 09, 2014 at 11:59 AM

You seem to be directly modifying its transform.position, which will not take any physics into consideration. (transform.position += //your code)

Try adding a rigidbody to the object that you are trying to move and use Rigidbody.AddForce.

Comment
Add comment · Show 12 · 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 a_flux · Jan 09, 2014 at 12:19 PM 0
Share

How do I add rigidbody.AddForce and is it going to be attached to the moving object or the cube.

avatar image a_flux · Jan 09, 2014 at 12:44 PM 0
Share

I am really new to this so help would be really appreciated.

avatar image Professor Snake · Jan 09, 2014 at 01:03 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

It, as the name implies, adds a force to a rigidbody. It should replace the transform.position+= //code line, since it's going to be used as a method of moving your object while taking physics into consideration.

avatar image a_flux · Jan 09, 2014 at 02:03 PM 0
Share

Sorry would you $$anonymous$$d adding it to the script that i posted as i am afraid I could ruin it as I am really bad in coding. Cheers

avatar image a_flux · Jan 16, 2014 at 12:53 PM 0
Share

In every place that it says transport.position I replaced it with rigidbody.AddForce. Still not working.

Show more comments

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Note Pad not flipping back 1 Answer

RigidBody/meshcollider falling through boxcollider 2 Answers

Colliders not inheriting the Transforms of changed animation states(Run to Crawl) -Unity3D, 0 Answers

Mesh collider not working as expected 2 Answers

why mesh collider penetrate box collider? 3 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