Can't lock rotation axis

Hi everyone,

I have an object that I want to rotate horizontally over time towards it’s destination object but when I lock the x and z axis it won’t rotate at all. What am I doing wrong ?

Thanks in advance.

public class move : MonoBehaviour
    {
        public GameObject strt;
        public GameObject end;


        void Update()
        {

              Vector3 targetDir = strt.transform.position - transform.position;
                targetDir = new Vector3(0, targetDir.y, 0);
                transform.up = Vector3.RotateTowards(transform.up, targetDir, (100 * Mathf.Deg2Rad * Time.deltaTime), 0f);
}

}
1 Like

I think the issue is on the transform.up line, you probably want to apply the changes to transform.Rotate instead.

Thanks for your reply. When I change up to rotate the object doesn’t rotate at all. Can you give me an example how it should be done ?

Try this, I can’t really test right now, but this should be the direction to go with.

public class move : MonoBehaviour
{
    public GameObject strt;
    public GameObject end;
    Vector3 newRotation = Vector3.zero;

    void Update()
    {

        Vector3 targetDir = strt.transform.position - transform.position;
        targetDir = new Vector3(0, targetDir.y, 0);
        newRotation = Vector3.RotateTowards(transform.up, targetDir, (100 * Mathf.Deg2Rad * Time.deltaTime), 0f);
        transform.Rotate(newRotation);
    }

}

Thanks for your reply. Unfortunately it’s rotating on the wrong axis. Even if I change ‘up’ to ‘forward’ it keeps rotating in circles and it never points into the direction of the ‘end’ object.

Hi I’ve uploaded the file.
If anyone wants to take a look at it to see what the problem is that would be much appreciated.

https://ufile.io/y8dqg

Sorry I can’t really open that link, that site is way too shady.
You can tweeak the values to get the desired one.
Also, if you haven’t checked this yet, you can use lookAt Unity - Scripting API: Transform.LookAt

I tried to adjust the x,y and z value but I keep getting the same results. The object rotates on the wrong axis. Maybe I can mail the project to you ?

My previous code works but I can’t lock the other two axis and as a result at some point during the movement it rotates around the wrong axis.

It’s fine, you don’t have to send me your project, I actually sat down now and did it properly, using the unity example, just try this:
You can remove and change the extra parts, and change the speed value to whatever you want.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestScript : MonoBehaviour
{
    public GameObject strt;
    public GameObject end;
    float speed = 1f;

    void Update()
    {

        Vector3 targetDir = strt.transform.position - transform.position;

        // The step size is equal to speed times frame time.
        float step = speed * Time.deltaTime;

        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
        Debug.DrawRay(transform.position, newDir, Color.red);

        // Move our position a step closer to the target.
        transform.rotation = Quaternion.LookRotation(newDir);
    }

}

Thanks so much for your effort but it’s still rotating on the wrong axis even when I changed transform.forward to transform.up.

Something else must be wrong but it can’t be the code. In my editor I just made an empty gameobject and attached the move script to it. It must be something I did in the editor. Maybe you can upload your file so I can download it and see for myself. I have no problem downloading it from that website.

Cheers

I don’t have anything to upload, all I did was attach that script to a cube and assign target object in the inspector, then move the target around to see it, it worked perfectly.
You say that you have an issue with axis rotation, do you mean your model is facing the wrong direction? IF so, you can make your model/object be child of the object in that script and rotate it to the desired way based on how the parent object faces when the script is running.
If you still can’t get the results, please make a short video, post it on youtube, and show us exactly what you are facing.

Here it is. if you miss some info I re upload a better version.

Thanks for the vid, I re-created the script and it should work as you intend now:
In the inspector, lock the blockx,y or z according to the axis you want.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestScript : MonoBehaviour
{
    public GameObject strt;
    public GameObject end;
    public float speed = 100f;
    public bool blockX = false;
    public bool blockY = false;
    public bool blockZ = false;

    void Update()
    {

        Vector3 targetDir = strt.transform.position - transform.position;
        float step = speed * Time.deltaTime;
        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
        Quaternion quat = Quaternion.LookRotation(newDir);
        if(blockX)
            quat.x = 0f;
        if(blockY)
            quat.y = 0f;
        if(blockZ)
            quat.z = 0f;
        transform.rotation = quat;
    }

}

I added the following line so my object should move towards the end object:

transform.Translate(Vector3.up * (Time.deltaTime) * 2.5f);
t += 0.05f * Time.deltaTime;

However the object rotates sideways to the object and when I move it doesn’t go towards the end object. To make things clearer I added a texture (blue triangle) to the end object. The tweaking didn’t help me to get the object rotate to it’s front towards the end object. To be honest I never expected that locking an axis would be such a problem for me. Maybe it’s because I don’t use a 3d object but an empty gameobject ?

In my previous code it would rotate with it’s front towards the end object but I really wanted to lock the other axis because at some point it was making spins on his own axis. If you want I can upload a video of that as well so you can see what I mean.

Sorry for taking so much of your time. I really appreciate your effort.