• 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 joseramon22 · Dec 08, 2017 at 01:27 PM · physicsrigidbodyforceturningturn

After adding a force to a moving gameobject it will behaviour in a strange way.

i have a game object with a velocity (rb.velocity.x,rb.velocity.y,10) set at start.When this objectcollides with another one that has an especific tag (curva) i make that game object turn by rotating it and adding relative forcé forwar.This makes object go too much forward, for compensate that i also add a global backward force. This works well, but when the gameobject exit the collider, it start moving diagonal with no evident cause. I am a newbie, so i dont know how to solve that, thank you for your help.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class sCRIPT : MonoBehaviour {
     private Rigidbody rb;
     Vector3 turn = new Vector3 (0,-0.6f,0);
     public GameObject h;
     public float moveHorizontal;
     public float test;
     float inturn = 1;
     public float crash = 0;
     public float zVel;
     public float pos;
     void Start () {
         test = 0;
         rb = GetComponent<Rigidbody>();
         rb.freezeRotation = enabled;
         rb.velocity = new Vector3 (rb.velocity.x,rb.velocity.y,-20);
     }
     void FixedUpdate ()
     {
         if (crash == 0) {
             moveHorizontal = Input.GetAxis ("Horizontal");
             Vector3 movement = new Vector3 (moveHorizontal * inturn, 0, 0);
             rb.AddRelativeForce (movement * -10);
     }
         if (test == 1) {
             transform.Rotate (turn);
             rb.AddRelativeForce (new Vector3 (0, 0, -20));
             rb.AddForce (new Vector3 (0, 0, ((-zVel) - 5)), ForceMode.Acceleration);
         }
         if (transform.eulerAngles.y != 0) {
             if (transform.eulerAngles.y <= 270) {
                 test = 0;
             }
         }
         zVel = (transform.position.z-pos)/Time.fixedDeltaTime;
         pos = transform.position.z;
     }
     void Update () 
     {
         if (Input.GetKeyDown (KeyCode.Space)) {
             rb.velocity = new Vector3 (rb.velocity.x, 11, rb.velocity.z);
         }
     }
     void OnTriggerEnter (Collider other){
         if (other.gameObject.CompareTag ("curva")) {
             inturn = 1.5f;
             test = 1;
         }
     }
     void OnCollisionStay(Collision other){
             if (other.gameObject.CompareTag ("pared")) {
             crash = 1;
         }
     }
     void OnTriggerExit (Collider other){
         if (other.gameObject.CompareTag ("curva")) {
             inturn = 1;
             test = 0;
         } 
     }
     void OnCollisionExit (Collision other){
         if (other.gameObject.CompareTag ("pared")) {
             crash = 0;
         }
     }
 }

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.

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

186 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

Related Questions

How do I get objects in front of the player to be blasted away? 1 Answer

Getting two Objects to the same direction with different speed 0 Answers

Door hinge joints 0 Answers

How to make rigidbody not effect movement while still using it for collisions? 0 Answers

Backspin of Sphere with Rigidbody 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