Please suggest me the answer here is my code.
Rotate Animation rotate = new Rotate Animation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setInterpolator(new LinearInterpolator());
rotate.setRepeatMode(Animation.INFINITE);
rotate.computeDurationHint();
rotate.setRepeatCount(Animation.INFINITE);
rotate.setDuration(durationsecs);
rotate.setStartOffset(-1);
rotate.setFillAfter(true);
mIV_BloodPumpSpeed.startAnimation(rotate);
private RotateAnimation setRotatedAnimation(Context context, float pivotX,
float pivotY, int duration) {
RotateAnimation rotated = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, pivotX, Animation.RELATIVE_TO_SELF,
pivotY);
rotated.setInterpolator(context, android.R.anim.cycle_interpolator);
rotated.setRepeatCount(Animation.INFINITE);
rotated.setRepeatMode(Animation.RESTART);
rotated.setDuration(duration);
return rotated;
}
Related
I made the code which can follow the clone player in unity. But It worked only the first clone player. If join room second or later, It can't find clone player and doesn't work. How can I fix that ? Thanks for anything your helps.
follow.cs:
private void Awake()
{
mainCam = this;
}
void Start()
{
height = Camera.main.orthographicSize;
width = height * Screen.width / Screen.height;
}
void LateUpdate()
{
if (player == null)
{
player = GameObject.Find("Player(Clone)");
}
else
{
//transform.position = new Vector3(target.position.x, target.position.y, -10f);
transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * speed);
float lx = size.x * 0.5f - width;
float clampX = Mathf.Clamp(transform.position.x, -lx + center.x, lx + center.x);
float ly = size.y * 0.5f - height;
float clampY = Mathf.Clamp(transform.position.y, -ly + center.y, ly + center.y);
transform.position = new Vector3(clampX, clampY, -10f);
}
}
player spawn script:
public void Spawn()
{
GameObject PI = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity);
Follow.mainCam.target = PI.transform;
Debug.Log("I'm in the room.");
}
I guess that player stay null, maybe when you are trying to GameObject.Find() by his name, that method returns null.
Maybe you can try it with a tag or use another name?
The simplest solution is to make the camera the child of the player object and then change it's position accordingly through the script
You could also potentially use CineMachine which has some built-in follow methods
In order to change the parent of an object in the hierarchy through code you basically need to do something like this
public Transform camera
public Transform player
camera.parent = player;
and you can then set the camera position like this
camera.position = new Vector3(0, 0, -10);
Since the camera is the child of the player 0, 0, -10 basically means that the camera will be -10 in the Z axis away from the player (which should be behind the player). Whenever the player moves, because the camera is now attached as a child, it will follow the player.
I have a script that makes my camera rotate around a target when right click is pressed. Currently, when the right click is released, the rotation comes to a full stop, which is fully expected behavior. I want it to keep rotating for a little while after the mouse button is up, before it comes to a full stop, like if it was decelerating. Here is the code that handles the rotation around the given target:
if (!target)
return;
if (Input.GetMouseButtonDown(1))
previousPosition = cam.ScreenToViewportPoint(Input.mousePosition);
if (Input.GetMouseButton(1))
{
Vector3 nextDirection = previousPosition - cam.ScreenToViewportPoint(Input.mousePosition);
direction = Vector3.SmoothDamp(direction, nextDirection, ref smoothVelocity, smoothTime);
cam.transform.position = target.position;
cam.transform.Rotate(new Vector3(1, 0, 0), direction.y * 180);
cam.transform.Rotate(new Vector3(0, 1, 0), -direction.x * 180, Space.World);
cam.transform.Translate(new Vector3(0, 0, -distanceFromSphere));
previousPosition = cam.ScreenToViewportPoint(Input.mousePosition);
}
Here are the needed references:
public Camera cam;
public Vector3 previousPosition;
public Transform target;
public float distanceFromSphere = 100;
public Vector3 direction;
public Vector3 smoothVelocity = Vector3.zero;
public float smoothTime = 3f;
Is there a way to make the camera rotation be accelerated when the mouse button is down, and when the mouse is up, to be decelerated?
What you need is inertia, get the Rotate and Translate calls outside the if statement, and at the end multiply the direction with 0.98 or 0.99 depending on how much intertia you need.
public float inertia = 0.98f;
direction *= inertia;
Chris Ch's solution works fine, but I had already done something different, here is my solution to the original question:
void UpdateWorldRotation()
{
if (!target)
return;
if (Input.GetMouseButtonDown(1))
previousPosition = cam.ScreenToViewportPoint(Input.mousePosition);
Vector3 currentSmoothVelocity = new Vector3();
if (Input.GetMouseButton(1))
{
nextDirection = previousPosition - cam.ScreenToViewportPoint(Input.mousePosition);
currentSmoothVelocity = smoothVelocity;
}
if (Input.GetMouseButtonUp(1))
{
StartCoroutine(SmoothStop());
currentSmoothVelocity = smoothVelocity / 2;
}
direction = Vector3.SmoothDamp(direction, nextDirection, ref currentSmoothVelocity, smoothTime, 100, 10f * Time.deltaTime);
cam.transform.position = target.position;
cam.transform.Rotate(new Vector3(1, 0, 0), direction.y * 180);
cam.transform.Rotate(new Vector3(0, 1, 0), -direction.x * 180, Space.World);
cam.transform.Translate(new Vector3(0, 0, -distanceFromSphere));
previousPosition = cam.ScreenToViewportPoint(Input.mousePosition);
}
IEnumerator SmoothStop()
{
yield return new WaitForSeconds(smoothStopTime);
nextDirection = Vector3.zero;
}
This way, the sphere keeps rotating for a specific amount of time with a slower velocity before it comes to a full stop.
Am developing a follow target camera, it's working fine but when the vehicle(Target) is on slope, the camera is not rotating to show the full vehicle.
void LateUpdate()
{
if (car1.controlled && Camera.main != null)
{
float speedFactor = Mathf.Clamp01(target.root.GetComponent<Rigidbody>().velocity.magnitude / 20.0f);
if (speedFactor < 0.01f)
speedFactor = 0.01f;
Camera.main.fieldOfView = Mathf.Lerp(40, 65, speedFactor);
float currentDistance = Mathf.Lerp(13.5f, 8.5f, speedFactor);
currentVelocity = currentVelocity.normalized;
Vector3 newTargetPosition = target.position + Vector3.up * height;
Vector3 newPosition = newTargetPosition - ((currentVelocity * currentDistance));
newPosition.y = newTargetPosition.y;
Vector3 targetDirection = newPosition - newTargetPosition;
if (Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
newPosition = hit.point;
Camera.main.transform.position = newPosition;
Camera.main.transform.LookAt(newTargetPosition);
}
LookAt is going to set the camera rotation to be looking straight at the newTargetPosition. It doesn't actually moves the camera up, it simply rotates it to look at the vehicule. You should modify your code so the camera moves up when the vehicule goes down a slope and then make it look at the vehicule.
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 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);
}