Lets say I have a path, could be a line or a curve in Unity and I wanted to allow my player to snap to that path and move along it how would I achieve this. This would look like either a cover system or something similar to how ledges work in Sly Cooper.
While if you want your player's movement to follow a path, there is a technique called WayPoint, which is simply using the build-in navigation system in Unity. There exist so many articles that teaching you how to manually build such a Way Point System, and at the same time, you can get a SimpleWayPoint module from the online assets store.
But if your player is controlled by user, which means the user can control it's movement, let's say pressing 'W' button meaning moving forward while pressing 'S' button meaning moving backwards, and the leftwards and rightwards movement are constrained by the path you defined, unfortunately I didn't find any mature technology which allows you realize this constrains.
However, in the project I accomplished several months ago, I manually handle the coordinates successfully, since I create a city whose streets strictly observe horizontal and vertical grid lines, I can easily controls the target gameobject's X and Y coordinates, by either using the build-in freeze coordinates API or manually reseting the axis values over and over again in the Update() method. So maybe you could using the Update() method to constrain the targets' coordinates by dynamically calculating it's coordinates using the functions representing the path, but I can imagine how complex it would be.
You could try using a navmeshagent, which will be enabled as soon the player would like to use this "auto movement". In your code you can set the destination and in your scene you can create a navmesh, which can be whatever shape you like
You can do it by:
Mark the position from where you want your player NOT to move forward/cross the line.
In your script, mention the position.
LOGIC
if, player position > position marked
then stop the movement,
SYNTAX
private void update(){
if(transform.position.x < -10){
transform.position = new Vector3(-10, transform.position.y, transform.position.z); // Here you can either use `Vector3` or `Vector2` according to your game
}
}
Related
I would like to understand how to create a script to climb objects of different heights like in this video this.
I know you have to use LayerMask to understand if you are in front of an object, but I don't understand the script that brings the character from below to above the object or on the other side (if it is a wall to climb over) .
For a ladder, I thought I'd put "gravity = false" and "transform.up * Input vertical" to go up or down. But to position yourself above a wall in this way, what script do it's used?
And how does the same animation be usable on walls of different heights as in the video ?
Use an animator, setup your animations in your state machine, in the animator click ‘apply root motion’ and your animation will apply to your character position.
I'm fairly new to game development and I'm trying to develop a 2D game in unity where the main character has a jetpack with two thrusters and I want him to control each one individually. So if he only turns on one thruster he goes 5 meters above ground and hovers there while using both would make him hover at a height of 10 meters. How would i go about doing this?
I tried just simply adding forces as if the character was jumping and freezing the y-axis to 0 until he let go of the jetpack button but that did not give me the feel i was looking for. I also tried raycasting a line out of the jetpacks to the closest surface and adding a force to the jetpack to allow the player to float at that height but that I couldn't figure out a proper way to implement it.
The Raycast sounds like a great idea. Do the raycast to the next available platform and do as #Ahndwoo said in a comment, `
What you would need to do is divide the force of your thrusters by the
magnitude of the Ray. Thus, as the Ray gets longer, the force
decreases.
By doing this you'll get the natural movement from a force and you'll control how high can you get.
I would Make your thrusters holder as an object and as more turbines it has, the more powerful i'll get.
I'm using a character controller, which moves the character along a set path using ray casting:
This works fine for moving by x and z axis, and I'm also able to land on objects if I hit them directly from top. But if I enter an object with a collider from the side, either pass through or get stuck:
I assume this happens because the player is set to position itself along the raycast, and to avoid the player to teleport up on the platforms, I need the raycast to ignore them. Now this causes the new collision problem. Does anyone have any idea on how to work around this, so my player properly is colliding with the platforms? I'm using all physics inside a FixedUpdate(), and the path is created using the Tween plug-in, but I assume this issue would appear with any script using raycast in this manner.
Add specific mask to those platforms, and in code where you calling ray cast, just add new variable for mask and there just don't select the platform mask.
I am wanting to make a birds-eye view pixel-art game.
I currently have two sprite sheets set up, and split and whatnot
groundSheet and characterSheet these are split up into
ground_0_0_0 (A concrete floor)
ground_1_0_0 (grass)
character_0_0_0 (man idle animation frame 1)
character_0_0_1 (man idle animation frame 2)
character_0_1_0 (man run animation frame 1)
character_0_1_1 (man run animation frame 2)
character_1_0_0 (woman idle animation frame 1)
character_1_0_1 (woman idle animation frame 2)
character_1_1_0 (woman run animation frame 1)
character_1_1_1 (woman run animation frame 2)
The numbers after are a note as to:
first number - the main set of sprite animations (eg man)
second number - the animation set in use (eg run or idle)
third number - the frame of said animation.
(the ground has this as i plan to have animated grounds late on)
Now, I wish to make a script for the character (and ground alike) that has an editable value that is view able in the unity editor, for example how things like the sprite renderer has sprite, colour etc. Which dictates what first number to use (see above) what second number and the delay for the animation of the third number. This will then tell the sprite renderer what pictures to load and how quickly to load them. I also wish for the script to scan for the file starting with for example character_0_0_ and then count how many files after it, so it knows what to do when animating. I would like this script to be checking for a change in one of the variables viewable in the unity editor to change and as soon as it does it checks everything it needs for an animtion.
Something else could be done where there is only 1 box to edit in unity, which you put character_0_0_ or ground_1_0_ or something similar, and it checks everything that way (it also makes the script universal, and usable on the ground, character and walls (which I am adding later)).
This may seem confusing, but it make sense for me and many of you will probably mention a much easier way to do animations and such, but please only say these if it does what I want above.
For scripts and such my file layout:
/Assets
/scripts
ground.cs
character.cs
/sprites
characterSheet.png
character_0_0_0
character_0_0_1
character_0_1_0
character_0_1_1
character_1_0_0
character_1_0_1
character_1_1_0
character_1_1_1
groundSheet.png
ground_0_0_0
ground_1_0_0
(For some reason Stack overflow said the above was code, so i had to make it as that)
ground.cs and character.cs are the scripts in which I want to made as explained above.
In my object view thingy I have
Main Camera
ground
character
I am practically a newb to C# and JS I know bascially the grammar of C# (like where to use {} and put ; at the end of the lines). If you help me with this, i request that you explain the script, like use the // thing to simply explain what each command does, I know a few but not all of them. And I know someone is going to say it is really well documented in tutorial X and such, but most tutorials are not in unity 5 and while helping with the matter do not touch on it exactly.
Thank you for your help, if there is anything about this question/request that you do not understand (It is pretty complex) I will explain.
Maybe I am completely wrong, but it seems to me that you are trying to recreate an Animation system.
Is there a specific reason for which you wouldn't use Unity's animation system?
You can do everything that you describe here with it (change sprite, choose animation speed). And you would have almost no code to write. Just drag and drop you sprites in the editor for a start
EDIT - answer to first comment:
What you should do is create all the animations you need. Then in the animator, you choose which condition will trigger a transition to another animation (for instance, boolean jump is true will transition to animation jump). Then in your code you can call GetComponent().SetBool("Jump", true).
To have different character, you can create different gameObjects (one per character). They all have a different animator with animations specific to the character.
The other solutiojn if you really want one one gameObject and one animator is that you also add string condition to you animation (example, if character=="character1" and jump==true, transition to jump animation).
If I were you I would really focus on testing and learning all you can do with Unity animator. I can't think of a case were you would need to recreate the animation system yourself
Your question was long winded and hard to understand but ill try to help,
firstly if you want editable values in the unity editor I would Suggest using a serialized structure of information like this
[System.Serializable] // this makes it viewable in the inspector
public struct Sprite_Manager;
{
public Sprite[] Render_Sprites; // array of sprites to store sprites
public SpriteRenderer S_Renderer;
public float Anim_Delay;
}
public class Character : MonoBehavior {
Sprite_Manager SMG = new Sprite_Manager(); // instantiate a new instance of the struct
void Set_Sprite()
{
for(int i = 0; i < SMG.Render_Sprites.Length; i++)
{
SMG.S_Renderer.sprite = SMG.Render_Sprites[i];
}
}
void Update
{
Invoke("Set_Sprite", SMG.Anim_Delay);
}
}
Not sure if this is exactly what your looking for but this is one way you could setup a structure of sprite based information and use Invoke to setup some sort of delay when passing new sprites to the renderer.
I have a marine model used in my start project, which will uncontrollably lift off the ground when running. I import the fbx resources, set the animation type as humanoid and configured the avatar by automatically mapping, set up a animator controller that contains only a running animation. Here is about several seconds after playing:
But when using a generic animation type everything works fine. Any suggestions to fix this while still using the avatar system?
UPDATE:
Configure of my 3D model:
This is obviously caused by root motion. What happens is, one loop of your animation takes the character slightly higher. Adding these slight changes up, you get what you're getting. If you don't need root motion (doesn't look like you do), disable it (from the animator component's settings). If you do, either edit the animation to make sure it fits, or disable root motion along the Y-axis (you can do this from the animation's import settings).
In case you don't know what root motion is, it's when the root bone of your model has animations applied. You obviously can't create the entire animation of character running up and down your levels, and until recently (though not MUCH recently) characters where animated in-place, and moved procedurally via code (I know for a fact that Unreal Tournament 3 uses this method, as would any UDK user). Then, people started wondering how they could make their characters move more realistically? I mean, it's not like you walk forward at a constant rate of 4 km/h, you tend to slow down and speed up during different parts of the walk cycle. The same can be applied to video game characters using the technique known as root motion.
With root motion, you actually move the character forward during its animations. This will cause an animation to look really bad in max or maya, since the character will just snap back to its original place after a loop. However, this data is used intelligently in game engines: Rather than use the absolute position the animation yields, you take the velocity out of it between each two frames, and move your character based on that velocity (Unreal engine actually has a really neat acceleration mode for applying root motion, though I'm not really sure how that would be different from velocity mode). This will make your character move forward at the same rate the animation does, and thus you can actually animate the character's movement as well as its limbs and joints. Moreover, since you're using the velocity and not position data from the animation, it will look exactly as you'd expect it to. If you're interested in this technique, take a look at the Mechanim demo pack they have on the asset store. It makes extensive use of root motion to move the character around.
My company was having a similar issue but we still wanted to keep the "Apply Root Motion" toggle checked. When the same animation played on loop, the model stayed in place but if several different animations were played one after another, this caused the model to rotate / shift in position.
The solution for us was ticking these check boxes in the animation settings for each animation. Root Transform Rotation, Root Transform Position (Y), Root Transform Position (XZ).
I had the same issue a few days ago. I found out that the problem was the Apply Root Motion in the Animator script. Make sure it's unchecked.
Tag your player with "Player" in scene
and use this script
float y;
GameObject player;
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player");
y = player.transform.position.y;
}
// Update is called once per frame
void Update ()
{
float diff = player.transform.position.y;
player.transform.Translate ( 0, 0,z - diff);
y = player.transform.position.y;
}
it is little hacky soultion but works!!
note: if you want to use y movement at some point just calculate and add it to diff variable.
For those who couldn't solve this issue with 'bake into pose'.
I tried 'Bake into Pose-Y', but it didn't work.
Meanwhile, in FBX > Animation > Motion, I set 'Root Motion Node' as 'Root Transform'(It was 'None' before), it solved my problem. Unity version is 2020.3.34f1.