Home>
Lateral movement suddenly stopped moving in unity, so I want to improve it.
I searched for questions and tried various methods
It does not solve.
If i were editing Animation or Animator.
In a script that controls the character
No longer move sideways.
I can jump for some reason.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class novaControler: MonoBehaviour {
// common
public GameObject mainCamera;
Rigidbody2D rb2d;
Animator anim;
//attack
int attackNum;
// move
public float Runspeed = 10.0f;
public float Scroll = 1.0f;
// Jump
public float jumpPower = 700;
public LayerMask groundLayer;
public float velY;
private bool isGrounded;
// Use this for initialization
void Start () {
anim = GetComponent ();
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
velY = rb2d.velocity.y;
bool isJumping = velY>0.1? true: false;
bool isFalling = velY<-0.1f? true: false;
anim.SetBool ("is jumping", is Jumping);
anim.SetBool ("isfalling", isFalling);
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.RightArrow)) {
move ();
} else {
anim.SetBool ("walk", false);
}
if (Input.GetKeyDown (KeyCode.Space)) {
jump ();
} else if (Input.GetKey (KeyCode.Z)) {
attack ();
} else if (Input.GetKey (KeyCode.X)) {
gun ();
}
}
void jump () {
isGrounded = Physics2D.Linecast (transform.position + transform.up * 1, transform.position --transform.up * 1f, groundLayer);
Debug.DrawLine (transform.position + transform.up * 1, transform.position --transform.up * 1f, Color.red);
if (Input.GetKey (KeyCode.Space)) {
if (isGrounded) {
anim.SetBool ("walk", false);
anim.SetTrigger ("jump");
isGrounded = false;
rb2d.AddForce (Vector2.up * jumpPower);
}
}
}
void move () {
Vector2 direction = transform.localScale;
if (Input.GetKey (KeyCode.RightArrow)) {
direction.x = 6;
anim.SetBool ("walk", true);
rb2d.velocity = new Vector2 (Scroll * 1 * Runspeed, rb2d.velocity.y);
} else if (Input.GetKey (KeyCode.LeftArrow)) {
direction.x = -6;
rb2d.velocity = new Vector2 (Scroll * -1 * Runspeed, rb2d.velocity.y);
anim.SetBool ("walk", true);
} else {
}
transform.localScale = direction;
}
void attack () {
if (attackNum == 0) {
anim.SetInteger ("attack", 1);
}
}
void gun () {
}
}
What I tried
Turn on/off Animator, Script, SpriteRender, BoxCollider2D, CircleCollider
Remove Component for RigidBody2D.
Remove the child object.
Unity 5.6.71f 32bit
SharpDevelop version: 5.1.0
-
Answer # 1
Related articles
- c # - in unity2d action games, the animator does not work well and i am having trouble
- c # - [unity] i was able to rotate horizontally and vertically during touch operations on the smartphone screen, but i want to m
- c # - implement nav for npc characters in unity2d environment [use navmeshagent2d version]
- c # - with prefab, suddenly you can not drag and drop into variables
- c # - unity2d transfer to destination
- c # - i want to prevent infinite jump with unity2d
- unity2d c # lightening hit judgment in shooting games
- action games always jumping in unity c #
- c # - i want to create a hover action using a gradient in wpf
- c # - about action games (unity 2d platformer controller) in unity
- c # - unity 2d action game i want to avoid slipping on a slope
- c # - i want to rotate the z axis 90 degrees with unity2d
- c # - pun2 suddenly destroy object
- i want to have an argument in action of c # queue
- c # - unity 2d action i want to make the enemy blow away when attacking
- c # - in unity2d, getkeydown processing may or may not be performed as intended
- c # - unity 2d action game when you fall without jumping, the fall becomes slow
- c # - the character image suddenly disappears and the hit judgment disappears
Related questions
- c # - player production in unity (shooting game)
- c # - [unity] format exception: input string was not in a correct format error
- c # - i'm trying not to pick up an item when i have it, but if i pick up the item again, it can be used twice
- c # - unity) fb storage android device test doesn't work
- c # - collision detection between the stage and the car is not possible
- c # - i want the player to follow the camera (3d)
- c # - i want to display at the previous scroll position when reopening in the scroll view of unity
- c # - the car with wheelcollider does not hit the stage
- c # - how to get components in unity
- c # - moving the unity scene doesn't switch as intended
What if I turn off animator in Inspector?
If the position animation is set with anim.SetBool ("walk", true) ;, the positoin may be forcibly changed even if you move it.