I’m a beginner to coding and unity in general, and I’m trying to figure out how to make one object replicate the movements of another object, and also pivot around that object when it rotates, so basically just like parening, but i can’t just parent them together because for collision and physics reasons they both have to be rigidbodies, and that doesn’t work right apparently…
the “parent” object is a player controller that would be moving with keyboard input and the “child” object is floating horizontally above the ground in front of the player and will be controlled with mouse input, but i need the child object to move along and in relation to the movements of the parent object, while still being physical and controlled by the mouse.
I thought about using a joint to connect them, but it would still not move the child object when the parent object would move, so i need to figure out how to make scripts that would create this behavior, and so i need some help with that, and i would love to hear any ideas you guys might have…
I am a beginner so i would really appreciate a short explenation of any remotely obscure concepts you may incorporate in your advice…
thanks in advance
Try this:
public Transform toFollow;
private Vector3 offset;
void Start()
{
offset = toFollow.position - transform.position;
}
void Update()
{
transform.position = toFollow.position - offset;
transform.rotation = toFollow.rotation;
}
This does not allow it to be controlled by my mouse input, and also it revolves with the player object but it doesn’t pivot around it.
for this early stage in prototyping i’m just using this simple script as the initial core concept of how to move the “child” object with the mouse
public int speed;
void Update ()
{
GetComponent<Rigidbody>().AddForce(transform.right * Input.GetAxis("Mouse X")* speed * Time.deltaTime);
GetComponent<Rigidbody>().AddForce(transform.forward * Input.GetAxis("Mouse Y") * speed * Time.deltaTime);
}
}
Is there a better way to create vertical mouse control for an object like this? obviously it’s just the beginning of the concept and i will have to add code to handle distance from the ground and rotation in relation to the ground, but whatever solution i use has to integrate\co-operate with this mouse control. (unless anyone can think of better mouse control it could be done with)
Possibly - without getting much into details - you could just parent a “dummy” empty GameObject and follow its movement and rotarion with the right object on the Update function.
I had thought about using an empty gameobject somehow, but in any way that i imagine it there just seem to be too many issues with how it would work in a complex 3D enviroment full of objects to collide with… also generally we need to use ‘addforce’ here for movement instead of just messing with the transform.position to realistically handle collisions (right?) and for that we need a rigidbody component on the actual object we control…
Unless anyone can think of a different better way to do all of this?
Use Rigidbody.MovePosition (Unity - Scripting API: Rigidbody.MovePosition). Also, you should do your translations on rigidbodies within FixedUpdate, rather than Update.
But how can i get it to pivot around the parent object?
also, doesn’t multiplying by time.deltaTime achieve the same result as doing it in fixed update?