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
Related
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
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
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
I have a simple sprite sheet that I'm using in a Unity project:
It has frames for walking and standing in each of four directions. I've cut the image up using Unity's Sprite Editor and made a separate Animation object for each series, all of which fit neatly into a single animation controller:
I've added the animation controller to an Animator component on a GameObject in my scene. This is where I start running into trouble. I can't for the life of me figure out how to change between the animations in my code.
You are not supposed to create the connections between your animations in a script. What you do is you create the connections in the Animator and set up variables for if you are standing or walking and in which direction you are going. Then you just edit these variables in a script and the Animator takes care of chosing the right animation.
This video shows it in action: Example
(you see the connections and in the bottom left the variables he set up)
Quick google of "mechanim 2d tutorial" gives you: Tutorial
edit:
Too long for a comment :)
Basically what i would do is create 2 variables, one bool named "Standing" and one Integer named "Direction". Then you make one connection from AnyState to every other state you have(Rightclick on AnyState to make the transitions). Maybe order them in a circle around AnyState.
After this, click on the connections and in the Inspector set the conditions by chosing "Standing", then add one more and set it to "Direction" "Equals" and the number for the direction.
Then in your script somewhere in your Update function you might have something like:
private readonly int UP = 0;
...
if(speed.magnitude > 0.0f)
Animator.SetBool("Standing", false);
else
Animator.SetBool("Standing", true);
if( Condition for walking up? )
Animator.SetInteger("Direction", UP);
if( Condition for walking down? )
Animator.SetInteger("Direction", DOWN);
...
this might not work directly, but you get the idea. You might want to use a switch for the direction depending on how you set it up.
Why even use code? Just hook it all up in the Animator using parameters and just change the parameters as needed.
The Animator.CrossFade function will achieve the desired results:
C#:
Animator animator = this.GetComponent<Animator>();
animator.CrossFade("WizzyWalkDown", 0);
It may seem a little excessive, or counter-intuitive, but remember that the AnimationController class was built to make transitions between animations easy. It can still very much be used for simpler animation types like this, though!
Just first off I'll say that the context here is Actionscript 3.0 (IDE: Flashbuilder) along with the Starling Framework.
So, I want to create a Tile Map that could be used for a platformer or something similar.
I want to use 8x8 pixel tiles on an 800x600 pixel stage, and the problem I am having is that I don't know how to add these 7500+ tile objects to the stage without dramatically reducing the framerate.
I've found that the drop in performance comes from adding each tile to the stage, not from initializing each Tile object.
I know I'm not giving much specific information, but what I'm asking for is if there is a standardized way to draw thousands of static objects onto the stage without a loss of performance. I feel like there is a way, and I just have yet to find it.
Update:
After all of your kind help, I have found what seems to be a great solution. At first I wanted to implement Amy's solution, using copyPixels() and draw() to make one large bitmap data for the whole map and then render that to the screen. Then, though, I wanted to know if there was a Starling equivalent to this, because everything would be so much simpler if I didn't have to mix Starling with Native Flash.
Thanks to Amy again, I looked into Starling's RenderTexture class a bit more, and found that using it's "drawBundled()" and "draw()" methods, I could easily draw all of the tiles into a RenderTexture, and then put the RenderTexture into an Image (Starling's Image Class) and then just add that Image to the screen.
That solution is a million times faster than the silly slow solutions I tried before, with flattening sprites and such. Its faster both in it's initialization time and there seems to be no drop in framerate while the renderTexture's Image is on the screen.
The one thing I want to test with this is if it is easy to update the graphics of a tile during the gameplay. Say, if water spreads from a source (or something) and a "Grass" tile had to become a "Water" tile, would the RenderTexture and it's image be able to change their appearance without some sort of lag spike or performance hiccup. I will test this out soon.
Thank you all for your help!
Don't add that many objects to the stage. Instead, create a BitmpaData the size of your stage and use copyPixels() or draw() to draw onto it. Here's an article that should get you started. You can then take the concepts you learned in that post and learn anything specific you need to do that's not covered (flashandmath.com has a lot of good tutorials about pixel manipulation)
You need to manage the tiles that need to be added and removed as you move around the game. Only add to stage tiles that are with in 800 px of the center of the screen. Once the tile is beyond 800 px from center remove it. That should keep everything moving smoothly. Good luck.
or look into drawing/coping your tiles into one bitmap. You would be basically stamping your tiles onto the new bitmap. Here is an example from adobe:
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
var bmd1:BitmapData = new BitmapData(40, 40, false, 0x000000FF);
var bmd2:BitmapData = new BitmapData(80, 40, false, 0x0000CC44);
var rect:Rectangle = new Rectangle(0, 0, 20, 20);
var pt:Point = new Point(10, 10);
bmd2.copyPixels(bmd1, rect, pt);
var bm1:Bitmap = new Bitmap(bmd1);
this.addChild(bm1);
var bm2:Bitmap = new Bitmap(bmd2);
this.addChild(bm2);
bm2.x = 50;
More Info on the bitmapData class. I think copyPixels is what you are after.