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 /
  • Help Room /
avatar image
0
Question by bui_krisztian · Feb 01, 2018 at 05:42 PM · collisionrigidbodymoveposition

Moving a cube with RigidBody. MovePostition, it randomly stops moving

When I press the W key, the cube starts going forward, but sometimes it suddenly stops. When I press the W or S key, it doesn't move. It only starts moving when I rotate the cube, but it can stuck again. What causing this issue?

I control the cube in the Ember class. using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Emberscript : MonoBehaviour {
 
     public class Ember
     {
         public Transform body;
         //public Transform lArm, rArm;
         public Vector3 movingVector = new Vector3(0, 0, 0);
         public int rotation = 0;
         public bool falling = false;
         public Rigidbody rigidbody;
 
         public Ember(Transform body, Transform lArm, Transform rArm)
         {
             this.body = body;
             //this.lArm = lArm;
             //this.rArm = rArm;
         }
 
         public void Move()
         {
             rigidbody.MovePosition(rigidbody.position + Quaternion.Euler(0, rotation, 0) * movingVector * Time.fixedDeltaTime);
 
             body.rotation = Quaternion.Euler(0, rotation, 0);
         }
 
         public void Jump()
         {
             rigidbody.AddForce(Vector3.up * Mathf.Sqrt(2 * -2f * Physics.gravity.y), ForceMode.VelocityChange);
         }
     }
 
     void UserInput()
     {
         if (Input.GetKeyDown(KeyCode.W))
         {
             ember.movingVector += new Vector3(0, 0, 5f);
         }
         if (Input.GetKeyUp(KeyCode.W))
         {
             ember.movingVector -= new Vector3(0, 0, 5f);
         }
         if (Input.GetKeyDown(KeyCode.S))
         {
             ember.movingVector += new Vector3(0, 0, -5f);
         }
         if (Input.GetKeyUp(KeyCode.S))
         {
             ember.movingVector -= new Vector3(0, 0, -5f);
         }
         if (Input.GetKey(KeyCode.A))
         {
             ember.rotation += -1;
         }
         if (Input.GetKey(KeyCode.D))
         {
             ember.rotation += 1;
         }
         if (Input.GetKeyDown(KeyCode.Space))
         {
             ember.Jump();
         }
     }
 
     void SpawnCircle(float r)
     {
         float cubeLength = 2 * r * Mathf.PI / 360;
         for (int i = 0; i < 360; i++)
         {
             GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
             cube.transform.position = Quaternion.Euler(0, i, 0) * new Vector3(0, 0, r);
             cube.transform.localScale = new Vector3(cubeLength + 0.05f, 1f, 1f);
             cube.transform.rotation = Quaternion.Euler(0, i, 0);                
         }
     }
 
     Ember ember;
     public Transform lArm, rArm;
     int numberOfCircles = 0;
 
     void Start () {
         ember = new Ember(transform,lArm,rArm);
         ember.rigidbody = ember.body.GetComponent<Rigidbody>();
     }
     
     void Update () {
         UserInput();
 
         if(Vector3.Distance(ember.body.position, Vector3.zero) - numberOfCircles * 5 > 0)
         {
             numberOfCircles++;
             SpawnCircle(numberOfCircles * 5);
         }
 
         Debug.Log(ember.rigidbody.position + Quaternion.Euler(0, ember.rotation, 0) * ember.movingVector);
     }
 
     void FixedUpdate()
     {
         ember.Move();
     }
 }
 
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

184 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 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

Can I assign a collider to an explosion? If not, how can i detect if the player is inside an explosion radius? 2 Answers

Restrict held object movement 0 Answers

Position and collider seem to incorrectly shift when moving object 0 Answers

how to prevent a kinematic rigidbody from going through static colliders 0 Answers

Flying character rigidbody - can't see any static collider 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