How to make camera follow a car? - unityscript

I'm developing car racing game in unity. I put car moving script in bot car and main camera. Camera is following car when moving forward but when car collides with other object and car diverts its position or when I turn car turns also camera but earlier than car so car goes out of scope. I tried to put character controller, tried putting camera in hierarchy still it's not following car smoothly even with smooth follow script. So tell me either way to make camera follow car as we see in other car racing game.

What I recommend for this type of game(where the camera is set to follow a certain object, from place A to place B) is to parent an empty gameobject where you want the camera to end up to the car and another empty gameobject parented to the car for where you want the camera to be looking at.
After this, use the basic camera follow and camera lookat scripts that come with the standar assets and play around with the smoothing factors to suit your game. You have to make sure you imported the scripts package for your project.
OR, if the package is imported, select your camera, go to the component menu, and under "Camera Control" select the "Smooth Follow" script.
If you need the camera to dynamically change position depending on (for example) where the car is on the road, I would consider the same solution but using iTween to alter both gameobjects positions along a predefined path.
More about:
smoothfollow: http://wiki.unity3d.com/index.php/SmoothFollow2
lookat: http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

The setting game camera is really challenging especially when it's come to vehicle camera control because camera movement speed depends upon vehicle speed and it's a little bit difficult to control the camera for a vehicle using the C# script.
I highly recommend you should use unity build in ' Cinemachine Package
' you can install it for free. if you want to learn about cinemachine
and go ahead and check out this video
https://www.youtube.com/watchv=X33t13gOBFw&list=PLOBPWyvPXShNN01VQ1LLPqfCtqeRo95Z1&index=23
and by the way, in this video they are setting cinemachine camera for vehicle

You can
Set the camera as a children of your car (the object you want to
follow)
Write this code taken from here
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start ()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate ()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
}
}

One way to do it would be to just set the camera as a child of the car. This will essentially lock the camera and give it the same offsets in position and rotation between the car and the camera. You could also try Cinemachine, which is a package that has a lot of useful features that will basically do the same thing while also giving you some more features that you may want. There's quite a bit of documentation on it if you really want to get into it:
https://unity.com/unity/features/editor/art-and-design/cinemachine

Just add the camera to the car as a child object

Related

How to prevent object from flying towards camera when using Ray-cast in unity?

I got an unity project where object is meant to be moving with mouse cursor. The object moves fine, but when the object is still, it starts to float towards the camera that Ray-casts. I would like that the object doesn't float towards camera.
I couldn't find any reason for objects behavior.
I'm quite a novice who's had issues with gameobjects following cursors myself.
But could you try freezing the Y position in the Rigidbody and ensure gravity is unchecked?
More of a workaround if it even works.
Also I believe it's better practice to use rigidbody.position than transform.position. Try this:
public Rigidbody rigidbody;
void Update()
{
rigidbody.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
}
What is happening here is when you send a ray and you get a hit result using hit.point it gives the exact location of the tris on the object it is hitting. Lets say the object is centered in the world (Vector3.zero). Ray is hitting a tris that is different than the position zero. Just because the object is in x:0y:0z:0 doesnt mean all the tris on the object is located at the same coordinates.
You get the coordinates of the hit.point, they are probably closer than the object center position, therefore the object is updating its center position to the hit.point location. And it does that every frame moving the object closer to camera.
You might want to try sending a ray from screen to world position. You can use custom vector length which would help to keep the object in the same depth you like.
Unity Docs Screen to World

Unity 3D - Dynamically 'slicing' background image

I'm fairly new to Unity and not quite sure how to handle this problem.
I have two images, one has clouds on it (day) and one has stars on it (night). What I want to do is show the clouds in the top of my scene and the stars on the bottom. There is a ground object in the middle of the screen where the player will be walking on, this should be the dividing line between the two images. The ground however is not one straight line but can have height differences.
The "solution" I came up with is to use the ground object(s) to slice the images so it kinda serves as a dividing line. But not sure if this is even possible. Maybe I could do something with 2 different camera's or mask the images somehow.. (Just throwing my own thoughts in here as well) I'll be fumbling around with these things in between and try to keep the topic up to date with what I tried.
I put in an attachment to (hopefully) make it more clear.
Greets,
Lukie
attachment: https://imgur.com/a/lblJXPi
The first solution to my mind was preparing a tileset. If you're not going to design a different section every time. So if you're not going to do a computer design. You can do it yourself by adjusting the size.
You can also dynamically generate the stars with the -y axis of the ground object and the clouds with the + y-axis. You can use instantiate function
Example:
public GameObject clouds;
public GameObject stars;
// Start is called before the first frame update
private void Awake()
{
Instantiate(clouds, new Vector3(this.transform.position.x, this.transform.position.y + 3.625f, this.transform.position.z), Quaternion.identity);
Instantiate(stars, new Vector3(this.transform.position.x, this.transform.position.y - 3.625f, this.transform.position.z), Quaternion.identity);
}
Of course, the background design that you will use here must be sustainable.
Dynamic Background

