how to move Camera smoothly in unity3D? - unity3d

When I want to move Camera from origin position to destination position,it looks so stiff.So if it can set move speed accordding to offset,how to do ?

There is a nice tutorial on this problem, it is normally described at the beginning of all tutorials on the unity website. Inside the Survival Shooter Tutorial there is a explanation how to make the movement of the camera to the destination position smooth while moving.
Here is the code for moving the camera. Create a script, add it to the camera and add the GameObject you want to move to, into the scripts placeholder. It will automatically save the Transform component like setup in the script. (In my case its the player of the survival shooter tutorial ):
public class CameraFollow : MonoBehaviour
{
// The position that that camera will be following.
public Transform target;
// The speed with which the camera will be following.
public float smoothing = 5f;
// The initial offset from the target.
Vector3 offset;
void Start ()
{
// Calculate the initial offset.
offset = transform.position - target.position;
}
void FixedUpdate ()
{
// Create a postion the camera is aiming for based on
// the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current
// position and it's target position.
transform.position = Vector3.Lerp (transform.position,
targetCamPos,
smoothing * Time.deltaTime);
}
}

You can use iTween plugins. And if you want your camera to move with your object ie to follow it smoothly. You can use smoothFollow script.

Have you tried using Vector3.MoveTowards? You can specify the step size to use, which should be smooth enough.
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.MoveTowards.html

public Vector3 target; //The player
public float smoothTime= 0.3f; //Smooth Time
private Vector2 velocity; //Velocity
Vector3 firstPosition;
Vector3 secondPosition;
Vector3 delta;
Vector3 ca;
tk2dCamera UICa;
bool click=false;
void Start(){
ca=transform.position;
UICa=GameObject.FindGameObjectWithTag("UI").GetComponent<tk2dCamera>();
}
void FixedUpdate ()
{
if(Input.GetMouseButtonDown(0)){
firstPosition=UICa.camera.ScreenToWorldPoint(Input.mousePosition);
ca=transform.position;
}
if(Input.GetMouseButton(0)){
secondPosition=UICa.camera.ScreenToWorldPoint(Input.mousePosition);
delta=secondPosition-firstPosition;
target=ca+delta;
}
//Set the position
if(delta!=Vector3.zero){
transform.position = new Vector3(Mathf.SmoothDamp(transform.position.x, target.x, ref velocity.x, smoothTime),Mathf.SmoothDamp( transform.position.y, target.y, ref velocity.y, smoothTime),transform.position.z);
}
}

Related

Smooth 2d rotation in unity

I'm trying to get an object to rotate smoothly on the z axis when it comes into contact with a collider. However it just seems to snap really quickly and I can't find a way to smooth this out.
I've split the rotation out into 2 scenarios - one for when the player enters the collider (which needs to be smooth), and one for when the player is already in the collider (which needs to be snappy). this is so that when the player enters the objects gravity (collider) he'll rotate smoothly before falling to the ground. However, if the player is already within the objects gravitational pull, its snappy so they can run on the ground (a circle) without any wierd floating or rolling occuring.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravitationalBody : MonoBehaviour
{
[Range(0, 0.3f)] [SerializeField] private float m_OrientationSmoothing = 0.3f; // How much to smooth out the movement
void OnTriggerEnter2D(Collider2D obj) {
// Define direction of gravity
Vector3 gravityDirection = transform.position - obj.transform.position;
// apply gravity to all objects within range
obj.GetComponent<Rigidbody2D>().AddForce(gravityDirection);
orientObjects(obj, gravityDirection, true);
}
void OnTriggerStay2D(Collider2D obj)
{
// Define direction of gravity
Vector3 gravityDirection = transform.position - obj.transform.position;
// apply gravity to all objects within range
obj.GetComponent<Rigidbody2D>().AddForce(gravityDirection);
orientObjects(obj, gravityDirection, false);
}
// Control players Z axis rotation to point toward center of planets
void orientObjects(Collider2D obj, Vector3 gravityDirection, bool lerp)
{
float orientation = 90 + Mathf.Atan2(gravityDirection.y, gravityDirection.x) * Mathf.Rad2Deg;
if (lerp)
{
Quaternion q = Quaternion.AngleAxis(orientation, Vector3.forward);
obj.transform.rotation = Quaternion.Lerp(obj.transform.rotation, q, m_OrientationSmoothing * Time.deltaTime);
}
else obj.transform.rotation = Quaternion.Euler(0f, 0f, orientation);
}
}
I don't know if it will help but if you want a smooth rotation look at this code, I'm sure you can find inspiration here:
[SerializeField] float _degreesPerSecond = 30f;
[SerializeField] Vector3 _axis = Vector3.forward;
void Update ()
{
transform.Rotate( _axis.normalized * _degreesPerSecond * Time.deltaTime );
}

