I have written this code for my controller in Unity. The problem is that the jump in Y axis has different height than the jump for X and Y axis simultaneously.
// Update is called once per frame
void FixedUpdate()
{
Debug.Log(rigidbody.velocity);
float inputX = Input.GetAxis("Horizontal");
//Movement X
if(inputX != 0 && isGrounded)
{
animator.SetBool("isWalking", true);
rigidbody.velocity = new Vector2(inputX*speed, 0);
//Turn left & right
if(inputX > 0)
{
spriteRenderer.flipX = false;
} else if (inputX < 0)
{
spriteRenderer.flipX = true;
}
} else
{
animator.SetBool("isWalking", false);
}
//Jump
if(Input.GetAxis("Jump") > 0 && isGrounded)
{
rigidbody.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse);
}
}
if i understand your code right, you only gives force to X when you are in ground, so i suggest to remove isgrounded variable and use it just for jump for example in your update method
if(isGround){
if(keypress == y){
addforce in Y
}
}
actually im a bit oxided in unity but i hope to be usefull about logical
I am pretty bad myself when it comes to the jumping physics, but i found these links maybe these will help you.
Unity 2d jumping script
https://answers.unity.com/questions/710765/2d-c-jump-script-addforce-1.html
I also want to know which jump is higher. You stated that there is a height difference when you jump without moving vs when you are moving. Which of the two jumps higher? Are you actually sure that one jumps higher than the other or does it just look like one jumps higher? You can test this by placing a platform an test if you can make it.
Sorry I can't me of more help and can only speculate of what might cause the problem. I would recommend however that when you do movement that you multiply it by Time.DeltaTime. This makes the movement time based instead of frame based. This will make the movement feel smoother.
Related
So I'm testing around with Unity and Visual Studio, and as a starter project I'm working to replicate Zelda (NES) and I'm starting with movement.
So far, with much jank, I've figured out how to prevent diagonal movement, but I'm having trouble figuring out the next part. In the game, if favors vertical movement when there are 2 inputs, but if you hit a wall, it lets you move horizontally until nothing is blocking your vertical movement.
I do not know enough to even think about how to do this. Anyone have an idea that can set me the right direction? Or perhaps just a better way of doing it? I'm still learning C#
public float MoveSpeed;
public Rigidbody2D rb;
Vector2 movement;
void Update() {
MovementInput();
}
private void FixedUpdate() {
rb.velocity = movement * MoveSpeed;
}
void MovementInput() {
float mx = Input.GetAxisRaw("Horizontal");
float my = Input.GetAxisRaw("Vertical");
//This makes diagonal impossible, favoring vertical.
if (my != 0) {
mx = 0;
}
movement = new Vector2(mx, my).normalized;
}
Try to compare the horizontal and the vertical axis absolute value. The highest would gives you the good direction.
if (Math.Abs(my) > Math.Abs(mx)) {
// up or down
} else {
// left or right
}
I'm learning unity and c#, and want to make my movement to be camera relative movement instead of world relative movement. How do I do that?
I'm learning unity and c#, my unity version is 2018.3.12f1. I would be happy for help.
just to let know, instead of moving the cam I'm rotating the player.
void Update()
{
float AxisY = Player.transform.eulerAngles.y;
/* Movement starts here */
Vector3 Movement = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { //running code
Player.transform.position += Movement * running_speed * Time.deltaTime;
} else {
Player.transform.position += Movement * speed * Time.deltaTime;
}
/*Movement ends here */
/* Rotation controller starts here */
Quaternion target = Quaternion.Euler(Player.transform.eulerAngles.x, Player.transform.eulerAngles.y, Player.transform.eulerAngles.z);
/*if (Player.transform.eulerAngles.x != 0 || Player.transform.eulerAngles.z != 0 || Player.transform.eulerAngles.y != 0) {
Player.transform.rotation = Quaternion.Euler(0,0,0);
}*/
if (Input.GetKey(KeyCode.E))
{
Debug.Log("E got pressed");
//float AxisYPositive = Player.transform.eulerAngles.y;
AxisY = AxisY+1;
Player.transform.rotation = Quaternion.Euler(0, AxisY, 0);
} else if (Input.GetKey(KeyCode.Q))
{
Debug.Log("Q got pressed");
//float AxisYNegetive = Player.transform.eulerAngles.y;
AxisY=AxisY-1;
Player.transform.rotation = Quaternion.Euler(0, AxisY, 0);
}
}
}
The player's movement is world relative, how to make the movement camera relative?
If you want to make the movements relative to the gameObject, call the method Transform.Rotate() on the transform of the gameObject you want to rotate rather than modifying its Quaternion directly. Just make sure the final argument is set to Space.Self.
if (Input.GetKey(KeyCode.E))
{
Debug.Log("E got pressed");
//float AxisYPositive = Player.transform.eulerAngles.y;
AxisY = AxisY+1;
Player.transform.Rotate(Quaternion.Euler(0, AxisY, 0), Space.Self);
}
In general you don't want to directly mess with objects transform.rotation, at least not unless you at least somewhat understand quaternions (I don't!).
I can see a few issues with your code, but the common thread seems to be that you don't really understand how transforms work. Specifically, you might want to look into World/Local space.
The usual way to control a player goes roughly like this:
void DoMovement(Transform player)
{
//If you move first your controls might feel 'drifty', especially at low FPS.
Turn(player);
Move(player);
}
void Turn(Transform player)
{
float yaw = Input.GetAxis("Yaw") * time.deltaTime; //Aka turn left/right
player.Rotate(0, yaw, 0, Space.Self);
// Space.Self is the default value, but I put it here for clarity.
//That means the player will rotate relative to themselves,
//...instead of relative to the world-axis, like in your code.
}
You didn't ask about movement, but as-is your character will always move relative to the world. The below should make it move relative to the camera.
Transform _cameraTransform; //Assumes this is set druing Start()
void Move(Transform player)
{
var forwardMove = _cameraTransform.Forward; //Get whatever direction is 'forward' for camera
forwardMove.Y = 0; //Don't want movement up and down.
forwardMove = forwardMove.normalized; //Normalize sets the 'power' of the vector to 1.
//If you set Y to 0 and don't normalize you'll go slower when camera looks down
//...than when camera is flat along the plane
player.position += forwardMove * Input.GetAxis("Vertical") * time.deltaTime;
//Here you could do the same for strafe/side to side movement.
//Would be same as above, but using the transform.right and Horizontal axis
}
Now, I'm making some assumptions here since you haven't specified what kind of game it is and what kind of controls you want. I'm assuming you have a character running around on a mostly flat plane (no aircraft/spaceship controls), and that the camera is attached to the player. This might not not actually be the case.
In any case I advice you to check out the tutorials, especially the Roll-a-Ball tutorial which I have found is good for beginners to get a grasp on basic players controls that are not just world-relative. The other tutorials, too, are pretty good if you think they're interesting.
Aside from the official Unity tuts a ton of decent to amazing tutorials out there, including video tutorials, so for something like this you could just search for <game type> tutorial and pick whatever seems good to you. While getting started I advice you to avoid the shortest videos, as you will likely benefit greatly from explanation that only fits in longer videos. Of course, that doesn't mean you should pick the longest videos either.
In case someone needs to move an object and don't care about colliders, you can use transform.Translate and assign to his second parameter relativeTo your camera (or any transform) to automatically calculate the translation relative to the object assigned.
Allright, I have gameObjects with tag "Enemy" and I collect them like this:
enemies = GameObject.FindGameObjectsWithTag("Enemy");
After that, I am trying to move the first "Enemy":
int newPositionX;
int newPositionY;
bool targetReached = false;
int moveSpeed = 1;
void Update()
{
if (!targetReached)
{
newPositionX = Mathf.FloorToInt(enemies[0].transform.position.x)-1;
newPositionY = Mathf.FloorToInt(enemies[0].transform.position.y);
enemies[0].transform.position = Vector3.MoveTowards(enemies[0].transform.position,
new Vector3(newPositionX, newPositionY), moveSpeed * Time.deltaTime);
targetReached = true;
}
if (Vector3.Distance(enemies[0].transform.position, new Vector3(
newPositionX, newPositionY)) < 0.1f)
{
targetReached = false;
}
}
But an "Enemy" is not making any moves. I can edit that if I remove the:
targetReached = true;
the "Enemy" is moving but there's no way I can stop it. That makes me think that to make sth moving MoveTowards function should be called permanently in the Update?
MoveTowards returns a vector that moves the gameobject one step in the direction of the target.
Also, I am not sure about this, but looking at your code, the if statement that sets the targetReached to false only runs if the targetReached is already false, so you may want to put that out of the if (!targetReached) statement
You probably had the idea that Unity is a complete game engine.
It isn't.
The most basic of things, like tweening and movement aren't provided in the ways just about every other game engine on earth provides them.
So you need to download and setup a plugins and add-ons to get basic functionality.
Search for tweening engines for Unity, and read up about them, as you'll need to understand them and how they incorporate themselves into Unity for the next time you meet missing basics of Unity.
I'd like to thank you in advance for helping me with this issue.
I have a player that consists of three part:
The body
The head
and the Arm
All three of these are individual sprites.
I have grouped them all under one Empty GameObject that I named "Player"
I have then added an animation on the "Player" GameObject that changes the Y position from 0 to 0.022, and then from 0.022 back to 0.
This create a nice little hover animation where the character bounces up and down.
The animation works perfectly fine!
However, I also have a script attached to the "Player" object, this script is called "PlayerController".
In PlayerController I change the player's x Position if they hold down A (for leftwards movement) or D (for rightwards movement).
protected bool facingRight = true;
float xPos = 0;
void Update()
{
if (Input.GetKey(KeyCode.D))
{
xPos += 0.01f;
if (!facingRight)
{
Flip();
}
}
else if (Input.GetKey(KeyCode.A))
{
xPos -= 0.01f;
if (facingRight)
{
Flip();
}
}
gameObject.transform.position = new Vector2(xPos, transform.position.y);
}
protected void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
When I hold A or D, the character refuses to move. However he does flip correctly.
I personally think my animation is fighting the script, and that the animation has a higher priority than the script...?
Here's a video of me explaining my issue
(Not sure if videos are generally accepted in this community or not. If not, then I apologize)
If you guys could please help me understand what is happening and how to correct it I would be extremely grateful.
Thank you! :)
Solved the problem after hours of tinkering.
Turns out the solution was simple:
All I had to do was structure my player like this:
Player (Empty GameObject)
PlayerAssembly (Body, Head, and Arm's parent)
Body Parts (Body, Head, Arm)
and then put the script, rigidBody2D, and collider on "Player".
Then put the animator on "PlayerAssembly".
I'm trying to make it possible to drag an object. This object can only rotate so much. (Similair to a door).
Here is the code abit edited that rotates an object which works.
I have 2 vectors for maxrotation and minrotation.
This code will be called whenever the user is dragging the interactible object. (like update but only when dragged)
if (GestureManager.Instance.IsNavigating &&
HandsManager.Instance.FocusedGameObject == gameObject)
{
//speed and navigiation of rotation
float rotationFactor;
rotationFactor = ManipulationManager.Instance.ManipulationPosition.y * RotationSensitivity;
totransform.Rotate(new Vector3(rotationFactor, 0, 0));
}
It would be great if I could use an if statement here. And I tried quite some things but it's still not working.
As stated the code paste here works. The object should be dragable but only up to a certain points.
totransform is the transform that will be rotated
Any ideas would be great and most appreciated.
Kind regards.
I think you want to look at eulerAngles. Check the values you're getting then set an if-statement before doing the rotation. This is a sample code for you to find the values you want:
if (GestureManager.Instance.IsNavigating &&
HandsManager.Instance.FocusedGameObject == gameObject)
{
//speed and navigiation of rotation
float rotationFactor = ManipulationManager.Instance.ManipulationPosition.y * RotationSensitivity;
Debug.Log(totransform.eulerAngles);
if (totransform.eulerAngles.x < 100) {
totransform.Rotate(new Vector3(rotationFactor, 0, 0));
}
}
So here is the solution that worked for me. First I declare the movement variable (not seen down below, which is 2 in this case). Then I track the distance covered and put a limit on that.
Of course there are some improvements to this code like use movement instead of 2. But because of time constraints I didn't do it.
if (GestureManager.Instance.IsNavigating &&
HandsManager.Instance.FocusedGameObject == gameObject)
{
//here we get the movement direction and set it in movement.
if (GestureManager.Instance.NavigationPosition.y > 0)
{
movement = 2;
}
else if (GestureManager.Instance.NavigationPosition.y < 0)
{
movement = -2;
}
//the first part is false if we reach higher then maxdistance and the movement is going up
//the second part is false if we reach the lower distance and the movement is going down.
if ((!(distance > maxdistance.x) || movement < 0) && ((!(distance < mindistance.x) || movement > 0)))
{
//here we add the movement to the distance so we know if it gets closer or further
distance += movement;
//here we rotate
totransform.Rotate(new Vector3(movement, 0, 0));
}
}