- Home /
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.
make sure the collider on the wall is stretched out across the whole mesh.
A few things to check before I can really help
Post some code for us :) (The code you use to move your cube)
Check your cube has a rigid body
Check both the cube and the wall have a collider
Check the colliders are not triggers
How are you moving the cube? changing transform.position will not take collision into account until after the transformation.
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
Yes, I did make sure of that and still not stopping it from going through the wall.
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;
}
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.
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".
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
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
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.
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