Missle/Bullet script. C#
Okie so im wanting to have a targeting system on my cargo space ship, in a way that i can click on a object in game and then this will show up as a target and when i press space all 8 turrets on my ship will turn slowly to face the target, then once in a 100 radius i can press the space bar and the turrets will fire a round of 5 large bullets which will only travel in a straight line. ive tried to make a target click on object system using:
void OnMouseDown()
{
enemy = gameObject.transform;
}
however when i click on the object it does not set it as the target.Think ive done that Completely wrong. Would anyone have some pieces of code i can mash together with mine or so?.
Answer by A_Li_N · Dec 30, 2015 at 04:40 AM
In your example, a gameObject with that script attached would set itself as a target; 'gameObject.transform' is the 'current' gameObject's transform. You would want to attempt to find the object at the mouse's position and set it in the onclick. I do not know how to do that yet :( (all of 2 days of exploration)
It all depends on the organization of your scripts, but this is what I have done (so far):
public void RotateTowards(Vector3 target)
{
//Get the difference between the target location and the object location
Vector3 dir = target - transform.position;
//Normalize it
dir.Normalize();
//Find the angle between the current facing and the target, flip it 90 degrees so it is on the Z axis
float zAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
//Get the rotation that the object would be at when pointing at the target
Quaternion desiredRot = Quaternion.Euler(0, 0, zAngle);
//Get what the rotation would be if we applied it
Quaternion rotationAtEndOfThisFrame = Quaternion.RotateTowards(transform.rotation, desiredRot, RotationSpeed * Time.deltaTime);
//If we are not attached to something or if MaxRotation is 0 or the end difference between the current rotation and rotationAtEndOfThisFrame is less than MaxRotation
if (transform.parent == null || MaxRotation == 0 || Quaternion.Angle(rotationAtEndOfThisFrame, transform.parent.rotation) <= MaxRotation)
{
//Set the rotation
transform.rotation = rotationAtEndOfThisFrame;
}
}
'target' can be any vector3, meaning another transform's position or the mouse's position.
Answer by jgratrix · Dec 30, 2015 at 01:21 PM
@A_Li_N
Hey thanks for that, yes i found out a slightly easier way to find the target using raycast:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse is down");
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.tag == "Enemy")
{
Debug.Log("It's working!");
}
else {
Debug.Log("nopz");
}
}
else {
Debug.Log("No hit");
}
}
}
So all i would need to do is set HitInfo equal to target then wallah, make a simple missile script and use this target and it should work. As for the turret i should be able to use Tranform.LookAt(HitInfo); and it should look at the target.
Your answer
![](https://koobas.hobune.stream/wayback/20220612054308im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stop this Behaivour after its done with its its job. 0 Answers
Can you point me in the direction of how to get space physics like the one in this video? 1 Answer
Detect if Game Object has been clicked on in 3Dspace 1 Answer
Particle rotation problems in Unity 2017.1.0f3 2 Answers
Screen Space - Camera makes bullets fly in wrong directions 1 Answer