My objective here is to have a smoothed out "follow camera", for my space-fighter proof of concept game. The camera should match roll off the target object in all axis.
To that end I've "stolen" and modified this code from the unity Answers-site, and it works beautifully for X and Y (pitch and yaw), but it refuses to roll.
Code:
public float Distance;
public float Height;
public float RotationDamping;
public GameObject Target;
void LateUpdate()
{
var wantedRotationAngleYaw = Target.transform.eulerAngles.y;
var currentRotationAngleYaw = transform.eulerAngles.y;
var wantedRotationAnglePitch = Target.transform.eulerAngles.x;
var currentRotationAnglePitch = transform.eulerAngles.x;
var wantedRotationAngleRoll = Target.transform.eulerAngles.z;
var currentRotationAngleRoll = transform.eulerAngles.z;
currentRotationAngleYaw = Mathf.LerpAngle(currentRotationAngleYaw, wantedRotationAngleYaw, RotationDamping * Time.deltaTime);
currentRotationAnglePitch = Mathf.LerpAngle(currentRotationAnglePitch, wantedRotationAnglePitch, RotationDamping * Time.deltaTime);
currentRotationAngleRoll = Mathf.LerpAngle(currentRotationAngleRoll, wantedRotationAngleRoll, RotationDamping * Time.deltaTime);
var currentRotation = Quaternion.Euler(currentRotationAnglePitch, currentRotationAngleYaw, currentRotationAngleRoll);
transform.position = Target.transform.position;
transform.position -= currentRotation * Vector3.forward * Distance;
transform.LookAt(Target.transform);
transform.position += transform.up * Height;
}
Image:
I would be more certain about this answer if you explained what you were trying to do, but you should consider moving by Height in the direction of currentRotation * Vector3.up instead of transform.up. Also, consider using currentRotation * Vector3.up to set the local up direction when calling LookAt:
transform.position = Target.transform.position;
transform.position -= currentRotation * Vector3.forward * Distance;
Vector3 currentUp = currentRotation * Vector3.up;
transform.LookAt(Target.transform, currentUp);
transform.position += currentUp * Height;
Related
I developing an 6DOF game. I have some problems with the auto leveling (so the ship will always goes horizontal). The auto leveling worked, but I can't turn anymore on the x-axis more than 90 degrees. The spaceship must fly free in any direction. I hope someone can help me.
// Update is called once per frame
void Update()
{
yaw = Input.GetAxis("Mouse X") * speed;
pitch = Input.GetAxis("Mouse Y") * speed;
roll = Input.GetAxis("Roll") * speed;
float ver = 0;
float up = 0;
float hor = 0;
up = Input.GetAxis("Up");
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
ver *= speed * Time.deltaTime;
up *= speed * Time.deltaTime;
hor *= speed * Time.deltaTime;
transform.Rotate(Vector3.up, yaw);
transform.Rotate(Vector3.left, pitch);
transform.Rotate(Vector3.forward, -roll);
Vector3 cross = Vector3.Cross(Vector3.up, transform.forward);
Quaternion rotator = Quaternion.FromToRotation(transform.right, cross)
// Apply rotation
transform.rotation = Quaternion.Slerp(transform.rotation, rotator * transform.rotation, Time.deltaTime);
transform.Translate(hor, up, ver);
}
As requested in comments (note: untested):
void Update() {
var yaw = Input.GetAxis("Mouse X");
var pitch = Input.GetAxis("Mouse Y");
var roll = Input.GetAxis("Roll");
var up = Input.GetAxis("Up"); // better name is 'heave' (up/down)
var hor = Input.GetAxis("Horizontal"); // better name is 'sway' (left/right)
var ver = Input.GetAxis("Vertical"); // better name is 'surge' (forward/back)
// If no rotation input, then 'auto-correct' for roll (up direction)
if ( Mathf.Approximately(yaw + pitch + roll, 0)) {
// Find what the 'up' rotation is
var levelRotation = Quaternion.LookRotation(transform.forward, Vector3.up);
// Apply it over time (Limit the angular change with Time.deltaTime)
transform.rotation = Quaternion.RotateTowards(transform.rotation, levelRotation, Speed * Time.deltaTime);
}
// Else there IS input rotation, so deal with that instead.
else {
// Scale inputs for speed (rotation speed should probably be different from translation speed)
yaw *= Speed * Time.deltaTime;
pitch *= Speed * Time.deltaTime;
roll *= Speed * Time.deltaTime;
var rotation = Quaternion.Euler(yaw, pitch, roll);
transform.rotation *= rotation;
}
// Acale and apply translation
ver *= speed * Time.deltaTime;
up *= speed * Time.deltaTime;
hor *= speed * Time.deltaTime;
transform.Translate(hor, up, ver);
}
(A lil bit of context) the main camera is a child of the player.
Hello,
So i have this problem where when my character looks up the sky and if i press W, it is going to move up which is not I want so how do u stop this from happening, here is my code:
private void CalculateMovement() {
//Movement
float _horizontalInput = Input.GetAxis("Horizontal");
float _verticalInput = Input.GetAxis("Vertical");
Vector3 _direction = new Vector3(_horizontalInput, 0, _verticalInput);
Vector3 _velocity = _direction * _speed;
_velocity = transform.transform.TransformDirection(_velocity);
//Mouse Input and rotation
float _mouseX = Input.GetAxis("Mouse X");
float _mouseY = Input.GetAxis("Mouse Y");
Vector3 mouseInput = new Vector3(transform.eulerAngles.x -
(_mouseY * _mouseSensititvity), transform.eulerAngles.y +
(_mouseX * _mouseSensititvity), 0);
transform.eulerAngles = mouseInput;
characterController.Move(_velocity * Time.deltaTime);
}
I guess you could simply eliminate any velocity on y after converting it to a world space vector like
_velocity = transform.transform.TransformDirection(_velocity);
_velocity.y = 0f;
I am going for a RuneScape-style camera that rotates around the player using WASD. Rotating horizontally works fine but when I mix the two (as in pitching up or down) the camera rotates around the player really awkwardly, the camera might invert or will sort of gimbal I guess.
Here's my code:
public float pitch;
public float zoomSpeed = 4f;
public float minZoom = 5f;
public float maxZoom = 15f;
public Transform target;
public Vector3 offset;
public float yawSpeed = 100f;
private float currentZoom = 10f;
private float currentYaw = 0f;
private float currentPitch = 0f;
void Update()
{
currentZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
currentYaw -= Input.GetAxis("Horizontal") * yawSpeed * Time.deltaTime;
currentPitch -= Input.GetAxis("Vertical") * yawSpeed * Time.deltaTime;
Debug.Log("Yaw: " + currentYaw + " Pitch: " + currentPitch);
}
void LateUpdate()
{
transform.position = target.position - offset * currentZoom;
transform.LookAt(target.position + Vector3.up * pitch);
transform.RotateAround(target.position, Vector3.up, currentYaw);
transform.RotateAround(target.position, Vector3.forward, currentPitch);
}
Any help would be gladly appreciated!
It looks to me that you are using currentPitch, but rotating it around the forward axis? Which would create roll on the world foward axis?
If your up vector is always world up, then the yaw you have will work. But what you want to do is recalculate the right vector from your current location to your target after you apply the yaw.
void LateUpdate() {
transform.position = target.position - offset * currentZoom;
transform.LookAt(target.position + Vector3.up * pitch);
transform.RotateAround(target.position, Vector3.up, currentYaw);
transform.RotateAround(target.position, Vector3.Cross((target.position - transform.position).normalized, Vector3.up), currentPitch);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseOrbit : MonoBehaviour {
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
float minFov = 15f;
float maxFov = 90f;
float sensitivity = 10f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
void Update()
{
// Updating camera distance on every frame
distance = RayCast3.distance3;
//Setting maximum distance so the camera doesnt go too far
if (distance > 2)
{
distance = 2;
}
}
void LateUpdate()
{
if (target)
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
//distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
//distance += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
Now i'm using fov:
And it's working fine.
float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
But now i want to use the distance variable and not fov.
So i tried first the line:
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
It didn't work so i tried the line:
distance += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
But in both lines the character is stuttering and it's not zooming in out with the mouse wheel.
It's really simple. Get mouse scroll wheel speed then multiply it by some speed value. You can also multiply it by Time.deltaTime if you want. Finally use transform.Translate to move the camera with that value.
This will move in z-axis:
private float zoomSpeed = 2.0f;
void Update()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.Translate(0, 0, scroll * zoomSpeed, Space.World);
}
Or where camera is facing:
private float zoomSpeed = 5.0f;
void Update()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.position += this.transform.forward * scroll * zoomSpeed;
}
You should calculate a delta between your camera point and your target point. Normalize it and multiply it with the scrollwheel delta. Add this on your camera position. I used the camera target as angle to zoom-in.
Pseudo:
var delta = cameraTarget - cameraPosition;
delta.Normalize();
cameraPosition += delta * ScrollWheel.delta * sensitivity;
// you can even move your cameraTarget in the same direction
cameraTarget += delta * ScrollWheel.delta * sensitivity;
You can use the distance instead of calculating a delta.
I have this code and I can't figure out how to make the camera stop following my player when he jumps, in unity3d
using UnityEngine;
using System.Collections;
public class Camera2DFollow2 : MonoBehaviour {
public Transform target;
public float damping = 1;
public float lookAheadFactor = 3;
public float lookAheadReturnSpeed = 0.5f;
public float lookAheadMoveThreshold = 0.1f;
float offsetZ;
Vector3 lastTargetPosition;
Vector3 currentVelocity;
Vector3 lookAheadPos;
// Use this for initialization
void Start () {
lastTargetPosition = target.position;
offsetZ = (transform.position - target.position).z;
transform.parent = null;
}
// Update is called once per frame
void Update () {
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - lastTargetPosition).x;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget) {
lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
} else {
lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);
}
Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
transform.position = newPos;
lastTargetPosition = target.position;
}
}
Try
Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
newPos.y = transform.position.y;
transform.position = newPos;
or
Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
aheadTargetPos.y = transform.position.y;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
transform.position = newPos;
You can set a minimum threshold value for the Y value of character. When characters jump you can save that transform point y and look for the distance between that point and character position.y If the character exceeds that threshold, than your camera can follow the character. Also, you can consider using Cinemachine. It is very powerful. (https://unity.com/unity/features/editor/art-and-design/cinemachine)