Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 zlade25 · Jul 07, 2018 at 11:12 PM · 2d gameunity 2d

Why does a rigidbody won't move on my EnemyMovement class?

This is my EnemyMovement.cs

public class EnemyMovement{

 public const int MAX_SWAP_DIRECTION_DURATION = 2;
 public const int MAX_WALK_DURATION = 15;
 public static string[] HORIZONTAL_MOVEMENT = new string[] {"LEFT", "RIGHT"};
 public static string[] VERTICAL_MOVEMENT = new string[] {"TOP", "BOTTOM"};

 private float walkSpeed, jumpSpeed, walkDur, swapDirDur;
 private string side, dir;
 private int movStat;
 private bool jumpStatus;

 public EnemyMovement(){}

 public void SetCurrentSide(string side){
     this.side = side;
 }

 public string GetCurrentSide(){
     return side;
 }

 public void SetCurrentDirection(string dir){
     this.dir = dir;
 }

 public string GetCurrentDirection(){
     return dir;
 }

 public void SetWalkSpeed(float walkSpeed){
     this.walkSpeed = walkSpeed;
 }

 public float GetWalkSpeed(){
     return walkSpeed;
 }
     
 public void SetJumpSpeed(float jumpSpeed){
     this.jumpSpeed = jumpSpeed;
 }

 public float GetJumpSpeed(){
     return jumpSpeed;
 }

 public void SetWalkDur(float walkDur){
     this.walkDur = walkDur;
 }

 public float GetWalkDur(){
     return walkDur;
 }

 public void SetSwapDirDur(float swapDirDur){
     this.swapDirDur = swapDirDur;
 }

 public float GetSwapDirDur(){
     return swapDirDur;
 }

 public void SetJumpStatus(bool jumpStatus){
     this.jumpStatus = jumpStatus;
 }

 public bool IsJump(){
     return jumpStatus;
 }

 public void Jump (GameObject ball, Rigidbody2D rBody, Transform transform){
     Vector3 targetdir = ball.transform.position - transform.position;
     transform.rotation= Quaternion.LookRotation(Vector3.forward,targetdir);
     rBody.AddForce(transform.up * jumpSpeed * Time.deltaTime, ForceMode2D.Impulse);
     transform.eulerAngles = new Vector3(0, 0, 0);
     rBody.constraints = RigidbodyConstraints2D.FreezeRotation;
 }

 public void Move(Rigidbody2D rBody, Transform transform){
     walkDur -= Time.deltaTime;
     swapDirDur -= Time.deltaTime;

     if(swapDirDur <= 0)
         swapDirDur = Random.Range (0, MAX_SWAP_DIRECTION_DURATION);

     if (walkDur <= 0) {
         jumpStatus = true;
         walkDur = Random.Range (0, MAX_WALK_DURATION);
     }

     switch (side) {
     case "Top":
         Movement ("Horizontal", rBody, transform);    
         break;
     case "Bottom":
         Movement ("Horizontal", rBody, transform);
         break;
     case "Right":
         Movement ("Vertical", rBody, transform);
         break;
     case "Left":
         Movement ("Vertical", rBody, transform);
         break;
     }
 }

