- Home /
rigidbody2d.velocity acting weirdly
I have this bull enemy that I want to charge at (where the player was) until it reaches the border. Then it's supposed to slowly move towards the player (away from the border) to prepare for another attack, but that doesn't happen. I'm using rigidbody2d.velocity to control the bull as it is kinematic. But for some reason even though the direction vectors point towards the player the bull flies off in a weird direction once it reach the map border.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bull_move : MonoBehaviour
{
public Rigidbody2D rb;
public SpriteRenderer rend;
public float chargeSpeed;
public float chargeAcceleration;
public string playerObjectName;
GameObject player;
public LayerMask playerLayer;
Vector3 targetPosition;
Vector3 attackDirection;
Vector2 attackDirection2D;
Vector2 playerPosition2D;
bool isFlipped;
public float baseTimer = 1.5f;
float bullTimer;
bool readjusting;
float readjustBaseTimer = 1f;
float readjustTimer = 1f;
public float readjustSpeed = 1f;
bool outOfBounds = false;
void Start()
{
bullTimer = baseTimer;
player = GameObject.Find(playerObjectName);
}
void FixedUpdate()
{
if (isFlipped)
{
rend.flipX = true;
}
else
{
rend.flipX = false;
}
if (!GetComponent<enemyBase>().dead & !readjusting)
{
// setting vectors for dashing at previous player location
if (bullTimer > baseTimer/3)
{
targetPosition = player.transform.position;
attackDirection = Vector3.Normalize(targetPosition - this.transform.position);
attackDirection2D = new Vector2(attackDirection.x, attackDirection.y);
if (targetPosition.x < this.transform.position.x)
{
isFlipped = true;
}
else
{
isFlipped = false;
}
}
// time delay before starting new attack
bullTimer = TimeDelay(bullTimer);
if (bullTimer <= 0)
{
rb.velocity = (attackDirection2D * Accelerate(chargeSpeed, chargeAcceleration));
}
}
// play area border coords
outOfBounds = !IsBetween(this.transform.position.x, -5.3f, 7.2f) || !IsBetween(this.transform.position.y, -3.5f, 3.5f);
if (outOfBounds & !readjusting)
{
StopAttack();
}
if (readjusting)
{
if (readjustTimer > 0)
{
playerPosition2D = new Vector2(player.transform.position.x, player.transform.position.y);
Debug.Log(playerPosition2D);
rb.velocity = playerPosition2D;
Debug.Log(rb.velocity);
Debug.Log("readjusting");
readjustTimer -= Time.deltaTime;
}
else
{
bullTimer = baseTimer;
readjustTimer = readjustBaseTimer;
readjusting = false;
}
}
}
float TimeDelay(float delay)
{
float timer = delay;
if (timer > 0)
{
timer -= Time.deltaTime;
}
return timer;
}
float Accelerate(float baseValue, float Acc)
{
// not working currently
float momentum = baseValue + (Time.deltaTime * Acc);
momentum = momentum + (Time.deltaTime * Acc);
return momentum;
}
bool IsBetween(float value, float b1, float b2)
{
return ((value > b1) & (value < b2));
}
void StopAttack()
{
rb.velocity = new Vector2(0, 0);
readjusting = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613035445im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to make an 2d object move the direction its facing using rigidbody2d.velcoity 1 Answer
Unpausing While Keeping Values 0 Answers
Adding speed relative to move vector (Rigidbody2D) 1 Answer
Need help for my Arkanoid-like game. i got problem to make my ball reflects on the wall 1 Answer
Velocity doesnt change properly 1 Answer