Trying to rotate object when key pressed

I'm attempting to make the ability to slide in an FPS game I'm working on and I've tried rotating the player object but when tested, it does nothing. Pressing the slide key I have assigned does nothing but rotate the player object in the Y and Z axis (I'm trying to rotate it on the X axis)
Here's the code I'm using (only relevant parts have been kept in):
public class SlideMovementScript : MonoBehaviour
{
public Rigidbody rig;
public CapsuleCollider capcol;
public GameObject player;
public float originalHeight;
public float reducedHeight;
public float slideSpeed = 10f;
// Start is called before the first frame update
void Start()
{
capcol = GetComponent<CapsuleCollider>();
rig = GetComponent<Rigidbody>();
originalHeight = capcol.height;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftControl) && Input.GetKey(KeyCode.W))
Sliding();
else if (Input.GetKeyUp(KeyCode.LeftControl))
GoUp();
}
private void Sliding()
{
capcol.height = reducedHeight;
player.transform.localRotation = Quaternion.Euler(1.599f, 0f, 0f);
rig.AddForce(transform.forward * slideSpeed, ForceMode.VelocityChange);
}
private void GoUp()
{
GetComponent<CapsuleCollider>().height = originalHeight;
player.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
}
}
I've gotten ZERO error messages from this but I haven't managed to find the problem
Another problem I've also had is when you start sliding, it just instantly rotates the Player object to X 1.599 Y 0 Z 0. I want it to rotate to where the player is facing but I haven't found a way to do it, even using transform.foward or other references won't work.
The Explanation (skip to script if you want) (read notes)
I reccomend using Quaternion's methods. You can use Quaternion.Slerp which can smooth out the rotation towards the player.
Slerp means spherical linear interpolation
Basically, Slerp is a 3 argumented method. the first argument is the current rotation, the second argument is the target rotation, and the third is at which point between those two you want to output.
More simply, Lerp is linear interpolation. Lerps arguments are the same, except it handles floats and not Quaternions. If you Mathf.Lerp(0,1,1); you will get 1, because 1 is all the ay to the second argument. If you do Mathf(0, 3, 0.5); you will get 1.5 because 1.5 is halfway (0.5) between them both.
You can imagine slerp like that, if you give rotation (90, 0, 0) and alerp it to (0, 0, 0) by 0.5 you will get (45, 0, 0).
On this example, I slerp from the current rotation, to the target rotation, by delta time. This way, as time goes on, it slightly increases the current rotation to be the target.
I can create the target as 1.599 rotated on the x.
To make this relative Make a parent game object to the player containing the mesh and rigidbody and maybe the collider (depends if you want the collider upright). I will call this "slidePlayer" slidePlayer must be a child of the player gameObject
Notes
Make sure to have slidePlayer to a gameObject that is a child of player, but must have the player mesh or sliding won't be visible.
Change rot to the target rotation.
Change speed to speed to rotate to the target rotation.
Here is your script I changed:
public class SlideMovementScript : MonoBehaviour
{
public Rigidbody rig;
public CapsuleCollider capcol;
public GameObject player;
public GameObject slidePlayer;
public float originalHeight;
public float reducedHeight;
public float slideSpeed = 10f;
// Start is called before the first frame update
void Start()
{
capcol = GetComponent<CapsuleCollider>();
rig = GetComponent<Rigidbody>();
originalHeight = capcol.height;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftControl) && Input.GetKey(KeyCode.W))
Sliding();
else if (Input.GetKeyUp(KeyCode.LeftControl))
GoUp();
}
private void Sliding()
{
capcol.height = reducedHeight;
Quaternion rot = Quaternion.Euler(1.599f, 0f, 0f);
float speed = 1f;
Quaternion curRot = slidePlayer.transform.localRotation;
slidePlayer.transform.localRotation = Quaternion.Slerp(curRot, rot, Time.deltaTime * speed);
rig.AddForce(transform.forward * slideSpeed, ForceMode.VelocityChange);
}
private void GoUp()
{
GetComponent<CapsuleCollider>().height = originalHeight;
Quaternion rot = Quaternion.Euler(0f, 0f, 0f);
float speed = 3f;
Quaternion curRot = slidePlayer.transform.localRotation;
slidePlayer.transform.localRotation = Quaternion.Slerp(curRot, rot, Time.deltaTime * speed);
}
}
Let me know if there are any problems in coments. Thanks.

