- Home /
Camera Orbiting Character: How to make an object move with another with no rotation
Hello. I am faced with a problem I need to tackle in my android game. I am reasonably new to scripting, and I will do my best to explain this problem.
I'm working on a camera that is supposed to orbit a third person character with touch swipe (it will be built on android). It is a 3d adventure game, third person character with joysticks for movement and touch swipe for camera rotation.
I need the main camera to be orbiting a character. My character is going to be rotating alot, so I need the camera to not rotate with the character.
-----What I've done now is:
1) I've created an empty object called "CameraPivot1" to my main character positioned at the neck.
2) I've created another empty object called "CameraPivot2", which has the main camera as a child to this. [CameraPivot2 is NOT a child to CameraPivot1].
3) I have a script that will rotate the 'CameraPivot2' and thus the camera AROUND this point.
What I need is a script that will position CameraPivot2 exactly where CameraPivot1 is. My character will be moving+rotating left + right, and it is important to keep the position of CameraPivot2 onto CameraPivot1. How does the c# script go? all I know is that is that it must be in the void update() because I need to update the position of CameraPivot2 onto CameraPivot1 every frame.
-----Why am I doing this? I've tried parenting the two together and using quaternion rotation script in CameraPivot2 to cancel the rotation of the camera from its parent, but this has ended up with jittery camera movement whenever I rotate the character AND camera at the same time.
SO... if I keep CameraPivot 1 and 2 NOT parented with each other, but held together, when I rotate the character, it will rotate CameraPivot1, but NOT CameraPivot2, and since the main camera is a child to CameraPivot2, it will not rotate the camera etc... more stable and no jitter. How does the C# script go?
Answer by shaneK001 · Jun 26, 2014 at 02:15 PM
I'm not sure what you're trying to do exactly, perhaps an image would work better? Anyways, If you have a target object somewhere on scene. It doesn't need the same position as camera, it's just for the target rotation.
Then, use something like this script: http://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.html
Attach that to the camera you want to have independent rotation, drag your target rotation object on in the inspector, and then rotate that target object another way to control your camera. That's what I think you're trying to do anyways.
Thanks for your answer shane$$anonymous$$001, but that is not exactly what I am looking for. This picture should hopefully explain.
The point of doing this is to have the camera orbit rotation to be independant to the main character, but at the same time, move WITH the character. (When I swipe, it is CameraPivot2 that rotates, which makes the camera orbit this).
I'm looking for a simple script that would make one object and another object "stick" together.
In this case, I'm looking for a script that can make CameraPivot2 "stick" with CameraPivot1 at all times WITHOUT parenting them together.
(I've parented them together before but what happens is whenever the character rotates, the camera spins out of control. I've tried quaternion rotate to cancel this inherited rotation, but that has just left me with jittery camera rotation).
Just the position? make this a c# script (everything between // and //) called $$anonymous$$atchTargetPosition and put it on the camerapivot2 object as a component. Do not make it a child or a parent of the other objects. You must drag the target on in the inspector
//
using UnityEngine;
using System.Collections;
public class $$anonymous$$atchTargetPosition : $$anonymous$$onoBehaviour {
public Transform target;
void Update() {
transform.position = new Vector3(target.position.x,target.position.y,target.position.z);
}
}
//