So like I said, I’m new to Unity… 2 days to be exact. I’m working on building a camera and haven’t been able to find what I need. Here’s the code for my character:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var xSpeed = 250.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var x = 270.0;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
else if (Input.GetKey("a")) { //rotate person
x -= .28 * xSpeed * 0.02;
transform.localRotation = Quaternion.AngleAxis (x, Vector3.up);
}
else if (Input.GetKey("d")) { //rotate person
x += .28 * xSpeed * 0.02;
transform.localRotation = Quaternion.AngleAxis (x, Vector3.up);
}
}
if (Input.GetAxis("Vertical"))
animation.CrossFade("Run");
else
animation.CrossFade("Idle");
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
And here is the code for my camera, which is part of my character:
var target : Transform;
var distance = 7.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
private var origx = -90;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (Input.GetKey("a")) { //rotate camera
//origx = x -= .28 * xSpeed * 0.02;
//transform.LookAt(target);
}
else if (Input.GetKey("d")) { //rotate camera
//origx = x += .65 * xSpeed * 0.02;
}
else {
if (target Input.GetMouseButton(0)) { //if mouse clicked, rotate camera
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
else if (Input.GetKeyUp("f10")) { //switch between 1st and 3rd person views
if (distance > 0) {
distance = 0;
y = 0;
}
else {
distance = 7;
y = 45;
}
}
else if (Input.GetMouseButtonUp(0)) { //reset camera to original spot
x = origx;
if (distance == 0)
y = 0;
else
y = 45;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0) { //zoom out
if (distance < 12 distance != 0)
distance += .5;
}
else if (Input.GetAxis("Mouse ScrollWheel") > 0) { //zoom in
if (distance > 4)
distance -= .5;
}
if (distance > 0)
Calculate3rd();
else
Calculate1st();
}
}
function Calculate3rd() { //recalculates angle of camera
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
function Calculate1st() {
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 1.5, .5) + target.position;
transform.rotation = rotation;
transform.position = position;
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
So what I’m having an issue with is my character rotation. When I hit ‘a’ or ‘d’, the character should rotate and the camera should stay center to the back of the character. The character does rotate, but the camera angle is always off. I wasn’t sure if I needed my camera to rotate or look at the target, so I commented out that code, and it works until I let go. Then it resets to the original position. Any help on what I need to do?
