- Home /
Locking on target with angle (mostly working)
Hi, Im a bit new to unity and 3D programming in general so I apologize for this basic question. Furthermore, it appears that is a very common question but after looking around I guess Im just not understanding some key concepts.
At the moment I have a turret object that changes rotation based on the players position. (locked to y) Its working fine. The turret also fires a fireball every second from a "spawn point" in front of it. What I want to happen is to have the spawnPoint, which it fires from, angle down and up based on the players position. I thought I could do this with LookRotation but it fires straight all the time. I assume this is because of the transform.forward when I fire.
Here is my code for my script.
var LookAtTarget:Transform;
var damp = 1.0;
var bulletPrefab:Transform;
var passedTime = 0.0;
var shootRate:double = 1.0;
var homingSpeed:double = 3.0;
var minDistance:double = 3.0;
function Update ()
{
//x and z lock
if (LookAtTarget)
{
//Move toward player position
//float fX = desX-playerX;
var fx = gameObject.Find("Worm").transform.position.x - transform.position.x;
var fy = gameObject.Find("Worm").transform.position.y - transform.position.y;
var fz = gameObject.Find("Worm").transform.position.z - transform.position.z;
var dist = Mathf.Sqrt(fx * fx + fy * fy + fz * fz);
if (dist > minDistance)
{
var step = homingSpeed / dist;
transform.position.x += (fx * step) * Time.deltaTime;
transform.position.z += (fz * step) * Time.deltaTime;
}
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
//Move shooting mechanism angeled at player
var rotateFireball = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.FindChild("spawnPoint").transform.rotation = rotateFireball;
//transform.FindChild("spawnPoint").transform.rotation = Quaternion.Slerp(transform.FindChild("spawnPoint").transform.rotation, newRotateFireball, Time.deltaTime * damp);
passedTime += Time.deltaTime;
//1 second has passed
if (passedTime >= shootRate)
Shoot();
}
transform.rotation.x = 0;
transform.rotation.z = 0;
}
function Shoot()
{
var bullet = Instantiate(bulletPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity);
bullet.gameObject.tag = "enemyProjectile";
bullet.rigidbody.AddForce(transform.FindChild("spawnPoint").transform.forward * 1000);
passedTime = 0;
}
Im not even fully sure why LookRotation(target.position - transform.position) is necessary either. But it seems to work for the turret rotation. I figure you would just need LookRotation(target.position)
Anywho, if anyone can help, I would greatly appreciate it. Thanks!
Answer by so3arbelnox · Feb 13, 2012 at 07:07 AM
Fixed.
var rotateFireball = Quaternion.LookRotation(LookAtTarget.position - transform.position);
Should be:
var rotateFireball = Quaternion.LookRotation(LookAtTarget.position - transform.FindChild("spawnPoint").transform.position);
Your answer
Follow this Question
Related Questions
Need Help with random rotatition on game turret 2 Answers
Character rotating on supposedly locked axis 2 Answers
Turret script. Making it aim in heigth only. 1 Answer
3D Turret Rotation 0 Answers
3D nested Turret Prefab Rotation 1 Answer