- Home /
Duplicate Questions : http://answers.unity3d.com/questions/623453/stopping-moving-object-going-through-a-wall.html : http://answers.unity3d.com/questions/612537/how-to-stop-my-object-from-going-through-another-o.html
Object going through another....How to solve it?
Dear All,
This is my 3rd question regarding this issue, I have an object (probe) that responds to mouse clicks, and I have a wall (cube) standing on the terrain (floor). My problem is that when I click on the wall the probe moves towards that point and some of it's tip goes into the wall which i don't want to see, I want my probe to reach the surface of the wall and stops. I did apply rigid body and box and mesh colliders to both, the probe and the wall and it sort of stopped the probe from going through the wall but now when the probe reaches and touches the wall it starts shaking and dragging along the wall in a funny way, I want my probe to touch the wall and stay put with out shaking or dragging movement even though it moves and shakes very slowly. How to overcome this problem. Any help will appreciated. Cheers
Sorry mate, but I am totally new to unity, do u mean applying physics to my code? if that's what u mean then No, otherwise please clarify. To cut down the chase I am attaching my code for you to check it, I am crap in coding too. Here it is:
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;
}
DO NOT POST DUPLICATE QUESTIONS :
http://answers.unity3d.com/questions/612537/how-to-stop-my-object-from-going-through-another-o.html
http://answers.unity3d.com/questions/623453/stopping-moving-object-going-through-a-wall.html
You have been advised by many people :
1/ submit your code when asking a question
2/ that modifying a transform is like teleporting a gameObject to a specified position, SO IT WILL NOT BE AFFECTED BY COLLISIONS. USE FORCE AND RIGIDBODY.
Thanks for closing the question, but did you check that there is no one is replying back or trying to help me out with the previous one and that's why i had to ask a new one...
Follow this Question
Related Questions
Transforming position for different colliders for repeating background? 0 Answers
Colide objects with runtime gizmo movement 0 Answers
Object collider not operating properly 2 Answers
Stopping my player from moving when hitting a wall. 5 Answers
Sinking Sand 2 Answers