- Home /
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;
}
}
}
}
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'.
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 :/
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
Follow this Question
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