 private void Movement(string direction, Rigidbody2D rBody, Transform transform){
     switch(direction){
     case "Horizontal":
         rBody.constraints = RigidbodyConstraints2D.None;
         rBody.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezeRotation;
         if (swapDirDur <= 0.0f) {
             swapDirDur = Random.Range (0, MAX_SWAP_DIRECTION_DURATION);
             movStat = Random.Range (0, HORIZONTAL_MOVEMENT.Length);
         } else {
             string horMov = HORIZONTAL_MOVEMENT [movStat];
             dir = horMov;
             switch (horMov) {
             case "LEFT":
                 //rBody.velocity = -transform.right * speed * Time.fixedDeltaTime;
                 rBody.MovePosition (transform.position - transform.right * walkSpeed * Time.fixedDeltaTime);
                 break;
             case "RIGHT":
                 //rBody.velocity = transform.right * speed * Time.fixedDeltaTime;
                 rBody.MovePosition (transform.position + transform.right * walkSpeed * Time.fixedDeltaTime);
                 break;
             }
         }
         break;
     case "Vertical":
         rBody.constraints = RigidbodyConstraints2D.None;
         rBody.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
         if (swapDirDur <= 0.0f) {
             swapDirDur = Random.Range (0, MAX_SWAP_DIRECTION_DURATION);
             movStat = Random.Range (0, VERTICAL_MOVEMENT.Length);
         } else {
             string verMov = VERTICAL_MOVEMENT [movStat];
             dir = verMov;
             switch (verMov) {
             case "TOP":
                 rBody.MovePosition (transform.position + transform.up * walkSpeed * Time.fixedDeltaTime);
                 break;
             case "BOTTOM":
                 rBody.MovePosition (transform.position - transform.up * walkSpeed * Time.fixedDeltaTime);
                 break;
             }
         }
         break;
     }
 }




And this is my enemy script that attach to then enemy object.

public class Enemy : MonoBehaviour {

 private EnemyMovement movement;

 private Rigidbody2D rBody;

 public GameObject ball;

 //Temporary
 private Text walk;
 private Text swap;
 private Text x;
 private Text y;
 private Text z;
 private Text jump;
 private Text direction;
 private Text sides;
 private Text speeds;

 // Use this for initialization
 void Start () {
     rBody = GetComponent<Rigidbody2D> ();
     movement = new EnemyMovement ();
     movement.SetWalkDur (Random.Range (0, EnemyMovement.MAX_WALK_DURATION));
     movement.SetSwapDirDur (Random.Range (0, EnemyMovement.MAX_SWAP_DIRECTION_DURATION));

     //Temp
     walk = GameObject.Find ("Walk").GetComponent<Text> ();
     swap = GameObject.Find ("Swap").GetComponent<Text> ();
     x = GameObject.Find ("X").GetComponent<Text> ();
     y = GameObject.Find ("Y").GetComponent<Text> ();
     z = GameObject.Find ("Z").GetComponent<Text> ();
     jump = GameObject.Find ("Jump").GetComponent<Text> ();
     direction = GameObject.Find ("Direction").GetComponent<Text> ();
     sides = GameObject.Find ("Side").GetComponent<Text> ();
     speeds = GameObject.Find ("Speed").GetComponent<Text> ();
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     if (movement.IsJump()) {
         movement.Jump (ball, rBody, transform);
     } else {
         movement.Move (rBody, transform);
     }

     walk.text = "WalkDur: " + movement.GetWalkDur();
     swap.text = "SwapDur: " + movement.GetSwapDirDur();
     x.text = "X: " + transform.position.x;
     y.text = "Y: " + transform.position.y;
     z.text = "Z: " + transform.position.z;
     jump.text = "Jump Status: " + movement.IsJump();
     direction.text = "Direction: " + movement.GetCurrentDirection();
     sides.text = "Sides: " + movement.GetCurrentSide();
     speeds.text = "Speed: " + movement.GetWalkSpeed();
 }

 void OnCollisionEnter2D(Collision2D col){
     transform.eulerAngles = new Vector3(0, 0, 0);
     movement.SetCurrentSide(col.gameObject.name);

     if (movement.IsJump ())
         movement.SetJumpStatus (false);

     rBody.gravityScale = 0;
 }

}

Please help me. I just passed the value of rigidbody and transform on a move method but it didn't work.

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

101 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

Related Questions

GameObject.FindGameObjectWithTag not finding the tagged objects. 1 Answer

how can I ceep position z on 0 2 Answers

How can I set up Unity3D for a 2D pixel art game? 0 Answers

good tutorials for 2d action rpg?,Good tutorials on 2D action-rpg? 1 Answer

Mouse Click Walk to Idle Animations 0 Answers


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