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.
Related
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
}
}
I have a game in Unity where cubes are repeatedly moved past the camera. I move the cube using a script where I set its Rigidbody's velocity each update. Once it moves out of view my script instantiates a new cube on the other side which begins the process again.
Recently I've found that it works fine for a random amount of cubes before, seemingly randomly, a cube is instantiated that does not move. Using the inspector I can see that this object has velocity. If I move it even a small amount using the editor it starts to move as normal.
Has anyone seen something like this before?
I'm fairly certain the problem was related to the fact I was trying to directly modify the velocity( The physics engine decided the object was at rest and stopped it moving. ). By setting the object to be kinematic and modifying its position in my code I solved the problem.
May be you are changing the velocity of your gameObject when it goes through a specific coordinate (in an if statement for example), unity is not very accurate sometimes with coordinates so it may be happening that the condition is never met. Change that condition and add a margin range to solve this error.
I'm trying to make a game using unity and at this point I have created a cube object, a ground object a couple other things. I can make it move by giving it a forward force. I'm basically trying to make a game about a cube that you can navigate through obstacles.
What I want to know is how to get the input like touch input. For example you would use getkey("d") If you wanted to get input from "d" in the keyboard. How would I do the same except for touch input in android?
Use this
Input.GetMouseButtonDown(0)
Example
if(Input.GetMouseButtonDown(0)){
circle.velocity = Vector2.up * jumpForce;
}
https://docs.unity3d.com/ScriptReference/Input.GetTouch.html
You can use this to get a touch object which will give you the screen position, then you can use your cameras position and angle to cast a ray from that point to see if the user is touching the cube.
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.
I'm trying to make a respawn system for a game in unity that starts the character back on the last platform they were on.
As it is currently, it keeps track of which platform they were last grounded on with onCollisionEnter and detects if onCollisionExit touches an out of bounds area.
I need to find the position of a face on the mesh with the y axis (assuming the best way is to do a raycast on the global y axis from the center of the platform) and add the height of the character/2 to determine where to respawn the character.
I'm very new to unity and c#, so I've never done a raycast before and I'm not sure if it's possible to raycast from inside an object to find it's mesh in a given direction, or if there is a better/more efficient way.
Thanks in advance :)
"if it's possible to raycast from inside an object to find it's mesh in a given direction"
You can place a empty gameobject at the center of your mesh (make it child of your mesh ) then pass the position of this empty gameobject to raycast origin.
I usually make re-spawn system with triggers. If you explain little bit more what actually you want to do. I'll try to guide you in that particular direction.