- Home /
Calculate a new position instead of using Vector3.back
I am writing a simple avoidance function. Spheres (enemies) target and move towards a capsule (player) to hinder its movement. When a sphere hits the capsule I instruct it to jump back in its own local space, after which it resumes moving towards the enemy again:
enemy.transform.Translate(Vector3.back);
After the jump I check whether its new position might overlap with another enemy, if so, I move that enemy backwards in it's local space as well.
Now instead of translating multiple enemies I'd prefer to calculate an enemy's new position to see if it interferes with another enemy position.
The trouble I have is that the enemy has it's own local rotation and I do not know how to calculate its new position after a vector3.back translation that takes in account its local rotation (or maybe direction, actually).
I simply do not know and cannot find the math to accomplish this.
So ehm,.. Help? Thanks in advance!
Remi
Answer by Dave-Carlile · Jan 04, 2016 at 09:45 PM
transform.forward gives you the object's direction in world space. You can use -transform.forward
for the reverse direction.
That said, is there a reason you don't just use the built-in physics? That should be able to resolve this sort of thing.
Hey Dave,
$$anonymous$$any thanks for your reply!
I did not realize transform had a .forward method (found it in the docs now though).
Neither Did I know you could use -($$anonymous$$us)transform.
This looks like something that can be very useful, going to try it out asap.
As for the physics, i'm targeting html5 so if I can omit the physics library it will be a smaller download. I'm only using spheres, so simple distance calculations are only needed for overlapping stuff.
$$anonymous$$any thanks again!
It's not -(transform) it's -(transform.forward). In other words it's a vector that's being negated, not a Transform.
Been working with this, but somehow it looks as if the new enemy position is not in the position I expect it to be. $$anonymous$$y code:
public void EnemyAvoidance (GameObject avoidObject)
{
Vector3 temppos = avoidObject.transform.position;
temppos +=-(transform.forward);
print (temppos);
Debug.Break ();
}
The top of the image is the situation when an enemy(white sphere) touches the player(green sphere), including the new calculated position in the console. After above code I expect the enemy to be below its current position but in line with the purple arrow (p).
However, when I enter the console output in the inspector manually the enemy ends up in a different position.
Not quite sure what to make of this.. Any help much appreciated! Thanks.
Am I right in thinking that the white sphere is the "avoidObject" and the green sphere is the gameObject which has the EnemyAvoidance function's script on it?
If so, can you explain where you get the purple line's direction from? It looks like it's calculated relative to the white sphere's rotation. But if my first para is correct then you would expect the white sphere to move in a direction deter$$anonymous$$ed by the green object's rotation (because you're using transform.forward
rather than avoidObject.transform.forward
).
The white object is indeed the "avoidObject". EnemyAvoidance however is just a class with a default transform of 0,0,0;0,0,0;1,1,1
So yeah I should use avoidObject.transform.forward, but unity wont let me input avoidObject.-(transform.forward); Guess I have the syntax wrong?
Your answer
Follow this Question
Related Questions
How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers
how to find direction between two points 2 Answers
Finding difference between gameObject's x coordinate according to one object's transform 1 Answer
How to rotate an object to face the direction it's going? 1 Answer
Creating a bouncing game without using physics - Vector3 math problem, 2 Answers