Struggled with the title as you may have noticed. Not all that catchy.
Chucking this one out there a bit. I’ve got top down game with a player and a missile. Simply I want the missile to follow the player which I have achieved already. However, for effect I would like to spin (rotate if you like) towards the player as well.
Problem is, the missile is having to rotate on the Y axis to follow the player, but at the same time I would like to rotate it constantly on the Z axis regardless of what the Y axis is.
I made this piece of code (in Start) to spin it on its Z axis which works:
Of course they don’t work together which is expected. I’ve tried different methods for both and can’t find two pieces that work together. I’m sure it can be done because anything is possible but I am struggling to find two methods that don’t conflict with each other.
The simple way is to make your missle a child of an empty gameObject. Have the gameObject execute your missle follow command and have your missle rotate on any axis you want without messing up your follow path.
That would work but both scripts are trying to rotate the object.
It works for a start, the missile spins in a cork screw effect but once I move to the left or right, the missiles path alters by rotating on the Y axis and then it just tumbles. Which makes sense so that means poor scripting? Its like I want to modify the Y and Z values independently of each other if that makes sense.
There the solution to his problem are these two lines. I tried something like this but couldn’t figure out how to implement my follow player code using this method.
// Rotate around Y axis
transform.RotateAround(transform.up,Time.deltaTime * YForce);
// Rotate around Z axis
transform.RotateAround(Vector3.forward, Time.deltaTime * ZForce);
EDIT:
This is what I came up with an this didn’t work either.
void Update()
{
Vector3 lookDir = target.position - myTransform.position; lookDir.y = 0; // zero the height difference
//myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; //moves enemy towards player
// Look at player
transform.RotateAround(lookDir,Time.deltaTime * rotationSpeed);
// Spin effect
transform.RotateAround(Vector3.back, Time.deltaTime * 2);
//rigidbody.angularVelocity = -transform.up * 2;
}
Unfortunately when using any type of LookAt methods it becomes very difficult to have any external control over an objects eulerangles your self. This is where helper objects come into play. You allow the helper object to be controlled by your LookAt method while your missile is free to do as you wish.
I made a simple demo. The missile will follow / attack a target and rotate around its local z axis.
//Apply this script to your MissileHelper Object
using UnityEngine;
using System.Collections;
public class missle : MonoBehaviour {
private Transform MissileHelper; //Parent
public Transform Missile; //Child
public Transform _Target;
public float MissileSpeed = 3;
public float MissileRotSpeed = 100;
void Start () {
MissileHelper = this.transform;
}
void Update () {
//Look at the target
MissileHelper.LookAt(_Target.position);
//Move to the target
MissileHelper.Translate(Vector3.forward * MissileSpeed * Time.deltaTime);
//Rotate the missile around the z axis
Missile.localEulerAngles += new Vector3(0,0,MissileRotSpeed * Time.deltaTime);
}
}