- Home /
Question by
Forged1212 · Jul 20, 2016 at 08:57 PM ·
c#rotationscripting beginner
How can I have the turret on top of the parent ship be similar transforms?
Hello Unity community, I'm having a little issue with the child turret on my ship shown below.
The turret's rotation isn't relative to the parent ship. It aims at the target but it becomes offset from its rotation. I'm pretty new to Unity and learning CSharp so I really don't know what I could really do here at the moment. Here is my TurretController script if anyone was wondering. Any help would be appreciated.
using UnityEngine;
using System.Collections;
public class DestroyerTurretController : MonoBehaviour {
public Transform target;
private bool TurretSeek;
GameObject prefab;
public Transform projectilespawn;
public float fireRate = 0.5f;
private float nextFire = 0.0f;
void Start () {
prefab = Resources.Load("projectile") as GameObject;
}
void Update () {
if (TurretSeek == true & Time.time > nextFire)
{
transform.LookAt(target);
GameObject projectile = Instantiate(prefab) as GameObject;
projectile.transform.position = transform.position + projectilespawn.transform.forward * 2.3f;
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = projectilespawn.transform.forward * 100;
rb.rotation = projectilespawn.rotation;
nextFire = Time.time + fireRate;
}
}
void OnTriggerEnter(Collider other)
{
TurretSeek = true;
}
}
destroyerturrettest.png
(154.4 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612085151im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Multiple Cars not working 1 Answer
[C#] Quaternion Rotations with Input.GetAxis problems. 1 Answer
Camera that follows rotating object with mouse input 0 Answers
How to detect right answer or not 0 Answers