Camera move&rotation along vehicle

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 at this example: Error - USAupload | Upload Files for free

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!

Add comment

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?

Please check it again on YT:
https://www.youtube.com/watch?v=co50Jg7PiCA

Make a sliding target point.

When moving the camera try to move both point and the camera, then restrict the point to the allowed region.

Basically…

  1. Store distance from camera to target point.
  2. Move the target point and the camera.
  3. Pull the target point into the allowed region.
  4. Measure distance from camera to target. If it is larger than stored distance, pull camera closer, till the old distance is restored.
2 Likes

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.

1 Like

Could you show me a small example?
Also, If you know something like this in assets store would be perfect.
Thank you!

The algorthm described looks like this in action:

https://www.youtube.com/watch?v=8aX-XeejJkc

Which is a decent starting point. From there you’d need to add inertia and make it tweak a bit so it favors turning over sliding.

At the moment I won’t be making any assets.

2 Likes

This is exactly what I want. How did you make this movement?
Thanks

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?

1 Like

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 :slight_smile: 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.

But that’s the solution. The algorithm.

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.

So, what have you tried, and where’s your code?

1 Like

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);
        }
    }
}

Someone?

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.

3 Likes