- Home /
Error CS0120 and CS1612 - Wheelcollider sidewaysFriction and Rigidbody.velocity
I'm currently working on a car project due in 2 days, and having some issues:
The error I'm having is:
"CS1612(XX,25(XX represents line 59-62)): Cannot modify a type return value of 'UnityEngine.WheelCollider.sidewaysFriction'. Consider storing the value in a temporary variable"
Here's my (C#)Code (I Used the '*' to show the error area):
using UnityEngine;
using System.Collections;
public class DriveScriptC : MonoBehaviour{
public float motorForce;
public float SteerAngle;
public WheelCollider BLWheel;
public WheelCollider BRWheel;
public WheelCollider FLWheel;
public WheelCollider FRWheel;
public float brakeForce;
public GameObject fin;
public bool CheckpointA;
public float slip;//slip of the velicle
public float hit;
public WheelFrictionCurve sidewaysFriction;
// Use this for initialization
void Start ()
{//what vars are
slip = 0;
CheckpointA = false;
fin = GameObject.FindWithTag ("Finish");
}
// Update is called once per frame
void Update () {
float v = Input.GetAxis ("Vertical") * motorForce;//
float h = Input.GetAxis ("Horizontal") * SteerAngle;//declaring var
//motor power
BLWheel.motorTorque = v;
BRWheel.motorTorque = v;
//steering
FLWheel.steerAngle = h;
FRWheel.steerAngle = h;
if (Input.GetKey (KeyCode.Space)) {//When SpaceBar is down(held), apply brake
BLWheel.brakeTorque = brakeForce;
BRWheel.brakeTorque = brakeForce;
}
if (Input.GetKeyUp (KeyCode.Space)){//When SpaceBar is up(not held), don't apply brake
BLWheel.brakeTorque = 0;
BRWheel.brakeTorque = 0;
}
*slip = 0.001 + 0.022 / (rigidbody.velocity.magnitude + 1);*
*//velocity-slip curve*
*FLWheel.sidewaysFriction.stiffness = slip*Time.deltaTime; // X*
*FRWheel.sidewaysFriction.stiffness = slip*Time.deltaTime; // XX*
*BLWheel.sidewaysFriction.stiffness = slip*Time.deltaTime; // XXX*
*BRWheel.sidewaysFriction.stiffness = slip*Time.deltaTime; //XXXX*
WheelHit FLhit;
WheelHit FRhit;
WheelHit BLhit;
WheelHit BRhit;
if(FLWheel.GetGroundHit(out FLhit)){
if(FLhit.collider.gameObject.tag == "Finish"){
if(CheckpointA == true){//if Checkpoint selected
print("FINISH!");//Print "Finish"
}
}
}
if(FRWheel.GetGroundHit(out FRhit)){
if(FRhit.collider.gameObject.tag == "Finish"){
if(CheckpointA == true){//if Checkpoint selected
print("FINISH!");//Print "Finish"
}
}
}
if(BLWheel.GetGroundHit(out BLhit)){
if(BLhit.collider.gameObject.tag == "Finish"){
if(CheckpointA == true){//if Checkpoint selected
print("FINISH!");//Print "Finish"
}
}
}
if(BRWheel.GetGroundHit(out BRhit)){
if(BRhit.collider.gameObject.tag == "Finish"){
if(CheckpointA == true){//if Checkpoint selected
print("FINISH!");//Print "Finish"
}
}
}
}
}
I do admit, I am kind of a newbie...
It should be rigidbody; your numbers like 0.22 should end in f (i.e 0.22f)
"CS0120(57,51): An object reference is required to access non-static member 'UnityEngine.WheelCollider.velocity'" was solved. Thanks, getyour411.
Any idea how to solve the other?
Answer by carrollh · Jan 20, 2015 at 07:42 PM
I at first though this was a scope error, but stiffness is public. Maybe it's because you have to use the WheelCollider.GetGroundHit, and then use the friction components in the value returned from it...?
The WheelCollider.GetGroundHit API says "any simulation of different ground materials must be done manually by adjusting forwardFriction and sidewaysFriction based on collider's material returned here."
And the WheelFrictionCurve API demos it as
var hit : WheelHit;
var wheel : WheelCollider = GetComponent(WheelCollider);
if( wheel.GetGroundHit( hit ) ) {
wheel.forwardFriction.stiffness = hit.collider.material.staticFriction;
wheel.sidewaysFriction.stiffness = hit.collider.material.staticFriction;
}
So maybe try implementing a WheelHit instance with a material on it, and then query and use that to set the stiffness. I'm at a loss as to exactly why you can't set it manually like you are though. Looking at the visibility of the things, I would think your original 4 lines of code would at least compile.
Your answer
Follow this Question
Related Questions
Stopping gameobject with colliders 1 Answer
How to detect my car is drifting or not ? 1 Answer
Wheel colliders act weird 0 Answers
Best Settings/Config Wheelcollider Tank 0 Answers
Internal collisions 1 Answer