Hello, I want to make a script to rotate the camera smooth along a vehicle. Also, the movement should be elastic and the camera follow the edges of the vehicle and keep the same distance between the camera and each point of that vehicle.
Look how camera is moving along that boat and when it reaches an edge it goes backwards to keep the distance from that boat. How can I make it? Thank you!
Assuming you are familiar with update, fixedupdate, lateupdate, etc. and transforms, one way to tackle this is to put a follow script on the camera that sets the camera position to be the vehicle’s position + a defined offset, like 10 units behind and above. you may have to play with putting the position setting code for the camera in lateupdate.
That alone might be choppy, I’d look into caching a vector3 in the camera follow script, and use Vector3.MoveTowards to constantly update the camera’s new target position. I’ve found movetowards a bit more smooth than just using Vector3.Lerp
Do i have to put multiple target points on each vehicle ( on the edges and in the middle of it ) ? To follow each one depending on moving direction? There are other ways to detect the distance between camera and the vehicle edges to move the camera further or closer depending on the edges?
Do you know something similar in assets store?
Thanks!
you should only need one target point, the best one would be the center of the car model. but not all 3d models you get into unity are centered, so check, and if not calculate an offset and use it from the model transform zero point.
your video link spins too long, I can’t view it, but moving closer and further away could be accomplished by altering your camera to vehicle offset.
I hope you understand now exactly what I want to make. If I will use a single target point, I don’t know how to move the camera along the vehicle and move further when It reaches the edge of the vehicle. How to detect the edge if I am using a single centered point?
Obviously that will be larger than stored distance because I want the camera to go futher on edges like in that video. I don’t think that is the solution.
But how to detect the edges?
I’m fairly certain that this IS the solution. And rather than “THINK” you should TEST it.
Move both camera and control point. Constrain control point to the ara. Pull the camera towards the control point to maintain previous distance. And you’ll have the effect. Sans inertia.
Using EXACT algorithm I described, about which you said “I don’t think this is a solution”
Remember distance to camera.
When user swipes, move both target and the camera.
Pull target back into allowed area. In case of a ship, restrict it to a single line.
Pull camera back to the target to restore the memorized distance.
Make camera look at the target.
That’s literally it.
I’ve already explained the algorithm step-by-step to you. Will you try to implement it yourself, or are you waiting for a complete solution to be posted?
There is no explanation in your post, just some logical steps to follow that many would have thought of .
“Pull target back into allowed area.” I really don’t know how to make an allowed area and how to slide the target into it. This is not helpful I’m waiting for someone who can explain to me what functions to use at least. Thanks anyway
Still waiting for someone to explain how to make it. A small example of the script, how to slide the object along vehicle and how to restrict the slider into an area.
Do you have programming experience?
Can you write a script that moves the camera when the user presses and drags mouse?
What have you tried to do so far?
Where’s your code?
At the moment it literally sounds like you’re asking someone to develop solution for you for free. Doing this sort of thing is not interesting, because in the end you’re doing yourself disfavor.
It is more interesting to teach how to do it, but so far you have not shown anything.
I probably asked the wrong question. How can I submit a script with my attempts since I don’t know what functions to use to create a slider to move target zones?
I’m not waiting for anyone to create my complete script for free as you say, but it wasn’t hard for you to give me a hint of the features you used above. From here on out, I can handle myself. If you are not willing to help, leave it to others. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
{
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offSet;
public float rotateSpeed;
private float speedMod = 10.0f;
private Vector3 point;//the coord to the point where the camera looks at
private void FixedUpdate()
{
if (Input.GetMouseButton(1) == true)
{
point = target.transform.position;
transform.LookAt(point);
transform.RotateAround(point, new Vector3(0.0f, 1.0f, 0.0f), 10 * Time.deltaTime * speedMod);
}
else
{
Vector3 desiredPosition = target.position + offSet;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
transform.LookAt(target);
}
}
}
Closed.
First wrong section. Please read forum descriptions.
Second, you have been provided quite complete and useful information on how to implement the solution you are looking for. As explained rather politely, this forum is not a script writing service. A great description of you need to do was provided very clearly. ( @neginfinity did not give you a “hint”, they described exactly what you need to do) If it isn’t enough, please go check out the learn section on the basics to get the knowledge you need to complete. If you still have specific questions, please post in the proper forum for that help.