Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Car : MonoBehaviour
{
    float rotationSpeed = 100f;
    float accelerationForce = 0.3f;
    float brakeForce = 0.2f;
    float maxSpeed = 5.0f;
    float liftOffForce = 0.0f;

    [SerializeField] Track track;

    float currentSpeed = 0f;
    bool crashed = true;
    float timeElapsed = 0f;

    LayerMask collisionMask;
    // Start is called before the first frame update
    void Start()
    {
        ResetForStart();
    }

    public void ResetForStart()
    {
        transform.position = new Vector3(-0.5f, Random.Range(-4.7f, -5.3f));
        transform.localEulerAngles = new Vector3(0f, 0f, 90f);
        gameObject.SetActive(true);
        timeElapsed = 0f;
        currentSpeed = 0f;
        crashed = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Mathf.Abs(currentSpeed) > maxSpeed)
        {
            if (currentSpeed < 0)
            {
                currentSpeed = -maxSpeed;
            }
            else
            {
                currentSpeed = maxSpeed;
            }
        }

        if (!crashed)
        {
            timeElapsed += Time.deltaTime;
            GetMovement();
        }
    }

    private void GetMovement()
    {
        ManualMove();
    }

    private void ManualMove()
    {
        if (Input.GetAxisRaw("Horizontal") > 0f)
        {
            transform.Rotate(Vector3.forward * -rotationSpeed * Time.deltaTime);
        }
        else if (Input.GetAxisRaw("Horizontal") < 0f)
        {
            transform.Rotate(Vector3.forward * rotationSpeed * timeElapsed.deltaTime);
        }

        if (Input.GetAxisRaw("Vertical") > 0f)
        {
            currentSpeed += accelerationForce;
        }
        else
        {
            if (currentSpeed > 0)
            {
                currentSpeed -= liftOffForce;
                if (currentSpeed < 0)
                {
                    currentSpeed = 0;
                }
            }
            else if (currentSpeed < 0)
            {
                currentSpeed += liftOffForce;
                if (currentSpeed > 0)
                {
                    currentSpeed = 0;
                }
            }
        }
        transform.Translate(Vector2.up * currentSpeed * Time.deltaTime);
    }
}


The error says error CS1061; 'float' does not contain a definition for 'deltaTime' accepting a first argument of type 'float' could be found (are you missing a using directive or an assembly reference?)

What I have tried:

I dont know what try i see something video what talk about fixedUpdate but nothing work
Posted
Updated 6-Oct-22 18:36pm

1 solution

timeElapsed is a float value:
C#
float timeElapsed = 0f;
And they do contain contain a method called deltaTime. So this code:
C#
transform.Rotate(Vector3.forward * rotationSpeed * timeElapsed.deltaTime);
is trying to use a Time class method on a float value.
At a guess, you meant this:
C#
transform.Rotate(Vector3.forward * rotationSpeed * timeElapsed);
You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900