- Home /
Parenting single transform attribute with offset?
I want the parent/link the transforms of two objects in one axis only with an ability to add an offset.
Similar to how you can set a Point Constraint to a single axis in Maya.
I have tried parenting through scripts had no progress.
It for a mini map camera that will operate two floors for a building. When the player moves up and down the floors, I what the camera to follow the player in the Y but not in the X and Z. This will allow me to position maps I have made in Photoshop in relevant position. so as the player moves up and down the different floor maps are reveled.
Thanks
Answer by whydoidoit · Aug 09, 2013 at 11:40 AM
You will need to write a script to have one object follow the other, rather than using parenting to achieve the effect you want.
FollowTargetObject.cs
using System;
using UnityEngine;
public class FollowTargetObject : MonoBehaviour
{
public Transform thingToFollow;
public Vector3 offset;
public Vector3 mask = Vector3.up;
void LateUpdate()
{
if(thingToFollow)
{
var followPosition = thingToFollow.position + offset;
var currentPosition = transform.position;
transform.position = new Vector3(Mathf.Lerp(currentPosition.x, followPosition.x, mask.x),
Mathf.Lerp(currentPosition.y, followPosition.y, mask.y),
Mathf.Lerp(currentPosition.z, followPosition.z, mask.z));
}
}
}
Using this code you can control how much the object with this script attached follows the target object by varying the parameters of mask between 0 and 1.
Thanks for your help.
I have a attached this C# script to camera that i want to follow the object.
I'm getting the following error in the console: Assets/Scripts/mapcam.cs(26,14): error CS0116: A namespace can only contain types and namespace declarations
I'm guessing i need to tell it what object i want it to follow but i'm unsure where to specific this.
I have only been using Unity for couple of weeks and co$$anonymous$$g from a 3D animation background i'm struggling with the scripting.
Thanks again for your help.
Hi $$anonymous$$att - well it sounds like you have some code weirdness going on! That message implies an incorrectly formed .cs file.
using UnityEngine;
using System.Collections;
public class $$anonymous$$ini$$anonymous$$apCam : $$anonymous$$onoBehaviour {
public Transform thingToFollow;
public Vector3 offset;
public Vector3 mask = Vector3.up;
// Use this for initialization
void Start ()
{
if(thingToFollow)
{
var followPosition = thingToFollow.position + offset;
var currentPosition = transform.position;
transform.position = new Vector3($$anonymous$$athf.Lerp(currentPosition.x, followPosition.x, mask.x),
$$anonymous$$athf.Lerp(currentPosition.y, followPosition.y, mask.y),
$$anonymous$$athf.Lerp(currentPosition.z, followPosition.z, mask.z));
}
}
}
// Update is called once per frame
void Update () {
}
}
Well the Start function should be LateUpdate and the class name must be exactly the same as the filename including capitalization.
Your answer
