- Home /
Question about rigidbody velocity
So I'm following the Car Tutorial available in the resources section of the site and am trying to do something similar, but much simpler, on my own. So far I'm just using a cube to represent a car. My problem comes when, like in the example code, I check the sign of the rigidbody velocity in the z axis against the sign of the vertical input. If it's the same it should add to the engine power, otherwise it should detract from it. My code looks like this:
if(v == 0){
currentEnginePower -= Time.deltaTime * 200;
}
else if(Mathf.Sign(v) == Mathf.Sign(relativeVelocity.z)){
currentEnginePower += Time.deltaTime * 300;
}
else{
currentEnginePower -= Time.deltaTime * 300;
}
if(currentEnginePower < 0){
currentEnginePower = 0;
}
This doesn't seem to work at all though. If the sign of the vertical axis (v) is positive, nothing changes. If I hold it at -1 the engine power jumps between 6 and a very small number, something like 1 * 10^-7. The engine power never has a different value than one of these two. It should be noted that the cube's position and rotation seems to constantly change in very small amounts. Any idea whats going on? Do I need to have wheels to make this work correctly? The reason why I don't have them is because I'm splitting the workload with a partner and I'm focused solely on the physics part of the project.
Answer by ShadowAngel · Oct 19, 2012 at 08:36 PM
i dont see anything thet modifis any rigdbody force. If u like more inside of how force works, just play a little with Rigidbody.AddForce() Here some exemple, its a JS script thet uses force to move a fighter:
function Attack(Target : Transform)
{
var moveDirection : Vector3 = Vector3.zero;//intialize var for move direction.
var targetRotation = Quaternion.LookRotation(Target.position - transform.position); // get a rotation we need to look at target.
var Distance : float = Vector3.Distance (Target.position, this.transform.position); // get a distance from us to target
var SpeedUp : float = 5/Distance; // Speed up multiplayer. Closer we are to a target, more force to move.
var NextRotation : Quaternion = Quaternion.RotateTowards(transform.rotation, targetRotation, ThisAiActor.MaxSpeed/SpeedUp * 30 * Time.deltaTime);//Get a maximum rotation for this frame(to smoothly turn to our target)
transform.rotation = NextRotation;//rotate our fighter for this frame
moveDirection += Vector3.forward * SpeedUp * ThisStats.EnginePower;//get force with what we gona push our fighter for this frame.
moveDirection = transform.TransformDirection(moveDirection);//Transform forward direction from local to world.
if (!ThisAiActor.DisableMovment)
rigidbody.AddForce(moveDirection); //finaly push our fighter forward.(right and left movement we gona get from rotation becos we ALWAYS move forward)
}