- Home /
Circle strafing/Z-Targeting
This was something that I always wanted to do but could never manage to emulate.
Pretty much from what I can see, the character's movement vector is modified to orbit around the target, while assuring that the camera should have the target at all times, but I'm not sure how to actual modify the movement vector to do so. From what I researched, it would require the character to move in a diagonal like fashion when locked on to a target.
This is all I have so far in my Late Update. At this point, the character faces the target, but the camera doesn't stay directly behind the player.
void LateUpdate ()
{
if (target)
{
xSmooth = Mathf.SmoothDamp(xSmooth, x, ref xVelocity, smoothTime);
ySmooth = Mathf.SmoothDamp(ySmooth, y, ref yVelocity, smoothTime);
ClampAngle(ySmooth, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(ySmooth, xSmooth, 0);
// posSmooth = Vector3.SmoothDamp(posSmooth,target.position,posVelocity,smoothTime);
posSmooth = (target.position + player.position)/2; // no follow smoothing
var playerLook = new Vector3(target.position.x, player.position.y, target.position.z);
player.LookAt(playerLook);
transform.rotation = rotation;
transform.position = rotation * new Vector3(0.0f, 0.0f, -distance) + posSmooth;
transform.LookAt((target.position + player.position)/2 + new Vector3(0,0,3));
}
}
Any help would be appreciated.
Hey, I have the Camera Slerping into a Vector3 position fixed behind the Player and Baked the Root Transform Rotation in the Animation Source so the Player GameObject doesn't rotate during the Attack Animation (causes the Vector3 position to make the Camera jitter).
// Smoothly move Camera Position into lockOnPosition child object of Player as per smoothSpeed cameraTransform.position = Vector3.Slerp(cameraTransform.position, lockOnPosition.position, smoothSpeed * Time.deltaTime);
I have the issue where I can't get the rolling movement to work (where it orbits around the locked on enemy). Would you $$anonymous$$d sharing yours with me? :)
Answer by SrBilyon · Jan 27, 2013 at 03:15 AM
I managed to make a similar camera by merging and modifying some of the basic camera scripts provided by Unity:
using UnityEngine;
using System.Collections;
public class Orbit : MonoBehaviour
{
public Transform target, player;
float distance = 10.0f;
float xSpeed = 1;
float ySpeed = 1;
float yMinLimit = -20;
float yMaxLimit = 80;
private float x = 0.0f;
private float y = 0.0f;
float smoothTime = 0.3f;
private float xSmooth = 0.0f;
private float ySmooth = 0.0f;
private float xVelocity = 0.0f;
private float yVelocity = 0.0f;
private Vector3 posSmooth = Vector3.zero;
private Vector3 posVelocity = Vector3.zero;
public float zOffset = 3;
//@script AddComponentMenu("Camera-Control/Mouse Orbit smoothed")
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
// The distance in the x-z plane to the target
// the height we want the camera to be above the target
public float height = 5.0f;
// How much we
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
void LateUpdate()
{
if (target)
{
xSmooth = Mathf.SmoothDamp(xSmooth, x, ref xVelocity, smoothTime);
ySmooth = Mathf.SmoothDamp(ySmooth, y, ref yVelocity, smoothTime);
ClampAngle(ySmooth, yMinLimit, yMaxLimit);
posSmooth = (target.position + player.position) / 2; // no follow smoothing
var playerLook = new Vector3(target.position.x, player.position.y, target.position.z);
player.LookAt(playerLook);
transform.LookAt(player.position);
// Calculate the current rotation angles
var wantedRotationAngle = player.eulerAngles.y;
var wantedHeight = player.position.y + 2;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = player.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x, height, transform.position.z);
// Always look at the target
transform.LookAt(player);
}
}
static void ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
//return Mathf.Clamp (angle, min, max);
}
Your answer
Follow this Question
Related Questions
When moving camera I like my circle Plane look at cam. 0 Answers
Rocket launcher script help 1 Answer
Camera re-target 2 Answers
Changing AI target, after destroying gameObject 1 Answer
revise enemy code to player code 0 Answers