Face Target Problem

I have this issue I haven’t been able to fix. I want to make my ship face the target. Where I store my Target is in the MainCamera1 under the script Ray1 and that’s where I have my Target for my hud system. I need to make the ship turn and face the Target. I was thinking something like this, but it doesn’t work.

Right now I just want it to face the Target when I have a target.

This is what I have for the other part of the system.

That works but I have to put the target in the editor part. I want it to work with my targeting system.

This is Ray1 the targeting system. It is on the Main Camera.

I also tried attaching Ray1 to the ship and having the other script do this.

Hi, in your Scene, you should have your Ship as a GameObject, and another GameObject that will be the target, you need to attach a script, (name it “LookAtTarget”) to your Ship,
first of all, in this script you will have to declare a public variable (example: TargetVar ) as a Transform ( type of variable should be Transform) , in order to assign it from the Inspector.
Then in the Hierarchy (or in the scene), select the Ship, now in the inspector you will see that TargetVar is visible (as an item/slot) below the script attached, so:
drag and drop the Target Gameobject from the Hierarchy to the TargetVar slot ;

Then, you can Either use this if you want your ship to look instantly at the target:

transform.LookAt ( TargetVar.position.x, TargetVar.position.y, TargetVar.position.z )  ;

put it in the Update function of the Script…

OR if you prefer some interpolation, to gradually rotate towards the Target:
-add an empty Gameobject in your scene And name it HelperObj ;
-In your “LookAtTarget” Script, add a public variable HelperVar as a Transform, and assign it from the Inspector: to do so, Select the Ship in the scene to see its Script attached and drag drop the HelperObj to the HelperVar slot.

-Now enter these lines in the update function of the script:

HelperVar.position = transform.position;
HelperVar.LookAt ( TargetVar.position.x, TargetVar.position.y, TargetVar.position.z ) ;
Quaternion HelperRotation =HelperVar.rotation ;
transform.rotation = Quaternion.Slerp( transform.rotation, HelperRotation, Time.deltaTime * 2.0f ) ;

where the 2.0f value can be replaced by any value, greater the value faster the movement of rotation toward the Target;
tell me if you need further assistance… ^^

yeah I had to re-edit my latest post, I think its Okay now, took me some time to write it … hope it helps ^^^, don’t forget to leave a reply … note that the example codes here are in C# …

EDIT:
you wrote:
transform.LookAt(gameObject.Find(“Commander”).Ray1 .Target.target);

the wrong thing IMO is that you tried to search Objects… instead you should use variables that have been assigned from the Inspector as explained before - If necessary, see documentation here:

under “VARIABLES”.

Im not sure you know what I want. I want to make it so when I’m in the game and I click an object, it becomes my target. Than my ship turns toward it. Anyhow this is the newest version of my script I am working on. I think I just about got it. But there is just one error in it now.

using UnityEngine;
using System.Collections;

public class shipfacetarget : MonoBehaviour {
	public string target;
	public Transform target; 
	void Start () {

	}

	void Update () {
		if (Input.GetButtonDown ("Fire1")) {
			RaycastHit hit;
			Ray ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast (ray1, out hit))
				target = hit.transform.name;
			transform.LookAt(target);
		}
	}
}

Okay, so at a first glance, the problem seems to be here:
transform.LookAt(target);

instead, write:

    using UnityEngine;
    using System.Collections;
     
    public class shipfacetarget : MonoBehaviour {
        public Transform target = null ;
        void Start () {
     
        }
     
        void Update () {
            if (Input.GetButtonDown ("Fire1")) {
                RaycastHit hit;
                Ray ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
                if (Physics.Raycast (ray1, out hit))
                    target = hit.transform;
            }
            if ( target ){
                        transform.LookAt(target.position);
            }
        }
    }

note that if you want a smooth transition movements you may try to implement the method exposed in my old post…
seems to be OK now, but I did not tested it yet, so please try it and tell me ^^

