My problem is for a wall jump.
I basically want my wall jump to have a bit of force away from to wall to make it look and feel better than just jumping up straight.
The problem is, when I add a force on the x axis, it does not register when I am also holding the arrow towards the wall. If I am not holding down the walking button, then press jump, then the x force works.
I have a feeling that the horizontal axis force is competing with the opposite force that I want to add to the player when he hits the jump button.
Walking code:
private void ApplyMovement()
{
if (isGrounded)
{
myRigidbody.velocity = new Vector2(movementSpeed * movementInputDirection, myRigidbody.velocity.y);
}
else if (!isGrounded && !isWallSliding && movementInputDirection != 0)
{
Vector2 forceToAdd = new Vector2(movementForceInAir * movementInputDirection, 0);
myRigidbody.AddForce(forceToAdd);
if (Mathf.Abs(myRigidbody.velocity.x) > movementSpeed)
{
myRigidbody.velocity = new Vector2(movementSpeed * movementInputDirection, myRigidbody.velocity.y);
}
}
else if (!isGrounded && !isWallSliding && movementInputDirection == 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x * airDragMultiplier, myRigidbody.velocity.y);
}
if (isWallSliding)
{
if (myRigidbody.velocity.y < -wallSlideSpeed)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, -wallSlideSpeed);
}
}
}
Jump code:
private void Jump()
{
if (canJump && !isWallSliding)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
numberOfJumpsLeft--;
}
else if ((isWallSliding || isTouchingWall) && !isGrounded && canJump)
{
isWallSliding = false;
numberOfJumpsLeft--;
Vector2 forceToAdd = new Vector2(wallPushForce * -facingDirection, wallJumpForce *
wallJumpDirection.y);
myRigidbody.AddForce(forceToAdd, ForceMode2D.Impulse);
}
}
Related
I wrote a code for an enemy that, when he sees my character, will chase him. But when he sees my character and chases him, he goes through objects.There is a box collider 2D on it, but it moves with it. I've already tried everything, I don't know how to fix it. Can you please help fix
Here is my code:
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
idleSpeed = speed;
barDelta = Vector3.Distance(parent.position, barPoint.position);
bar = UIManager.AddEnemy(this);
}
void Update()
{
if (Vector2.Distance(transform.position, point.position) < positionOfPatrol && angry == false)
{
idle = true;
}
if (Vector2.Distance(transform.position, player.position) < stoppingDistance)
{
angry = true;
idle = false;
goBack = false;
}
if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
{
goBack = true;
angry = false;
}
if (idle == true)
{
Idle();
}
else if (angry == true)
{
Angry();
}
else if (goBack == true)
{
GoBack();
}
}
void Idle()
{
if (transform.position.x > point.position.x + positionOfPatrol)
{
moveingRight = false;
}
else if (transform.position.x < point.position.x - positionOfPatrol)
{
moveingRight = true;
}
if (moveingRight)
{
if (!facingRight) Flip();
facingRight = true;
transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
}
else
{
if (facingRight) Flip();
facingRight = false;
transform.position = new Vector2(transform.position.x - speed * Time.deltaTime, transform.position.y);
}
}
void Angry()
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
speed = idleSpeed + 1;
}
void GoBack()
{
transform.position = Vector2.MoveTowards(transform.position, point.position, speed * Time.deltaTime);
speed = idleSpeed;
}
}
You should try to use a rigidbody with the collider. The collider does not actually do physics, that is what the rigidbody is for. If you add a rigidbody and set its velocity instead of setting the transform's position, it will not only detect collisions with walls but it will stop moving and do all of the physics you would expect.
It started happening after I wrote the script for dashing. Now, whenever I dash into walls, my character gets stuck to that wall. If I dash towards the roof, then I can walk on the roof for example. Also, when I dash next to a wall / on the floor, it never stops dashing after a single press. Any help will be appreciated, thanks.
The code below is the dashing script, if needed I can post the variables set and my character controller script too.
void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
dashTime = baseDashTime;
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(feet.position, checkRadius, checkGround);
if(isGrounded == true)
{
dashTime = baseDashTime;
}
if (direction == 0)
{
if (Input.GetKey(KeyCode.W) && (Input.GetKeyDown(KeyCode.LeftShift)))
{
direction = 1;
}
else if (Input.GetKey(KeyCode.A) && (Input.GetKeyDown(KeyCode.LeftShift)))
{
direction = 2;
}
else if (Input.GetKey(KeyCode.S) && (Input.GetKeyDown(KeyCode.LeftShift)))
{
direction = 3;
}
else if (Input.GetKey(KeyCode.D) && (Input.GetKeyDown(KeyCode.LeftShift)))
{
direction = 4;
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
_rigidbody.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
_rigidbody.velocity = Vector2.up * dashSpeed;
}
else if (direction == 2)
{
_rigidbody.velocity = Vector2.left * dashSpeed;
}
else if (direction == 3)
{
_rigidbody.velocity = Vector2.down * dashSpeed;
}
else if (direction == 4)
{
_rigidbody.velocity = Vector2.right * dashSpeed;
}
}
}
}
In your code you have this statement in the update function. When you dash on the ground, im guessing IsGrounded will stay true, so the player will keep dashing forever. This is probably what causes the player to be stuck.
if(isGrounded == true)
{
dashTime = baseDashTime;
}
https://pasteboard.co/IrKZbCV.png
void OnCollisionEnter2D(Collision2D col)
{
float angle = Vector3.Angle(Vector3.up, col.contacts[0].normal);
if (col.transform.gameObject.tag == "enemyWalk")
{
Physics2D.IgnoreCollision(col.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>(), true);
}
else
{
Physics2D.IgnoreCollision(col.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>(), false);
}
if (col.transform.gameObject.tag == "stone")
{
angle = Vector3.Angle(Vector3.up, col.contacts[0].normal);
if (Mathf.Approximately(angle, 0))
{
isGround = true;
taş = true;
}
}
else if (col.transform.gameObject.tag == "ground")
{
angle = Vector3.Angle(Vector3.up, col.contacts[0].normal);
if (Mathf.Approximately(angle, 0))
{
isGround = true;
çimen = true;
}
}
}
if ((isJump.jmp || Input.GetKeyDown(KeyCode.Space)) && !isDeath && isGround)
{
rgd.AddForce(new Vector2(0, 2.750f), ForceMode2D.Impulse);
jumpAudio.Play();
isGround = false;
}
İ want to jump my character only one times when i press jump key,but as i show this situation on picture,sometimes my character jumping too high when i jumped at near of the bottom of the rock.İ mean,as if my jump code working many times at same time.
i'm waiting for your helps.
Hello stack overflow users! I was creating a game in unity 2018.2.2f1. All went great, but i missplaced the order of code lines in the program --> which caused a infite while loop appearing. I didn't know it would be infinite so I runned the program. Now my unity window is unselectable when i click on the icon, and when i enter the window, everything is frozen. I would close the program trough task manager, but i didnt save my files and i can't even save the project. Is there any way for me to save the project when it is in this state? My unity didn't crash, it just froze.
Here is my FIXED c# code if that somehow helps (and i am in visual studio):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
public float speed = 10f;
private Vector3 velocity = new Vector3();
private string direction = "none";
private bool canMoveAgain = true;
private bool go = false;
// Use this for initialization
void Start () {
speed = 10f;
}
// Update is called once per frame
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.name == "wall (3)" && direction == "down")
{
go = false;
}
if (collision.collider.name == "wall (2)" && direction == "up")
{
go = false;
}
if (collision.collider.name == "wall" && direction == "left")
{
go = false;
}
if (collision.collider.name == "wall (1)" && direction == "right")
{
go = false;
}
}
void Update () {
if (Input.GetKeyDown(KeyCode.DownArrow) && canMoveAgain)
{
direction = "down";
go = true;
canMoveAgain = false;
while (go)
{
velocity = new Vector3(0,-1,0);
transform.position += velocity;
}
velocity = new Vector3(0, 0, 0);
direction = "none";
} else if (Input.GetKeyDown(KeyCode.UpArrow) && canMoveAgain)
{
direction = "up";
go = true;
canMoveAgain = false;
while (go)
{
velocity = new Vector3(0, +1, 0);
transform.position += velocity;
}
velocity = new Vector3(0, 0, 0);
direction = "none";
} else if (Input.GetKeyDown(KeyCode.LeftArrow) && canMoveAgain)
{
direction = "left";
go = true;
canMoveAgain = false;
while (go)
{
velocity = new Vector3(-1, 0, 0);
transform.position += velocity;
}
velocity = new Vector3(0, 0, 0);
direction = "none";
} else if (Input.GetKeyDown(KeyCode.RightArrow) && canMoveAgain)
{
direction = "right";
go = true;
canMoveAgain = false;
while (go)
{
velocity = new Vector3(+1, 0, 0);
transform.position += velocity;
}
velocity = new Vector3(0, 0, 0);
direction = "none";
}
}
}
You do
while (go)
{
velocity = new Vector3(+1, 0, 0);
transform.position += velocity;
}
but go is never set to false inside the loop.
OnCollisionEnter is never executed since the while is still being executed => freeze.
since Update is called each frame anyway you should rather move that part to a separate if(go) block after checking the Inputs ..
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.name == "wall (3)" && direction == "down"
|| collision.collider.name == "wall (2)" && direction == "up"
|| collision.collider.name == "wall" && direction == "left"
|| collision.collider.name == "wall (1)" && direction == "right")
{
go = false;
velocity = Vector3.zero;
direction = "none"
}
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.DownArrow) && canMoveAgain)
{
direction = "down";
go = true;
canMoveAgain = false;
velocity = -Vector3.up;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && canMoveAgain)
{
direction = "up";
go = true;
canMoveAgain = false;
velocity = Vector3.up;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow) && canMoveAgain)
{
direction = "left";
go = true;
canMoveAgain = false;
velocity = -Vector3.right;
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && canMoveAgain)
{
direction = "right";
go = true;
canMoveAgain = false;
velocity = Vector3.right;
}
if(go)
{
transform.position += velocity;
}
}
Note that currently your code is also frame rate dependent. You might want to rather use something like
public float moveSpeed;
...
transform.position += velocity * moveSpeed * Time.deltaTime;
For the direction I would rather recommend an enum like
private enum MoveDirection
{
None,
Up,
Down,
Left,
Right
}
and use it e.g. like
direction = MoveDirection.Up;
and
if(direction == MoveDirection.Up)
which is more efficient than comparing strings.
Note: Typed on smartphone so no warranty but I hope the idea gets clear
First of all. Do not close Unity.
You got two things you can try to do.
Luckily your code has a backup when you press run.
It is inside the Temp folder. The Temp folder will be replaced if you press run again. Copy the project to somewhere safe.
Attach a Debugger, this can be either Visual Studio or MonoDevelop. Once they are attached to Unity just click pause. And you're free to save your project.
I hope you find this answer before it's too late.
My intention is to make my character wall jump/climb/slide, I got the sliding part working fine, but if he jumps while wall sliding, he should "bounce" back to the wall, the problem is that I can't balance the forces. In all tutorial I saw, it is simply a matter of detecting if the character is wall sliding, and if he is and he jumps, then you add a force oposite to the wall.
This is not working for me, because if I add enough force to make him jump, he goes way too fast and the player can barely see he jumped, he just sees that the character is now higher on the wall. If I add a smaller amount of force, it isn't enough to make a considerable jump and the player would have to hit space a thousand times to make him go up a few centimeters on the wall.
Any help is appreciated, I already tried a lot of things, even tried to freeze the controls, set gravity scale to 0 and make the character fo to the right points using MoveTowards, that is how desperate I am.
I'm also really new to Unity so I might be missing something really simple.
Here is a gif showing the character's behavior:
https://imgur.com/a/TgUHzP6
And here is the relevant parts of my character's script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TheBot : MonoBehaviour {
public float speed;
public int jumpForce;
public Transform groundCheck;
public Transform meleeCheck;
public Transform bulletSpawner;
public LayerMask layerGround;
public float meleeCoolDown;
public float meleeDamage;
private Rigidbody2D body;
private Animator anim;
private Dash dashController;
private Shooter shotController;
private float unloadWaitingTime = 3;
private float idleGunTime = 0;
private bool facingRight = true;
private bool onGround = true;
private bool jumping = false;
private bool attacking = false;
private bool dead = false;
private bool isGunLoaded = false;
private bool isGunLoading = false;
private bool isGunUnloading = false;
private bool takingDamage = false;
private bool dashing = false;
private bool isWallSliding = false;
private float wallJumpTime = 0f;
private Vector3[] wallJumpControlPoint;
// Use this for initialization
void Start () {
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
dashController = GetComponent<Dash>();
shotController = GetComponent<Shooter>();
}
// Update is called once per frame
void Update () {
PlayAnimations();
CheckIfGrounded();
checkIfWallSliding();
dashing = dashController.IsDashing();
if (Input.GetButtonDown("Jump") && (onGround || isWallSliding) && !isGunLoading && !jumping && !takingDamage){
jumping = true;
wallJumpControlPoint = new Vector3[3];
wallJumpControlPoint[0] = body.position;
wallJumpControlPoint[1] = new Vector3(body.position.x +4, body.position.y + 2);
wallJumpControlPoint[2] = new Vector3(body.position.x, body.position.y + 4);
}
if (Input.GetButtonDown("Melee") && !attacking && !isGunLoading){
Attack();
}
if(Input.GetButtonDown("Ranged") && !attacking && !isGunLoading && onGround){
Shoot();
}
if(Input.GetButtonDown("Dash") && !attacking && !isGunLoading && onGround){
dashController.DashTo(facingRight? Dash.RIGHT : Dash.LEFT);
}
if(isGunLoaded){
idleGunTime += Time.deltaTime;
if (idleGunTime >= unloadWaitingTime){
UnloadGun();
}
}
}
void FixedUpdate(){
if(!takingDamage){
float move = Input.GetAxis("Horizontal");
//while charachter is wall sliding, slowly fall
if (isWallSliding){
body.velocity = new Vector2(body.velocity.x, -0.7f);
}
if(!dashing){
if(onGround){
//if not dashing on on ground, walk with normal speed
body.velocity = new Vector2(move * speed, body.velocity.y);
} else {
//if character is not on ground, reduce the speed so he doesn't jump too far away
body.velocity = new Vector2(move * (speed * 0.7f), body.velocity.y);
}
}
if((move < 0 && facingRight) || (move > 0 && !facingRight) ){
//control direction character is facing
Flip();
}
if (jumping){
if(isWallSliding){
body.velocity = new Vector2(30, 20);
} else {
body.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
}
if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow)){
//if is moving while jumping, reduce jump height
body.velocity = new Vector2(body.velocity.x, body.velocity.y*0.8f);
}
onGround = false;
jumping = false;
}
}
}
void CheckIfGrounded(){
onGround = false;
Collider2D[] collisionResults = new Collider2D[2];
int objectsBeneath = Physics2D.OverlapBoxNonAlloc(groundCheck.position, new Vector2(0.9f, 0.3f), 0.0f, collisionResults, layerGround);
for (int i=0; i <objectsBeneath; i++ ){
if (!GameObject.ReferenceEquals(gameObject, collisionResults[i].gameObject)){
onGround = true;
}
}
}
void checkIfWallSliding(){
if (!onGround){
RaycastHit2D[] ray = new RaycastHit2D[1];
int totalRayHits = Physics2D.LinecastNonAlloc(bulletSpawner.position, body.position, ray, 1 << LayerMask.NameToLayer("SolidGround"));
bool wallFound = totalRayHits > 0 && ray[0].collider.gameObject.tag == "SolidGround";
isWallSliding = wallFound && ( (facingRight && Input.GetKey(KeyCode.RightArrow)) || (!facingRight && Input.GetKey(KeyCode.LeftArrow))) ;
} else {
isWallSliding = false;
if (body.velocity.y > 10){
body.velocity = new Vector2(body.velocity.x, 5);
}
}
}
public void Die(){
dead = true;
}
}
As you've attempted before, you will need to reduce your horizontal jump acceleration/velocity on the jump.
When you wall jump, you'll be pressing towards the wall. And as your code is currently, while you are in the air, your horizontal velocity is set to be in the direction you press. This makes any horizontal movement from the wall jump very hard to see, unless it's large enough to push you very far in one frame.
This (as well as the change we discussed in the comments) is why your previous attempts with low walljump magnitudes didn't work.
To fix this, you have to change how your air control works. One way of going about it is making it add a clamped modifier to your horizontal velocity instead of setting it directly to the target velocity.
if(!dashing){
if(onGround){
//if not dashing on on ground, walk with normal speed
body.velocity = new Vector2(move * speed, body.velocity.y);
} else {
//if character is not on ground, reduce the speed so he doesn't jump too far away
float airControlAccelerationLimit = 0.5f; // Higher = more responsive air control
float airSpeedModifier = 0.7f; // the 0.7f in your code, affects max air speed
float targetHorizVelocity = move
* speed
* airSpeedModifier; // How fast we are trying to move horizontally
float targetHorizChange = targetHorizVelocity
- body.velocity.x; // How much we want to change the horizontal velocity
float horizChange = Mathf.Clamp(
targetHorizChange ,
-airControlAccelerationLimit ,
airControlAccelerationLimit ); // How much we are limiting ourselves
// to changing the horizontal velocity
body.velocity = new Vector2(body.velocity.x + horizChange, body.velocity.y);
}
}
Here it is, in your code, along with making sure we only update velocity once OR use AddForce per FixedUpdate call. And we also change the wallsliding slowdown code to only activate if the player is about to go down faster than the wallslide speed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TheBot : MonoBehaviour {
public float speed;
public int jumpForce;
public Transform groundCheck;
public Transform meleeCheck;
public Transform bulletSpawner;
public LayerMask layerGround;
public float meleeCoolDown;
public float meleeDamage;
private Rigidbody2D body;
private Animator anim;
private Dash dashController;
private Shooter shotController;
private float unloadWaitingTime = 3;
private float idleGunTime = 0;
private bool facingRight = true;
private bool onGround = true;
private bool jumping = false;
private bool attacking = false;
private bool dead = false;
private bool isGunLoaded = false;
private bool isGunLoading = false;
private bool isGunUnloading = false;
private bool takingDamage = false;
private bool dashing = false;
private bool isWallSliding = false;
private float wallJumpTime = 0f;
private Vector3[] wallJumpControlPoint;
// Use this for initialization
void Start () {
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
dashController = GetComponent<Dash>();
shotController = GetComponent<Shooter>();
}
// Update is called once per frame
void Update () {
PlayAnimations();
CheckIfGrounded();
checkIfWallSliding();
dashing = dashController.IsDashing();
if (Input.GetButtonDown("Jump") && (onGround || isWallSliding) && !isGunLoading && !jumping && !takingDamage){
jumping = true;
wallJumpControlPoint = new Vector3[3];
wallJumpControlPoint[0] = body.position;
wallJumpControlPoint[1] = new Vector3(body.position.x +4, body.position.y + 2);
wallJumpControlPoint[2] = new Vector3(body.position.x, body.position.y + 4);
}
if (Input.GetButtonDown("Melee") && !attacking && !isGunLoading){
Attack();
}
if(Input.GetButtonDown("Ranged") && !attacking && !isGunLoading && onGround){
Shoot();
}
if(Input.GetButtonDown("Dash") && !attacking && !isGunLoading && onGround){
dashController.DashTo(facingRight? Dash.RIGHT : Dash.LEFT);
}
if(isGunLoaded){
idleGunTime += Time.deltaTime;
if (idleGunTime >= unloadWaitingTime){
UnloadGun();
}
}
}
void FixedUpdate(){
if(!takingDamage){
float move = Input.GetAxis("Horizontal");
//while charachter is wall sliding, slowly fall
if (isWallSliding && !jumping && body.velocity.y < -0.7f){
body.velocity = new Vector2(body.velocity.x, -0.7f)
}
if(!dashing){
if(onGround){
//if not dashing on on ground, walk with normal speed
body.velocity = new Vector2(move * speed, body.velocity.y);
} else {
//if character is not on ground, reduce the speed so he doesn't jump too far away
float airControlAccelerationLimit = 0.5f; // Higher = more responsive air control
float airSpeedModifier = 0.7f; // the 0.7f in your code, affects max air speed
float targetHorizVelocity = move
* speed
* airSpeedModifier; // How fast we are trying to move horizontally
float targetHorizChange = targetHorizVelocity
- body.velocity.x; // How much we want to change the horizontal velocity
float horizChange = Mathf.Clamp(
targetHorizChange ,
-airControlAccelerationLimit ,
airControlAccelerationLimit ); // How much we are limiting ourselves
// to changing the horizontal velocity
body.velocity = new Vector2(body.velocity.x + horizChange, body.velocity.y);
}
}
if((move < 0 && facingRight) || (move > 0 && !facingRight) ){
//control direction character is facing
Flip();
}
if (jumping){
if(isWallSliding){
body.velocity = new Vector2(body.velocity.x + 0.25f * jumpForce, jumpForce);
} else {
body.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
}
if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow)){
//if is moving while jumping, reduce jump height
body.velocity = new Vector2(body.velocity.x, body.velocity.y*0.8f);
}
onGround = false;
jumping = false;
}
}
}
void CheckIfGrounded(){
onGround = false;
Collider2D[] collisionResults = new Collider2D[2];
int objectsBeneath = Physics2D.OverlapBoxNonAlloc(groundCheck.position, new Vector2(0.9f, 0.3f), 0.0f, collisionResults, layerGround);
for (int i=0; i <objectsBeneath; i++ ){
if (!GameObject.ReferenceEquals(gameObject, collisionResults[i].gameObject)){
onGround = true;
}
}
}
void checkIfWallSliding(){
if (!onGround){
RaycastHit2D[] ray = new RaycastHit2D[1];
int totalRayHits = Physics2D.LinecastNonAlloc(bulletSpawner.position, body.position, ray, 1 << LayerMask.NameToLayer("SolidGround"));
bool wallFound = totalRayHits > 0 && ray[0].collider.gameObject.tag == "SolidGround";
isWallSliding = wallFound && ( (facingRight && Input.GetKey(KeyCode.RightArrow)) || (!facingRight && Input.GetKey(KeyCode.LeftArrow))) ;
} else {
isWallSliding = false;
}
}
public void Die(){
dead = true;
}
}