Unity, How to make the friction work with movement

I have a 3d world with a simple platform with a cube representing the player. When I rotate the platform the cube glides and perform as you expect when increase and decrease the friction in the physics material.
I want the cube to glide after the input for example forward is terminated. It does not. I tried to update the position with rigidbody.position and update it. I quickly understood that it would not work with the physics engine.
Now I have the following code. It does not work as expected anyway. I would like to have some pointers to solve this.
public class Player1 : MonoBehaviour
{
private float speed = 10f;
private Vector3 direction;
private Vector3 velocity;
private float vertical;
private float horizontal;
Rigidbody playerRigidBody;
// Start is called before the first frame update
void Start()
{
playerRigidBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
vertical = Input.GetAxisRaw("Vertical");
horizontal = Input.GetAxisRaw("Horizontal");
direction = new Vector3(horizontal, 0, vertical);
}
private void FixedUpdate()
{
velocity = direction.normalized * speed * Time.fixedDeltaTime;
playerRigidBody.MovePosition(transform.position + velocity);
}
}
Use playerRigidBody.AddForce(Vector3 direction, ForceMode forceMode) to move your player.
If you don't want your player to move at a demential speed use playerRigidBody.velocity = Vector3.Clamp(Vector3 vec3, float minValue, float maxValue);
Then play with different variables to get the result you want !

Unity:Stop camera follow player?

I am following Survival Shooter unity tutorial and in the tutorial the down given code is used to make the camera follow the player.The code is working but how can i alter it so that it stops following the player at a given X and Y point.
code
public Transform target; // The position that that camera will be following.
public float smoothing = 5f; // The speed with which the camera will be following.
Vector3 offset; // The initial offset from the target.
void Start ()
{
// Calculate the initial offset.
offset = transform.position - target.position;
}
void FixedUpdate ()
{
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current position and it's target position.
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
Simply stop updating his position:
private bool followPlayer = true;
void FixedUpdate ()
{
if(followPlayer){
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current position and it's target position.
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
Change the value of followPlayer to false and it will stop following
To determine if the player is at given point, you need to check distance between the player and the point, e.g:
public Transform target; // player position
public Transform stopingPoint; // stopping point position
public double tolerance; // the "radius" of stopping point
private bool followPlayer = true;
...
void FixedUpdate ()
{
if(!followPlayer)
return;
followPlayer = Vector3.Distance(target.position, stopingPoint.position) <= tolerance;
...

Rotate camera based on mouse postitions around a object Unity3D

Okay, So I have come this far:
public class CameraScript : MonoBehaviour {
public void RotateCamera()
{
float x = 5 * Input.GetAxis("Mouse X");
float y = 5 * -Input.GetAxis("Mouse Y");
Camera.mainCamera.transform.parent.transform.Rotate (y,x,0);
}
}
My camera has a parent which I rotate based on my mouse position. The only problem is that I can only swipe with the mouse to rotate the object. How can I rotate the object which is my camera is attached to based on my mouse position if I just click next to the object. Thanks in advance!
The value will be in the range -1...1 for keyboard and joystick input.
If the axis is setup to be delta mouse movement, the mouse delta is
multiplied by the axis sensitivity and the range is not -1...1. Unity Document
Note: This link is usefull please check it.
So you need to change your code like this.
public void RotateCamera()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); // Gets mouse position to Unity World coordinate system
Camera.mainCamera.transform.parent.transform.Rotate (mousePosition);
}
if there are problem you can do like this
public void RotateCamera()
{
Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(position); // Gets mouse position to Unity World coordinate system
Camera.mainCamera.transform.parent.transform.Rotate (mousePosition);
}
one more option is rotateTowards.
public float speed=10; //any value > 0
public void RotateCamera()
{
Vector3 targetDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.mainCamera.transform.parent.transform.position;
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
Camera.mainCamera.transform.parent.transform.rotation = Quaternion.LookRotation(newDir);
}
Maybe some syntax errors, i don't check them.