- Home /
Help, Sphere goes through the Wall!
Okay, in this project I have a wall, a sphere, a box and a base. So the wall is in the middle of the base and the box is placed behind it. What I want to happen is for the sphere to go around the wall so that it could stand next to the base. The problem is, it won't go around the wall, it goes through it, and I already put the colliders in them.
This is the follow code which I got from here. var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake() { myTransform = transform; //cache transform data for easy access/preformance }
function Start() { target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () { //rotate to look at the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Answer by jjobby · Oct 12, 2010 at 01:14 PM
It goes through the wall because you change its transform position directly. You should add sphere collider and rigidbody to it and then move it by using Addforce instead. From the code, if the sphere is just physics object in your scene (no AI, no control) then these codes shouldn't be needed.
var target : Transform;
target = GameObject.FindWithTag("Player").transform;
Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
But in case that the sphere is moving too fast and still goes through the wall then you should try to change collision detection option in rigidbody.
http://unity3d.com/support/documentation/Components/class-Rigidbody.html
Answer by Crafty Firefox · Oct 12, 2010 at 01:19 PM
This might be because you are telling your sphere to do so :).
I suppose you want the sphere to go around the wall using a physics simulation. For that, you need to:
- add a rigidbody to the sphere
- add a spherecollider to the sphere
- add a BoxCollider to the wall you wish to go around
- use FixedUpdate (as for all physics manipulation) instead of Update
- use AddForce (clean) or set the velocity of the sphere's rigidbody for movement
you will need to play around with the values for mass and force, until you achieve the desired effect.
Alternatively, you could use a pathfinding-approach (forgetting all of the above about physics) using e.g. A* and define your nodes for the A* in 3D-Space (or pseudo-3D, locking down y coordinate), doing a more or less (depending on implementation of course) smooth transition between the path you found. You will of course need to implement that yourself, there is no "out of the box" logic in Unity for that.
Of course, there is many more ways to "move a sphere around a wall to an object behind it" - but this completely depends on what you want to do, which you don't state so far ;).
Without any further detail, that's the pointers I can provide, hope they help.
Your answer
Follow this Question
Related Questions
Object collision 2 Answers
Player's Collider Stuck Help 1 Answer
My script says object is not colliding with the tag! But it is! 1 Answer