- Home /
Change a Transform to Aim to the center of a Collider. Not (0,0,0)
Hi: I am having trouble with figuring out how to change the set transform of a public transform to move towards the center of an object and not it's (0,0,0). I would like not to add another gameobject.
Code Ex
public Transform target;
public GameObject missleSmall;
void OnFire()
{
missleSmall.GetComponent<missleSmall>().myTarget = target;
}
"missleSmall" has a script that looksAt "myTarget" and moves forward.
This works fine in script. The problem is that the "missleSmall" goes towards the (0,0,0) of "myTarget" the feet. What I would like to do is have the "missleSmall" go towards the center of the target, the body.
This is what I tried to do:
TR.currentTarget.localPosition = new Vector3(0,1,0);
missleSmall.GetComponent<missleSmall>().myTarget = target;
As the target size is (2,2,2) the center of it would be 1 on the Y axis. But the "target" gets moved to the world coordinates. Just wondering if there is a way to do this without adding more gameobjects to the enemy?
Answer by neonblitzer · Mar 01, 2015 at 08:51 PM
Short of adding a "target position" child GameObject to the enemy (which sounds the easiest solution from my POV), you should store a Vector3 offset from the enemy's base position to the desired target position, for example in the enemy's MonoBehaviour as a public field.
public class Enemy : MonoBehaviour {
public Vector3 targetOffset; // set this to (0, 1, 0) in the editor
...
}
public class missleSmall : MonoBehaviour {
public Enemy myTarget;
public void Update() {
transform.LookAt(myTarget.transform.position + myTarget.targetOffset);
...
}
}
public class MyPlayer : MonoBehaviour {
void OnFire() {
missleSmall.GetComponent<missleSmall>().myTarget = target.GetComponent<Enemy>();
}
}
Alternatively, you could create a separate reusable MonoBehaviour just to handle the offset, something like:
public class OffsetContainer : MonoBehaviour {
public Vector3 offset;
}
...add that to your enemy, and retrieve that component in your code.
PS. Class names usually start with an uppercase letter, so missleSmall -> MissleSmall. :)
The project started out in JS which is the reason some of my classes start in lower case. Still converting everything to C#. I decided to go with a target position empty gameobject in the spot where I would like the projectiles to move towards, due to enemies being different sizes. Thank You
Answer by sparkzbarca · Mar 01, 2015 at 08:35 PM
so there are two ways to solve this.
Either you open the model in a modelling program and do a origin to center mass etc to move the origin of the model to it's chest
or
you add a variable called target offset to the target that tells objects targeting it it's center relative to it's origin.
the thing is right now your targeting a game object not a position according to your code. presumably at some point in the missilesmall script you actually use target.position. your going to want to use target.position + target offset to aim correctly. however creating an empty game object isn't an issue, it's literally a point in space. The only time it's going to be an issue is if you use find commands to search all the game objects. Get 5000 empty objects to search and you'll have a problem. i understand your desire to have a "clean" scene.
I suggest doing the first one, an objects position should be it's center not it's feet. This can end up skewing with scaling, rotation, position (as you've just seen) unity has boxes and such origin'd to there center, keep with the standard.
moving the models origin is super easy by the way.
download blender, a free modelling program. open the model press T to bring up the transform menu. origin to center of mass.
Control + A to apply
save the model
viola! it'll reimport to unity with the proper position now in the center of the model.
So what I am understanding from your post is to in Ex.2 is to make a empty gameobject in the center of the enemy for the missle to target? The Ex.1 is not an option due to other complications.
What do you mean by offset variable? Is that a vector3 or a object?
I would like a offset vector3 variable to add an amount to make the missle to aim alittle higher.
Your answer
Follow this Question
Related Questions
What is the algorithm to assign to transform.right? 1 Answer
How to use x axis for transform.LookAt 3 Answers
Rigidbody to follow player around 1 Answer
transform.LookAt boundaries 1 Answer
How to prevent Z axis rotation ? 0 Answers