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 :: ExpandoObject Syntax that Compile 
Csharp :: Propertychanged is not firing up when text is change 
Csharp :: hacker 
Csharp :: ef core totable 
Csharp :: asp.net razor get list without refresh 
Csharp :: linq conditionnally add where clause 
Csharp :: c# convert string to datetime any format 
Csharp :: IdentityServer vs JWT vs OAuth? 
Csharp :: c# SQLite execute Command 
Csharp :: unity tostring keep decimal part 
Csharp :: dfgf 
Csharp :: why icollection is use with virtual keyword in c# 
Csharp :: Post and Pre Increment operators in C# 
Csharp :: unity cone hit test spotlight lits object light on object test 
Csharp :: aws asp.net tutorial 
Csharp :: C# sprint key 
Csharp :: how to write an if statement with two checkboxes in c# 
Csharp :: AutoFixture ignore property 
Csharp :: orderby make sunday last day c# 
Csharp :: long to binary c# 
Csharp :: percentage random c# 
Csharp :: how to call method in different project in c# visual studio 
Csharp :: texture matrix 
Csharp :: polymorphism in c# 
Csharp :: access audio source from gameobject unity 
Csharp :: how to check if time is between two timespans in c# 
Csharp :: c# is not marked as serializable 
Csharp :: ik nothing is happening unity 
Csharp :: wpf scoll to on new item datagrtid 
Html :: html input integer and positive 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =