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
1
Question by Luka38 · Mar 13, 2014 at 12:50 PM · c#rotationaddforcecarparenting

AddForce to parent based on child's rotation C#

Hi there, my first post :)

So I have a modified car script that I want to make it move more or less like a real car. I have a car object that has an "arrow" as a child object. The arrow is positioned where the "middle wheel" would be, sort of like on those old three-wheeled cars. The arrow rotates left and right and I want to make the car move towards it's direction. I used AddForce to do this, but don't know how to make it add force to the direction of the middle wheel. Hope I'm not too vague about my problem.

This is a very shortened script, the car moves fine and everything, but the original code had the car rotate by on it's own, even when being still. Also I left out the whole movespeed, gear, and gas_pedal so the post isn't too cluttered. Here's the shortened script:

 using UnityEngine;
 using System.Collections;
 
 public class MicanjeCar4 : MonoBehaviour {
     
     public Transform Car;
     public Transform ArrowObjectRot;
     public float movespeed = 5.0F; 
     public int gas_pedal = 0;
     public int gear = 1;
     
     void Start () {
         arrowObjectRot = ArrowObjectRot.transform.localPosition;
     }
     
     void Update () 
     {
         if (Input.GetKey(addgas))
         {
             if (gas_pedal <= 100)
             {
                 if (movespeed <= 0)
                 {
                     if (gear == 1) {
                         Car.rigidbody.AddForce(transform.forward * Time.deltaTime * movespeed, ForceMode.Acceleration);
                         gas_pedal += 1;
                         
                     }
                     else {
                         isengine = false;
                     }
                 }
                 else {
                     Car.rigidbody.AddForce(transform.forward * Time.deltaTime * movespeed, ForceMode.Acceleration);
                     gas_pedal += 1;
                 }
             }
             
             else {
                 Car.rigidbody.AddForce(transform.forward * Time.deltaTime * movespeed, ForceMode.Acceleration);
             }
         }
         
         else 
         {
             if (gas_pedal > 0) {
                 Car.rigidbody.AddForce(transform.forward * Time.deltaTime * movespeed, ForceMode.Acceleration);
                 gas_pedal -= 1;
             }
         }
         
     }
 }


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 robertbu · Mar 13, 2014 at 06:20 PM

I can see this problem getting complicated if you are trying to use the physics to make a car turn on wheels, but taking your question at face value I'd suggest rather than using 'transform.forward' in your AddForce, use 'ArrowObjectRot.transform.forward'. So your line becomes:

  Car.rigidbody.AddForce(ArrowObjectRot.transform.forward * Time.deltaTime * movespeed, ForceMode.Acceleration);

The only thing that puzzles me is this line and your use of localPosition:

 arrowObjectRot = ArrowObjectRot.transform.localPosition;

If what you are really asking is how to use the relative position of 'arrowObjectRot', you may want something like this:

 var forward =  ArrowObjectRot.transform.position - transform.position;
 forward.y = 0;

Then you use 'forward' in your AddForce() in place of 'transform.forward'.

Comment
Add comment · Show 2 · 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 Luka38 · Mar 14, 2014 at 10:18 AM 0
Share

Thanks for the help. But the car is still driving in it's Z axis, but now when I rotate the arrow to the left or right the car loses speed, and when I rotate it towards the car's -Z position the car drives backwards. $$anonymous$$y program$$anonymous$$g is not really good (as you can see) so I don't know what I was actually doing with localPosition :/

avatar image robertbu · Mar 14, 2014 at 09:43 PM 0
Share

To figure out what is going wring, a picture would be helpful. A screen shot of the vehicle or something that gives me a visual clue about how you have this setup.

The second thing I need is for you to describe what side of the arrow object is 'forward'. That is, if the arrow object was not a child and had a rotation of (0,0,0), what side of the object would point forward.

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

20 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

Related Questions

Flip over an object (smooth transition) 3 Answers

WheelCollider rotates mesh wrong 1 Answer

Distribute terrain in zones 3 Answers

Orbit Camera Around Player And Add Force 3 Answers

transform.Translate moving while upside down? 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