- Home /
Is there a way to lock the local position of a game object?
I have a sphere as my player, and I am scripting it so that when I press a button, a game object becomes a child to my player object. I don't know what's going on, but I just can not get the child to lock it's relative position. I just want it floating in space beside my sphere, but nothing seems to work. The child just wanders off into empty space, slowly twirling around, and if I rotate about the vertical axis, it gradually gets thrown further off, as if affected by centrifugal force.
I've tried things like transform.localPosition = Vector3.zero;, and transform.position = player.position + offset;, and even borrowing script from the SmoothFollow camera script, but just nothing is working. I just want my game object to behave in movement as if it's a camera, can anyone help?
This is my script (in js):
[code]
#pragma strict
var player : Transform;
var attachToObject = GameObject;
var offset : Vector3;
function Start ()
{
player = GameObject.FindWithTag("Player").transform;
}
function Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
transform.parent = GameObject.FindWithTag("Player").transform;
rigidbody.useGravity = false;
transform.position = player.position + offset;
}
}
[/code]
(Sorry, I am trying to format this to show the code correctly too...)
(Sorry, I am trying to format this to show the code correctly too...) : You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window. I have done it =]
I'm not sure about this, as i'm not exactly sure what you are trying to do, but you may be able to achieve the behavior with the joint component
it could only ever twirl on it's own if physical. set the rigidbody to is$$anonymous$$inemtic while you went to have this behavior.