Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PsychonicJoe · Jun 24, 2021 at 05:36 PM · rigidbody2dvelocity

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;
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges