Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# unity follow object

public class Follow : MonoBehaviour
{
    public Transform Target;
    public Transform Self;
    Vector3 tempVec3 = new Vector3();

    void LateUpdate()
    {
    	// If the target is active in the scene
        if (Target != null)
        {
            tempVec3.x = Target.position.x; // Follow x position
            tempVec3.y = Target.position.y; // Follow y position
			tempVec3.z = this.transform.position.z; // For 2D
            // tempVec3.z = Target.position.y; // For 3D
          	this.transform.position = tempVec3;
        }
        // If the target is NOT active in the scene
        else if (Target == null)
        {
            tempVec3.x = Self.position.x;
            tempVec3.y = Self.transform.position.y;
            tempVec3.z = Self.transform.position.z;
            Self.transform.position = tempVec3;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: ef core detach entity 
Csharp :: parse int in c# 
Csharp :: unity json save array 
Csharp :: c# datetimepicker set weeks after today 
Csharp :: C# HttpClient POST request 
Csharp :: bootstrap modal popup 
Csharp :: mvc list to jsonresult 
Csharp :: T SQL Format GetDate() 
Csharp :: how to execute linux command from c# 
Csharp :: Check if two linked lists merge. If so, where? 
Csharp :: c# list sort by property string 
Csharp :: c# array map 
Csharp :: C# .net core convert to int round up 
Csharp :: if char is upper csharp 
Csharp :: how to save a c# dictionary 
Csharp :: c# how to refreshyour bindingsource 
Csharp :: weighted random c# 
Csharp :: C# push list 
Csharp :: c# create a zip files 
Csharp :: c# to binary 
Csharp :: how to move a gameobject to another object 
Csharp :: how to make an object move in unity 
Csharp :: get current playing animation of animator unity 
Csharp :: Unity Rigidbody how to set zero momentum 
Csharp :: list all files in directory and subdirectories c# 
Csharp :: c# bytes to image 
Csharp :: create line in unity 
Csharp :: get client ip address c# 
Csharp :: c# string contains any of list 
Csharp :: capitalize first letter c# 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =