- Home /
moving an object along a surface, then rotating it around end of surface
Im trying to move a circular gameobject along a surface and then rotate it around the corner / end point of that surface.
I think it can be soloved using trigonometry, but not 100 percent sure. I can move the object along the surface using this calculation...offset = tan(A) x radius of circle. this gives me a distance I can offset the circle by, but having trouble with the rotation.
please see attached image for a better understanding of what i am trying to achieve. 
It would be really helful if anyone can give me any advice on how to to resolve this problem. thanks in advance.
javascript code for stage1 of the diagram....
var player : GameObject;
var d1 : float = 1;
var offset : float = 0;
function Update () {
var angleA : float = Vector3.Angle(player.transform.position - transform.position, transform.forward);
var angleB : float = 90 - angleA;
var angleC : float = angleA + angleB;
var angleCRadians : float = angleC * (Mathf.PI/180);
var angleCTan = Mathf.Tan(angleCRadians);
offset = angleCTan x d1;
transform.position += transform.TransformDirection(Vector3( -offset, 0, -d1));
}
Ok, so it when the center of your circle reaches the end of the wall, it wants to rotate around that corner. the distance it will move is 1/4 of the circumference I think, so here is what I would do. Use the Rotate around function here http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html just rotate around that corner for however long you want, just remember its 1/4 of the circumference.
thanks for the reply @rppe, the problem i have is rotating the circle so that the entire circle is just behind the corner when the player is looking at the corner.
Im trying to build an enemy cover system so that an enemy can go to a coverzone (the circle) and be behind the corner. He can then lean out from that cover and shoot at the player.
thanks.
Answer by @rppe · Sep 02, 2012 at 08:10 PM
So your formula only offsets him in one direction? Just use that until the difference between his X position and the edge of the wall is his radius, and then use the same formula to calculate his z offset on the Right side of the wall. that should give you the effect you want,
David
the player wouldnt notice anyway, hes hiding from the player :p
thanks $$anonymous$$.
you are right. the formula above only effects the coverzone in one direction. I do have more code that makes the coverzone (circle) go down the left hand side of the wall if the player is above the wall, but only posted half the code so i could put across a clearer question (if that makes sense ;)
my idear is I will have a coverzone on all the corners of buildings etc in my game and they will move left or right of that coner depending on where the player is. they will turn off when the next coverzone / corner is in view. I can then have an array of all active coverzones that my enemy transforms can go to / hide behind.
you are also right with your solution, I did try that at some point. But when I have a corner that isnt a 90 degree corner it starts to not work so well (for example a corner or tip of a starfish type shape). I thought I could rotate it round the corner and it may solve the issue. another example would be if the wall just has a slight kink in it, then the circle ends up inside the wall when changing the offset direction. Hope you get what i mean, i dont think i have explained that very well.
$$anonymous$$aybe its starting to get to complex. I might have to think about a more simple meathod :)
or maybe someone has a better solution for an enemycover system, cant seem to find any meathods when googling etc.
thanks.
Your answer
Follow this Question
Related Questions
Trouble with positioning 2 Answers
Fixing instantiated object in one direction? 0 Answers
GameObject positioning in World Space 2 Answers
View an array of the transform position/rotation of all game objects with a specified tag., 0 Answers
Wrong positioning of game objects after instantiating them 0 Answers