Unity 3D Kudan "Place Markerless Object" whenever mesh leaves screen?

I´m kinda new to Unity 3D and C#. Also i´m not exactly sure how Kudans arbitrary tracking solution works in detail. I´m currently using the Unity Kudan SDK to build a VR positional tracking solution, atleast i will try it. Now my plan is:
Whenever the mesh is leaving the screen, i want to freeze it´s position and find new feature points (the "place markerless object" button is doing this: Find new feature point and place a mesh).
Once it found new feature points (which should be a matter of milliseconds) it defreezes the position of the mesh and use the new feature points to further alter it´s position.
The "find new feature point" idea is necessary because whenever the mesh and the old feature points are leaving the screen, tracking will get very inaccurate.
I already tried this in SampleApp.cs:
bool VRSignal;
public void Start()
{
//Get Bools from "KudanTracker"
GameObject g = GameObject.Find("Kudan Camera");
KudanTracker bScript = g.GetComponent<KudanTracker>();
bool VRSignal = bScript.ArbiTrackIsTracking();
}
public void Update()
{
if(VRSignal == false)
{
// from the floor placer.
Vector3 floorPosition; // The current position in 3D space of the floor
Quaternion floorOrientation; // The current orientation of the floor in 3D space, relative to the device
_kudanTracker.FloorPlaceGetPose(out floorPosition, out floorOrientation); // Gets the position and orientation of the floor and assigns the referenced Vector3 and Quaternion those values
_kudanTracker.ArbiTrackStart(floorPosition, floorOrientation); // Starts markerless tracking based upon the given floor position and orientations
}
}
But now it won´t track properly track anymore, also i´m pretty sure ArbiTrackIsTracking() won´t be the solution for that because it won´t lose tracking when the mesh left the screen.
Do you have any idea to solve this problem?
if I understand well, you want change position of 3d model with trigger as soon as your 3d model disappear of the screen.
And you are right, ArbiTrackIsTracking() remain true even if the 3d model go out the screen because if you move again your screen around the 3d model, the 3d model will be always tracked.
But if you move too much your smartphone logically the tracking stops.
My idea for your issue is to get the position of your 3d model markerless transform driver, because the 3d model moves in function the position and orientation of your smartphone to track.
So you can take the position of the moment where your 3d model begin to be tracked.
Then you give a value who will correspond to the difference of first and last position saved.
And if this difference is get, you stop the tracking with arbitrack stop.
if you have another question you can ask on my twitter account #ModeLolito I could answer faster.
And you can watch my youtube channel to see my works on kudan
https://www.youtube.com/user/modelisationLolito

Unity 3D : UI Image in the direction of a game object

I'll explain my issue (My English is a little lame sorry).
I have a Player ( Camera) who is able to move in a Unity Scene. In this scene there is some GameObjects. Those GameObjects May be too far for the camera to see. What I want to achieve is to have an UI Image that appear in the direction of every objects. Even if the objects are too far.
Example : The player stand at a position. In front of him there is an object that is far. I want to have a GUI on screen who says "there is an object at 200 m ". If there is an object behind him I dont want anything to appear. But if he turns back, it will appear cause the player is in the direction of the said GameObject.
I really hope I made myself clear. Please tell me if you need any further explanations. Thank You community !
Answering to an old question of myself :
Here's what I did :
if (markIsOp) //Check if it's worth calculating
{
//Calculs the Viewport Position of the object
Vector3 ViewportPosition = Camera.main.WorldToViewportPoint (transform.position);
//Calculs it's Unity Canvas position (O.5f because UI anchor is middle)
Vector2 WorldObject_ScreenPosition = new Vector2 (
((ViewportPosition.x * mainCanvasRect.sizeDelta.x) - (mainCanvasRect.sizeDelta.x * 0.5f)),
((ViewportPosition.y * mainCanvasRect.sizeDelta.y) - (mainCanvasRect.sizeDelta.y * 0.5f)));
//Making sure it's forward (markRect is my UI Element's RectTransform)
if (ViewportPosition.z > 0)
markRect.anchoredPosition = WorldObject_ScreenPosition;
}
Thank you for your responses.
Have a nice one
You could as well use Camera.WorldToScreenPoint to calculate the "UI-Position" of your GameObjects and then calculate the distance using z-Coordinates of Gameobjects and Camera.
Use a raycast from the player to the game object, and calculate the difference.

Not able to run unity 3d app properly

I have just started to learn Unity 3D. I am using this as first lesson. I created a terrain, added texture, plants and grasses, added first person controller and directional lighting. When I press play, the scene automatically starts moving along Y direction. I want the scene to move only when i give input using keyboard or mouse. How to achieve this ?
The first person controller has to be placed above the terrain completely. After doing this I get the desired effect.

Resources