Animation Starts but player won't move on key press in Unity - c#

Me and 3 friends started working on game and while codding run into error that animation on player starts but it won't move. We are working in Unity(C#)
I already checked for names in Animator and code so it's not it.
Here is a code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float moveSpeed = 5f;
public Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f , 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
if(Input.GetAxis("Horizontal") > 0){
anim.SetBool("isWalking", true);
}
else
{
anim.SetBool("isWalking", false);
}
}
void Jump(){
if (Input.GetButtonDown("Jump")){
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f,5f), ForceMode2D.Impulse);
}
}
}
It's 2D game if It matters

You could try to create the rigidbody2d in start(), then try
rigidbody.AddForce(Vector2(moveSpeed,0), ForceMode2D.Impulse);

Related

After transferring my project from my PC to a laptop a piece of code doesn't work

I'm learning Unity and am following a guide. In the game you have to fly a rocket through an obstacle course. This is my code for the movement of the rocket:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
Rigidbody rb;
[SerializeField] float mainThrust = 100f;
[SerializeField] float rotationSpeed = 50f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
ProcessThrust();
ProcessRotation();
}
void ProcessThrust()
{
if (Input.GetKey(KeyCode.Space))
{
//Boost
rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
}
}
void ProcessRotation()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
//Rotate left
ApplyRotate(rotationSpeed);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
//Rotate right
ApplyRotate(-rotationSpeed);
}
}
void ApplyRotate(float rotationPreFrame)
{
rb.freezeRotation = true; // freezing rotation so we can manually rotate
transform.Rotate(0, 0, 1 * rotationPreFrame * Time.deltaTime);
rb.freezeRotation = true; // unfreezing rotation so the physics system can take over
}
}
For some reason I can't fly the rocket, only rotate left and right. Got any idea why?
Did you check if you have a rigidbody applied on the object?
If so, check that inside Project settings > Player > Active input handling is set to BOTH.
Anyway, try to change your input system to the new one:
https://learn.unity.com/project/using-the-input-system-in-unity

Unity 2d Input lag

I'm making a 2d open world game with physics similar to that of stardew valley, but for some reason there's a lot of input lag on the character even when I remove all the other scripts and animations.
I even tried removing most of the sprites such as houses and whatnot but it still has a lot of lag, not framerate lag, just the player movement.
I will attempt to explain: If you just lightly tap the keyboard the player moves and stops just fine, but if you hold down the button then let go (for about 5s gives the most lag, doesn't get any worse after that), the player keeps moving a little after you let go of the key.
Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementControls : MonoBehaviour {
public float speed;
private Vector2 MoveSpeed;
private Rigidbody2D Player;
void Start() {
Player = GetComponent<Rigidbody2D>();
}
void Update() {
Vector2 PlayerInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
MoveSpeed = PlayerInput.normalized * speed;
}
void FixedUpdate() {
Player.MovePosition(Player.position + MoveSpeed * Time.fixedDeltaTime);
}
}
Anyone know how to fix this?
Here's what fixed the problem to anyone else with said problem:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementControls : MonoBehaviour {
public float speed; //Change the player's speed
private Vector2 MoveSpeed; //This is what the player's speed will be set to after some math
private Rigidbody2D Player; //The player
void Start() {
Player = GetComponent<Rigidbody2D>(); //Find the player GameObject
}
void Update() {
Vector2 PlayerInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); //Get input
MoveSpeed = PlayerInput * speed; //Math
Player.MovePosition(Player.position + MoveSpeed * Time.fixedDeltaTime); //Move the player
}
}

How to make my 2D character 'look' or flip in the direction he is moving?

I'm a total newbie in Unity and I'm learning/making my first game, which will be a platformer. I want to get the movement perfect first. I've added a simple script that enables the player to move and jump.
Here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour{
public float jumpspeed = 5f;
public float speed = 5f;
Rigidbody2D rb;
GameObject character;
// Start is called before the first frame update
void Start()
{
}
void Awake(){
rb = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)){
Jump();
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += move *Time.deltaTime * speed;
}
void Jump(){
rb.AddForce(new Vector2(0f, jumpspeed), ForceMode2D.Impulse);
}
}
Now, I want the character to face in the direction in which it moves. The png is facing to the right by default.
How can I do that? Also, can I make my movement script better?
Thanks in advance!
I typically do this using code like this based off a bool.
void FlipSprite()
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}

With what I should replace dirX for my bullets to shoot right and left

I am doing a 2d game and I want to know how I can make the bullets to shoot right and left . At this moment the bullets go just to the left , even if my player moves right. How I can make them shoot both sides or shoot just when they find an object tagged " enemy "
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Character : MonoBehaviour
{
Rigidbody2D rb;
float dirX;
[SerializeField]
float moveSpeed = 5f, jumpForce = 400f, bulletSpeed = 500f;
Vector3 localScale;
public Transform barrel;
public Rigidbody2D bullet;
// Use this for initialization
void Start()
{
localScale = transform.localScale;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
dirX = CrossPlatformInputManager.GetAxis("Horizontal");
if (CrossPlatformInputManager.GetButtonDown("Jump"))
Jump();
if (CrossPlatformInputManager.GetButtonDown("Fire1"))
Fire();
}
void FixedUpdate()
{
rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
}
void Jump()
{
if (rb.velocity.y == 0)
rb.AddForce(Vector2.up * jumpForce);
}
void Fire()
{
var firedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
firedBullet.AddForce(barrel.up * bulletSpeed);
}
}
You're already accounting for the direction of the barrel. You just need to change the direction of the barrel when you move.
One way to do that is to just set that directly:
// Update is called once per frame
void Update()
{
dirX = CrossPlatformInputManager.GetAxis("Horizontal");
if (dirX !=0)
{
barrel.up = Vector3.right * Mathf.Sign(dirX);
}
if (CrossPlatformInputManager.GetButtonDown("Jump"))
Jump();
if (CrossPlatformInputManager.GetButtonDown("Fire1"))
Fire();
}
If the barrel is a child of the player object, then changing the character's rotation so that the barrel's direction points in the correct direction will also work. There's not enough information in the question to know for sure but maybe using Quaternion.LookRotation to set the character's rotation like so would work:
// Update is called once per frame
void Update()
{
dirX = CrossPlatformInputManager.GetAxis("Horizontal");
if (dirX !=0)
{
Vector3 newPlayerForward = Vector3.forward * Mathf.Sign(dirX);
transform.rotation = Quaternion.LookRotation(newPlayerForward, Vector3.up);
}
if (CrossPlatformInputManager.GetButtonDown("Jump"))
Jump();
if (CrossPlatformInputManager.GetButtonDown("Fire1"))
Fire();
}

How can I get my animation to play while moving forward?

I have two animations, idle and jump. I want the jump animation to play at the same time as when the cube moves so it looks like it is moving up and forward but right now I can't get them to play together. Here is my code so far.
using UnityEngine;
using System.Collections;
public class MovePlayer : MonoBehaviour
{
private Vector3 newPos;
public Animator animator;
void Start(){
animator = GetComponent<Animator> ();
}
void Update(){
if (Input.GetKeyDown (KeyCode.UpArrow)) {
newPos = Vector3.forward + transform.position;
animator.SetBool ("jump", true);
transform.position = (newPos);
} else {
animator.SetBool ("jump", false);
}
}
The new position must be applied to the actual position :
transform.position = transform.position + (newPos);

Categories