• 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 doctor1232123 · Jul 20, 2018 at 01:13 AM · collisionmovementphysicsrigidbodyplayer

Collisions causing Rigidbody to go Haywire and move randomly.

Hello. I've been having trouble with collisions and the rigidbody. Whenever I move to a slope or Jump multiple times, the player starts to rotate and move around randomly. I've tried to find a solution online for sometime and tried options such as using Rigidbody.Addforce , but they either do not function the way I need it to or will not work at all. I'm using Transform.translate for the player's movement. The movement code : Running.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Audio;

 public class Running : MonoBehaviour
 {
 
     public float spd = 60.0f;
     public float Magnetude = 10f;
     public float top_spd = 200f;
     public float olSPD;
     static float i = 0;
     public float jumpSPD = 10.0f;
     public float jumpHieght = 50.0f;
     public float MaxHieght = 50.0f;
     public Camera cam;
 
     public Text Rings;
     private int count;
 
     public AudioSource Sound;
     public AudioSource Master;
     public AudioClip Ring_Collect;
     public AudioClip Music;
 
     private Rigidbody rb;
 
     private void Start()
     {
 
         olSPD = spd;
         i = 0;
         var cam = Camera.main;
         count = 0;
         DisplayRings();
         rb = GetComponent<Rigidbody>();
 
         Master.clip = Music;
         Master.Play();
 
 
     }
 
     void Run()
     {
 
         var x = Input.GetAxis("Horizontal") * Time.smoothDeltaTime * spd;
         var z = Input.GetAxis("Vertical") * Time.smoothDeltaTime * spd;
         var Player = GameObject.Find("PlayerObject");
         var camAngle = cam.transform.rotation.y;
         //print("CamAngle = " + camAngle);
         if (Input.GetAxis("Vertical") < 0.3 & Input.GetAxis("Horizontal") > -0.3 & Input.GetAxis("Vertical") > -0.3 & Input.GetAxis("Horizontal") < 0.3)
         {
             i = 0;
         }
         if (spd <= olSPD + 30)
         {
             if (Input.GetAxis("Vertical") >= 0.3 & Input.GetAxis("Horizontal") >= 0.3)
             {
 
 
                 this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
                 this.transform.RotateAround(this.transform.position, Vector3.up, ((camAngle) + 45));
 
 
                 transform.Translate(-z, 0, 0);
 
             }
             else if (Input.GetAxis("Vertical") >= 0.3 & Input.GetAxis("Horizontal") <= -0.3)
             {
 
 
                 this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
                 this.transform.RotateAround(this.transform.position, Vector3.up, ((camAngle) - 45));
 
 
                 transform.Translate(-z, 0, 0);
 
             }
             else if (Input.GetAxis("Vertical") <= -0.3 & Input.GetAxis("Horizontal") <= -0.3)
             {
 
 
                 this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
                 this.transform.RotateAround(this.transform.position, Vector3.up, ((camAngle) + 180 + 45));
 
 
                 transform.Translate(z, 0, 0);
 
             }
             else if (Input.GetAxis("Vertical") <= -0.3 & Input.GetAxis("Horizontal") >= 0.3)
             {
 
 
                 this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
                 this.transform.RotateAround(this.transform.position, Vector3.up, ((camAngle) + 180 - 45));
 
 
                 transform.Translate(z, 0, 0);
 
             }
             else if (Input.GetAxis("Vertical") <= -0.3)
             {
                 var dist = Vector3.Distance(this.transform.position, Player.transform.position);
                 if (i == 0)
                 {
                     this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
                     this.transform.eulerAngles = this.transform.eulerAngles + new Vector3(0, ((camAngle) + 180), 0);
                     i = 1;
                 }
                     print("1");
                     transform.Translate(z, 0, 0);
             }
 
             else if (Input.GetAxis("Vertical") >= 0.3)
             {
 
                 this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
                 this.transform.Rotate(0, (camAngle), 0);
 
                 transform.Translate(-z, 0, 0);
 
             }
             else if (Input.GetAxis("Horizontal") <= -0.3)
             {
                 this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
                 this.transform.Rotate(0, (camAngle) - 90, 0);
                 transform.Translate(x, 0, 0);
 
             }
             else if (Input.GetAxis("Horizontal") >= 0.3)
             {
                 this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
                 this.transform.Rotate(0, (camAngle) + 90, 0);
                 transform.Translate(-x, 0, 0);
 
             }
 
         }
         else if (spd >= olSPD + 30)
         {
             if (Input.GetAxis("Vertical") >= 0.3)
             {
                 transform.Rotate(0, x, 0);
                 transform.Translate(-z, 0, 0);
             }
             else if (Input.GetAxis("Vertical") <= -0.3)
             {
                 if (spd > olSPD + 10)
                 {
                     spd -= 90 * Time.deltaTime;
                 }
                 else
                 {
                     i = 0;
                 }
                 transform.Rotate(0, x, 0);
                 transform.Translate(spd * Time.deltaTime * 0.3f, 0, 0);
             }
             else if (Input.GetAxis("Horizontal") >= 0.3)
             {
 
                 transform.Rotate(0, x, 0);
                 transform.Translate(-x, 0, 0);
 
             }
             else if (Input.GetAxis("Horizontal") <= -0.3)
             {
 
                 transform.Rotate(0, x, 0);
                 transform.Translate(x, 0, 0);
 
             }
             if (Input.GetMouseButton(0))
             {
 
                 float distFromMid = -((Screen.width / 2) - Input.mousePosition.x) / 100;
                 x = distFromMid * Time.smoothDeltaTime * spd;
                 if (Input.GetAxis("Vertical") >= 0.3)
                 {
                     transform.Rotate(0, x, 0);
                     transform.Translate(-z, 0, 0);
                 }
                 else if (Input.GetAxis("Vertical") <= -0.3)
                 {
                     if (spd > olSPD + 10)
                     {
                         spd -= 90 * Time.deltaTime;
                     }
                     else
                     {
                         i = 0;
                     }
                     transform.Rotate(0, x, 0);
                     transform.Translate(spd * Time.deltaTime * 0.3f, 0, 0);
                 }
 
             }
         }
         else { i = 0; }
         if (Input.GetAxis("Vertical") < 0.3 & Input.GetAxis("Horizontal") > -0.3 & Input.GetAxis("Vertical") > -0.3 & Input.GetAxis("Horizontal") < 0.3)
         {
             i = 0;
         }
     }
 
     void Jump()
     {
 
         if(Input.GetButton("Fire1"))
         {
             var Jump = 0.0f;
             if (Jump < MaxHieght)
             {
                 Jump = Mathf.MoveTowards(Jump, MaxHieght, jumpSPD * Time.deltaTime);
             }
                 this.transform.Translate(0, Jump, 0);
         }
 
     }
 
 
 
 
     void ChangeWithTime()
     {
         if (Input.GetAxis("Vertical") > 0.3 | Input.GetAxis("Horizontal") < -0.3 | Input.GetAxis("Horizontal") > 0.3)
         {
             if (spd < top_spd)
             {
                 spd = Mathf.MoveTowards(spd, top_spd, Magnetude * Time.deltaTime);
             }
         }
         else if (spd > olSPD + 20)
         {
             spd = Mathf.MoveTowards(spd, olSPD, Magnetude * Time.deltaTime * 20);
             transform.Translate(-spd* Time.deltaTime * 0.3f, 0, 0);
         }
         else if (spd > olSPD)
         {
             spd = Mathf.MoveTowards(spd, olSPD, Magnetude * Time.deltaTime * 8);
             transform.Translate(-spd * Time.deltaTime * 0.3f, 0, 0);
         }
         else if (spd <= olSPD)
         {
             spd = olSPD;
         }
     }
     void OnTriggerEnter(Collider other)
     {
 
         if (other.gameObject.CompareTag("Collect"))
         {
             Sound.clip = Ring_Collect;
             Sound.Play(0);
             count += 1;
             DisplayRings();
             other.gameObject.SetActive(false);
         }
 
 
 
     }
 
     void DisplayRings()
     {
 
         Rings.text = "Rings: " + count;
 
     }
 
 
     void Update()
     {
 
         Run();
         ChangeWithTime();
         Jump();
 
     }
 }

Thank you.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Jul 20, 2018 at 07:47 AM

You must use FixedUpdate for any changes to rigidbodied gameobjects

Comment
Add comment · Show 1 · Share
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
avatar image doctor1232123 · Jul 20, 2018 at 12:47 PM 0
Share

this did seem to do something, yet when I use Rigidbody.Adforce (which I think is meant to be the way to move a character properly) the character just topples over. Would there be something else I need to do in order for it to work properly?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

226 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Issues Making Character Move With Rigidbody 1 Answer

Keep Horizontal Momentum after Jump 2 Answers

Stopping my player from moving when hitting a wall. 5 Answers

Player object falling through game environment, collision not detected with ground 0 Answers

Sphere get stuck in floor and fails to jump? 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