EDIT:
I’ve corrected, so that it points only after you have selected the target… else you will have to keep clicking to aim the target…

Thanks that’s exactly what I want. Works now ^^

Im am not exactly sure how to implement your old post.
I’m trying to add it but I get this problem:

Ok, so first:
-add an empty GameObject in your scene And name it HelperObj ;
-In your “shipfacetarget” Script, add a public variable HelperVar as a Transform, and assign the HelperObj GameObject to this variable (with the help of the Inspector of course).

change you Code to this (the variable HelperVar is here):

        using UnityEngine;
        using System.Collections;
         
        public class shipfacetarget : MonoBehaviour {
            public Transform target = null ;
            public Transform HelperVar ; // <- assign your empty Gameobject to this one !
            void Start () {
         
            }
         
            void Update () {
                if (Input.GetButtonDown ("Fire1")) {
                    RaycastHit hit;
                    Ray ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
                    if (Physics.Raycast (ray1, out hit))
                        target = hit.transform;
                }
                // this will be the final rotation that points towards the target !
                Quaternion HelperRotation ;

                if ( target ){
                                HelperVar.position = transform.position; // place the helper on the ship !!
                                HelperVar.LookAt ( target.position.x, target.position.y, target.position.z ) ; // then, points the Target !!
                                HelperRotation = HelperVar.rotation ; // save this rotation as quaternion as its the default angle system...
                                transform.rotation = Quaternion.Slerp( transform.rotation, HelperRotation, Time.deltaTime * 2.0f ) ; // now, bring the magic with *SLERP* !!
                                // WHERE THE 2.0f value (after delta.time)  can be replaced by any value, greater the value faster the movement of rotation toward the Target; !!
                }
            }
        }

this should do the trick !!! tell me if you need further assistance… ^^ :mrgreen:

Hmm …

Assets/SCRIPTS/shipfacetarget.cs(21,35): error CS1501: No overload for method LookAt' takes 3’ arguments

I’m so confused, looks like it has 3 arguments to me. Am I missing something here?

Yeah, its my mistake, replace with this:

HelperVar.LookAt ( target.position ) ;

its rather obvious I were wrong with this line, I’m surprised u didn’t found out ! :wink:

Tip: if you wanted to fine tune or offset the position the ship would face to, try smthing like :

HelperVar.LookAt ( new Vector3( target.position.x + OffSet_varX, target.position.y + OffSet_varY, target.position.z + OffSet_varZ ) ) ;

Where “OffSet_varX” is a value added to offset from the original coordinates !
is there something else ?
^^

What exactly is OffSet_var?

Hay check it out. Given enough momentum you can actually orbit objects :

Hmm last question. How do you get it to stop Facing the target?

OffSet_varX is a variable that you can declare, it can be integer or float … its not a function. you put in it the value you want for offset…
don’t forget to declare it if you should decide to use it the way I’ve showed you ;

And to stop the ship facing the Target, look at the last condition in the script:
if ( target ){

here, in the script provided you could easily write that “target = null” whenever you need to stop ; because as you can see the turning code is placed in this condition block ( if "targe"t has a value different from “null”) ; place a code that can change the target value to null, Right before this last condition ( if ( target ){ ) ; OK ?

                if (Input.GetButtonDown ("RightClick_Or_Cancel")) {

                    target = null;

                }
// And then just after the same previous condition code:
                if ( target ){

                                HelperVar.position = transform.position; // place the helper on the ship !!

                                HelperVar.LookAt ( target.position ) ; // then, points the Target !!

                                HelperRotation = HelperVar.rotation ; // save this rotation as quaternion as its the default angle system...

                                transform.rotation = Quaternion.Slerp( transform.rotation, HelperRotation, Time.deltaTime * 2.0f ) ; // now, bring the magic with *SLERP* !!

                                // WHERE THE 2.0f value (after delta.time)  can be replaced by any value, greater the value faster the movement of rotation toward the Target; !!

                }

Edit: edited the code snippet ! now it should work !