Search Unity

OBJECT ANGULAR VELOCITY WHILST ROTATING TO FOLLOW PLAYER AS WELL

Discussion in 'Scripting' started by VAN-D00M, Oct 30, 2014.

  1. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Hi

    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:
    Code (CSharp):
    1. rigidbody.angularVelocity = -transform.forward * 2;
    2.  
    Using this section of code to follow the player which works:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         Vector3 lookDir = target.position - myTransform.position; lookDir.y = 0;
    4.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed * Time.deltaTime);
    5.  
    6.  
    7.         myTransform.position += myTransform.forward * 1 * Time.deltaTime;
    8.  
    9.     }

    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.

    Does anyone have an idea?


    Cheers people
     
  2. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    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.
     
  3. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    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.
     
  4. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    I read this post a while back
    http://answers.unity3d.com/questions/375589/how-to-rotate-two-axes-independently-simultaneousl.html

    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.

    Code (CSharp):
    1. // Rotate around Y axis
    2. transform.RotateAround(transform.up,Time.deltaTime * YForce);
    3.  
    4. // Rotate around Z axis
    5. transform.RotateAround(Vector3.forward, Time.deltaTime * ZForce);

    EDIT:

    This is what I came up with an this didn't work either.

    Code (CSharp):
    1. void Update()
    2.     {
    3.         Vector3 lookDir = target.position - myTransform.position; lookDir.y = 0; // zero the height difference
    4.         //myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed * Time.deltaTime);
    5.  
    6.         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; //moves enemy towards player
    7.  
    8.  
    9.         // Look at player
    10.         transform.RotateAround(lookDir,Time.deltaTime * rotationSpeed);
    11.      
    12.         // Spin effect
    13.         transform.RotateAround(Vector3.back, Time.deltaTime * 2);
    14.  
    15.         //rigidbody.angularVelocity = -transform.up * 2;
    16.  
    17.     }
    18.  
     
    Last edited: Oct 30, 2014
  5. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    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.

    Code (csharp):
    1.  
    2. //Apply this script to your MissileHelper Object
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class missle : MonoBehaviour {
    8.     private Transform MissileHelper; //Parent
    9.     public Transform Missile; //Child
    10.     public Transform _Target;
    11.     public float MissileSpeed = 3;
    12.     public float MissileRotSpeed = 100;
    13.  
    14.     void Start () {
    15.         MissileHelper = this.transform;
    16.     }
    17.  
    18.  
    19.     void Update () {
    20.         //Look at the target
    21.         MissileHelper.LookAt(_Target.position);
    22.  
    23.         //Move to the target
    24.         MissileHelper.Translate(Vector3.forward * MissileSpeed * Time.deltaTime);
    25.  
    26.         //Rotate the missile around the z axis
    27.         Missile.localEulerAngles += new Vector3(0,0,MissileRotSpeed * Time.deltaTime);
    28.     }
    29. }
     

    Attached Files:

    Last edited: Oct 31, 2014
    VAN-D00M likes this.
  6. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Wicked! That works really well! Thank you for the help @ForceX
     
  7. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Your welcome :)