I have an object that rotates on the y axis with the "Mouse X" input and a video camera that through a slerp quaternion should follow the rotation of the object.
Unfortunately, when I rotate the object with fast mouse movements, the rotation of the slerp camera stops as if it hits a wall and does not follow that of the object.
Without quaternion slerp it works following the object but I need to do it with the interpolation.
rotation object (in LateUpdate:
float rot = Input.GetAxisRaw("Mouse X") * speedrotation;
transform.Rotate(0, rot, 0);
rotation camera (in LateUpdate):
var rot = Quaternion.Slerp(camera.transform.rotation, object.rotation, speed_rot * Time.deltaTime);
camera.transform.position = object.transform.position + rot;
camera.transform.LookAt(object.transform.position);
Try EulerAngles instead! That is better option for make camera controller!
Simple Code:
Vector3 currentCameraEulers;
float rotationDPI = 7f; // I didn't remeber how to call better this thing
void Update(){
float cameraX = Input.GetAxisRaw("Mouse X") * rotationDPI;
if(cameraX != 0f){
currentCameraEulers = new Vector3(0, cameraX, 0);
transform.eulerAngles += currentCameraEulers;
}
}
That should work completely fine.
Related
I create a game, like minigolf/pool. I want to have a camera which follow player.
Position is normally ok, I get the ball direction and I lerp.
Rotation is almost ok. Currently, rotation by Y axis is ok, but camera look straigth forward, and don't look down to the player :
I already try many thing , quaternion angleToaxis, quarternion lookat ... but doesn't look good, the camera go look away ...
Here my code
namespace CameraManagerNameSpace
{
public class CameraManager : MonoBehaviour
{
public float cameraHeight=13f;
public PlayerNameSpace.Player playerToFollow;
public float followSpeed = 3f;
public float rotationSpeed = 1f;
float distance;
Vector3 position;
Vector3 newPos;
Quaternion rotation;
Quaternion newRot;
Vector3 playerPrevPos, playerMoveDir;
bool firstMoveDone=false;
void Start()
{
playerPrevPos = playerToFollow.player_transform.position;
distance = Vector3.Distance(transform.position,playerToFollow.player_transform.position);
}
void FixedUpdate()
{
if(Vector3.Distance(playerToFollow.player_transform.position ,playerPrevPos)>0.5f || firstMoveDone)
{
playerMoveDir = playerToFollow.player_transform.position - playerPrevPos;
firstMoveDone = true;
}
else
{
playerMoveDir = new Vector3(0,0,0);
}
if (playerMoveDir != Vector3.zero)
{
playerMoveDir.Normalize();
newPos = playerToFollow.player_transform.position - playerMoveDir * distance;
newRot =Quaternion.LookRotation(playerMoveDir,Vector3.up);
position = Vector3.Lerp(transform.position, new Vector3(newPos.x,newPos.y+cameraHeight,newPos.z), followSpeed * Time.deltaTime);
rotation = Quaternion.Lerp(transform.rotation, newRot, rotationSpeed * Time.deltaTime);
transform.position = position;
transform.rotation = rotation;
playerPrevPos = playerToFollow.player_transform.position;
}
}
}
}
Also, I don't know why, but after the balls stop the camera continue to do some movement very jerky, jolting, halting.
Well in
newRot = Quaternion.LookRotation(playerMoveDir,Vector3.up);
you are saying the camera to look in the same direction the player is moving ... not to look at the player. This would work if you wouldn't give the camera an extra position offset in the Y axis.
You might want to rather try
// vector pointing from the camera towards the player
var targetDirection = playerToFollow.player_transform.position - transform.position;
newRot = Quaternion.LookRotation(targetDirection, Vector3.up);
You should also rather use Update since FixedUpdate is only used for physics related stuff (also see Update & FixedUpdate)
i have a cube that rolls with the arrow keys or control pad
up goes up, left turns left, and right turns right, so only the up makes it roll
im trying to get a camera to follow but not really getting anywhere
i found this script cant rember where that im trying to alter
but as i roll the cube forward the camera spins
simple video showing movment
https://imgur.com/a/BfoR1VF
any pointers in the right direction sorry about the pun would be good
simple lookat script
public Transform player;
void Start()
{
}
void Update()
{
Vector3 targetPostion = new Vector3(player.transform.position.x, transform.position.y,player.transform.position.z);
transform.LookAt(targetPostion);
}
and the follow script
// The target we are following
public Transform target;
// The distance in the x-z plane to the target
//So this would be your offset
public float distance = 10.0f;
// 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()
{
// Early out if we don't have a target
if (!target) return;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float 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 = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
// Always look at the target
Vector3 thetargetPostition = new Vector3(0, target.position.y,0);
transform.LookAt(target.position);
//transform.LookAt(thetargetPostition);
I have a player at the bottom of the screen, and an enemy at the top of the screen. I want them both to continuously rotate towards each other. Here is my code for both:
void Update ()
{
Vector3 diff = target.position - transform.position;
diff.Normalize();
float zRotation = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
Vector3 lookDirection = new Vector3(0, 0, zRotation - 90);
transform.rotation = Quaternion.Euler(lookDirection);
}
Given this state, the player behaves as expected, but the enemy always rotates away from the player, meaning that he looks up instead of down at where the player is.
If I switched the diff vector like this:
Vector3 diff = transform.position - target.position;
Then it's the player that looks away from the enemy.
Both sprites have 0 values for rotation and scale in the editor.
What is wrong here?
Your solution works fine for me: http://imgur.com/a/spi3O
Could it be that the "up" for the Enemy is the "down" for the player. Meaning one of your sprites is drawn upside down?
You can try the following, but it yields the same result:
var dir = target.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
If you're going to use the same method of calculating the rotation, you need to assure that your sprites have the same world-relative native rotation, or fix that.
I have a 3d city model. I want to walk throught walls/steets. There are some advices for this.
Ex1, ex2, ex3
Generally, they said that you should add a collider to your model. I did it. But I don't know next step. Should I move my camera or player gameobject? Should I handle click movement key in code?
I attach below code to my camera. It works for me. I take this code from this tutorial.
public float speed = 10.0f;
public float rotationSpeed = 100.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float translation = Input.GetAxis ("Vertical") * speed;
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
}
I try to make a 3rd person camera, which follows my player and the camera should rotate, but not the player, if I use the right analog Stick of my controller. I followed this tutorial
My code:
void adjustCameraToPlayer()
{
Quaternion rotation = Quaternion.identity;
if (Input.GetAxis("RightStickX") != 0f)
{
float horizontal = Input.GetAxis("RightStickX") / 100f;
transform.Rotate(0, horizontal, 0);
float desiredAngle = transform.eulerAngles.y;
rotation = Quaternion.Euler(0, desiredAngle, 0);
}
transform.position = player.transform.position-(rotation * offset);
transform.LookAt(player.transform);
}
My problem is that the camera rotates way too fast, I tried to change the dividend of the horizontal value but it did not help.
That's why you always should incorporate deltaTime into transform operations that happen every frame. That way you aren't rotating it at magnitude every single frame, but instead over time. Also you should incorporate a speed variable which can be manipulated in real time, so you can tweak it just how you want:
public float speed = 5f;
void adjustCameraToPlayer()
{
Quaternion rotation = Quaternion.identity;
if (Input.GetAxis("RightStickX") != 0f)
{
float horizontal = Input.GetAxis("RightStickX");
transform.Rotate(Vector3.up * horizontal * speed * Time.deltaTime);
float desiredAngle = transform.eulerAngles.y;
rotation = Quaternion.Euler(0, desiredAngle, 0);
}
transform.position = player.transform.position-(rotation * offset);
transform.LookAt(player.transform);
}