Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 a_flux · Jan 23, 2014 at 01:11 PM · rigidbodycollidermouseclickmovements

Stopping moving object going through a wall

Hi All

Sorry, but this is my 2nd question regarding this problem that I am facing. I have a moving cube that responds to mouse clicks, whenever I click the mouse the cube moves to that point. I have another object (a wall) when I click on the wall my cube moves into the wall (goes through it or part of the cube acually in it) and doesn't stop at the wall surface. Please I've seen some videos regarding applying rigidbdies and colliders to achieve this but with me no luck so far. Any one can state the steps of doing this but please with no code addition as I've seen it done without adding any scripts or coding which I prefer.

Thanks in advance and looking to your answers.

Comment
Add comment · Show 9
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 Bmarlyman21 · Jan 23, 2014 at 01:17 PM 0
Share

make sure the collider on the wall is stretched out across the whole mesh.

avatar image Exalia · Jan 23, 2014 at 01:58 PM 1
Share

A few things to check before I can really help

  1. Post some code for us :) (The code you use to move your cube)

  2. Check your cube has a rigid body

  3. Check both the cube and the wall have a collider

  4. Check the colliders are not triggers

  5. How are you moving the cube? changing transform.position will not take collision into account until after the transformation.

  6. Try using rigidbody.AddForce or rigidbody.SetVelocity to move your cube, this will take collision into account. (If you use this makesure you do it in FixedUpdate() and not Update()

Get back to me and I'll try and offer some more help

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

Yes, I did make sure of that and still not stopping it from going through the wall.

avatar image a_flux · Jan 23, 2014 at 07:34 PM 1
Share

Thanks Exalia for offering your help, please have a lock below about my code that I am using to move my object:


ar 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 23, 2014 at 07:37 PM 0
Share

I've tried to replace transform.position with rigidbody.AddForce but I really messed the whole code up, I am very poor in coding or editing codes. SO if you could help with that, then that'll be great. Other steps such as adding colliders, or rigidbodies then I can manage that.

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by IggyZuk · Nov 19, 2016 at 03:05 PM

All you have to do is set the Collision Detection property in the Rigidbody component to "Continuous".

alt text


ss-2016-11-19-at-012709.png (8.4 kB)
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 James_Dvorak · Jan 31, 2018 at 04:08 AM 0
Share

This fixed my problem perfectly! Thanks!

avatar image
0

Answer by TheReclaimer117 · Jan 23, 2014 at 08:09 PM

add a rigidbody and make sure your wall has a solid mesh. You can change the mesh and physics settings from your object too and set the material, so it can bounce, stop, whatever. Make sure that physics collider is on

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 rasoulcarrera · Jan 23, 2014 at 09:21 PM

And remember give a box or mesh collider to your wall And also to your box

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 a_flux · Jan 26, 2014 at 06:10 AM 0
Share

Thank you guys for your tips, I've tried adding rigidbody to my wall and cube, box collider to my wall and cube too, I added a constraints for the wall so it doesn't fly off the terrain after the cube touches it, but my cube is now flying off the terrain after touching my wall, I tried to add constrains to my cube but that made go back and behave just like before, which means if I add constrains on the cube it goes through the wall again, now how can I stop my cube from flying off the whole scene. Do I need to add a bit of coding to it just as Exalia said in earlier post. By the way I have my cube as a player inside a game object, so if I add box colliders or rigidbodies to the gameobject or the player inside it??? Waiting your advice.

avatar image
0

Answer by varmac · Nov 19, 2016 at 11:15 AM

using UnityEngine; using System.Collections; using UnityEngine.Audio;

public class Blast : MonoBehaviour {

 public GameObject explosion;
 public GameObject playerExplosion;

// private object gameController; private object playerUnitScript;

 void OnTriggerEnter(Collider other)
 {
      if (other.tag == "Boundary")
      {
         return;
         //Instantiate(explosion, transform.position, transform.rotation);
     }
     Instantiate(explosion, transform.position, transform.rotation);
     if (other.tag == "Player")
     {
         Instantiate(playerExplosion, other.transform.position, other.transform.rotation);

         // gameController.GameOVer();
     }
     Destroy(other.gameObject);
     Destroy(gameObject);
 }


   

} its not working can any pls solve this

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

27 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

Related Questions

Player keeps falling halfway through terrain floor 3 Answers

How to setup character Collisions? 2 Answers

rigidbody It's going up and bounces when is moving 1 Answer

Problem with Unity Terrain and GameObject 0 Answers

When jump my character controller model it goes out of collider 0 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