- Home /
Question by
mushdevstudio · Jul 23, 2014 at 09:57 PM ·
axisfollow
Follow only on Z axis?
How do I get the following code to only follow on 1 axis?
using UnityEngine; using System.Collections;
public class Follow : MonoBehaviour { public GameObject Subject; private Vector3 _offset;
void Start ()
{
_offset = Subject.transform.position - transform.position;
}
void Update ()
{
transform.position = Subject.transform.position - _offset;
}
}
Comment
Wiki
Answer by yekush · Jul 23, 2014 at 11:04 PM
Are you trying to get an enemy to follow your character, but only on the z axis?
If using Javascript isn't a problem:
Subject : Transform;
function Update(){
transform.position.z = Subject.position.z;}
This will just transport the object its attached to instantly to its location every frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotation Object Follow Help 2 Answers
Camera rotation around player while following. 6 Answers
unity 2d C # script follows the enemy player. 3 Answers
make enemy only follow along the X and Z axis 1 Answer
Lock main camera on x-axis 2 Answers