I have a diablo style camera where it’s kind of back, up, and to the side slightly of my player. I want the camera to follow the player just like if I was to parent it to the player BUT I don’t want the camera to rotate. How an I achieve this?
I know I would have to move the camera along the x z axis and probably move it the same as the player character is moving but that vector is in the player script so not sure if I can get that or even if that’s really the right way to do it.
I think if you create an empty game object, place it to the left of the player where you want it, make the player the parent of the object then it will stay in the correct position to the player. Then if you attach a rigidbody to that object and check ‘freeze rotation’ then that object won’t rotate. Then make that object the camera’s target.
What do you mean by the camera’s target? I think this is a create idea, but if I make the sphere (I used a sphere instead of empty game object just so it’s easier to see in editing mode, will disable renderer in game mode) a child of the character and the camera the child of the sphere the camera still rotates. So am I missing something about the target?
Most camera scripts work by following a target around. Usually there’s a line that say something like
var target : Transform;
That creates a slot in the inspector window that you drag and drop your player into. Then the camera follows that player. What I was suggesting was creating a target that does not rotate, so the camera does not rotate.
I think you should post your scripts so we can take a look at how you are doing things. The camera script, specifically.
I don’t have any camera script currently. All I did was place the camera where I wanted it manually in the scene and if I parent the camera to the player it follows it, but of course it rotates also, which is the thing I don’t want it to do.
Yeah I did that but the camera still rotates. I think because the top level parent is still the character because you’ll have:
Character
Sphere Game Object
Main Camera
So it seems like because the Character is still the main parent and it’s allowed to rotate, the sphere game object doesn’t rotate around itself but it pivots around the character since it’s a child of it, and that means the camera also rotates then.
Here’s a simple script to keep the camera behind your character as he turns, always pointing the same way relative to the character:
var target : Transform;
public var distance = 2.8;
public var targetHeight = 2.0;
private var x = 0.0;
private var y = 0.0;
function Start () {
var angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
}
function LateUpdate () {
if(!target)
return;
y = target.eulerAngles.y;
// ROTATE CAMERA:
var rotation:Quaternion = Quaternion.Euler(x, y, 0);
transform.rotation = rotation;
// POSITION CAMERA:
var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
transform.position = position;
}
Put the camera where you want it relative to the character, then run your game. In the inspector window, adjust the distance variable to get the distance right, and adjust the Target height to control up and down.
Hi, I’m experimenting with this code you’ve provided right here, and I was wondering how to specify the position and rotation of the camera using JavaScript, rather than manually positioning and rotating the camera from the Unity Inspector.
The reason I ask is because I want the player to be able to switch between certain viewing angles at the press of a button, rather than having just one angle all the time.
EDIT: I played around with it a bit, and I got the camera’s position to change by using relativePosition = Vector3(0, -2, 6);. However, I’m still not quite sure how to adjust the rotation via JavaScript. Any help?
If I understand correctly what you are wanting to do…I created an empty game object and attached a simple script to match the players position (but not rotation). Then using the included camera script (forget what it’s called) I set this gameobject to it’s lookat target.
Then if you do want to rotate the camera around the player, you rotate that gameobject manually. In this case though, you would want to make sure your player control script takes the rotation it into account otherwise your charachters motion won’t be relative to the camera. If you don’t want to rotate the camera around the player then you don’t have to worry about it.
By the way...just to see if I do understand what you are asking about. Are you looking to simulate the "camera" movement in the old isometric games? That's more or less what I suggested does.
I’ve done those tests and the scripts runs fine. Thou I have a question, how could i move the camera to a different position “automaticly” if there’s an object in front of the player that obstructs the vision? like an building or something?
I’m not sure if you guys worked this out, but I had an issue similar to this with trying to establish an online game where the camera was a third person view that followed the player, I did not want pressing reverse to result in moving “forward” in the other direction.
I have the 3rd Person Controller with no camera attached to it. The character happens to be the “SwordGirl” model, so where you see SwordGirl would be replaced with the name of the Transform child of the Controller.
I have a single Camera free of any parents that has a networkView attached, but that depends on your situation whether or not you need it.
That script is attached to the Camera (Javascript) and the 1.5 was to provide a slight offset in the height, but you could even set that to a variable. Attach the script target to the main transform portion of your character and off ya go. I hope this helps. I know it isn’t anything fancy, but I’m only a few days in