private void DriveCar(float x, float y) { // Calculate speed from input and acceleration (transform.up is forward) Vector2 speed = transform.up * (y * carManager.acceleration); rb.AddForce(speed); // Create car rotation float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)); if (direction >= 0.0f) { rb.rotation += x * carManager.steering * (rb.velocity.magnitude / carManager.maxSpeed); } else { rb.rotation -= x * carManager.steering * (rb.velocity.magnitude / carManager.maxSpeed); } // Change velocity based on rotation float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.left)) * 2f; Vector2 relativeForce = Vector2.right * driftForce; rb.AddForce(rb.GetRelativeVector(relativeForce)); // Force max speed limit if (rb.velocity.magnitude > carManager.maxSpeed) { rb.velocity = rb.velocity.normalized * carManager.maxSpeed; } currentSpeed = rb.velocity.magnitude; }
void FixedUpdate() { if (AI) { List <double> inputs = new List <double>(System.Array.ConvertAll <Vector2, double>(sensorDirections, direction => { RaycastHit2D hit = Physics2D.Raycast(rb.position, rb.GetRelativeVector(direction), 256, LayerMask.GetMask("Wall")); return(hit ? hit.distance : 256); })) { Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)), Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.down)) }; double[] results = neuralNetwork.Calculate(inputs.ToArray()); ControlVehicle((float)results[0], (float)results[1]); //Stop car if it has not gone through any checkpoint for 3 seconds if (Time.time > lastTime + 3) { gameObject.SetActive(false); } } else { ControlVehicle(Input.GetAxisRaw("Vertical"), Input.GetAxisRaw("Horizontal")); } }
private void FixedUpdate() { Vector2 Vel = transform.right * (MovX * Acceleration); rb.AddForce(Vel); float Dir = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)); if (Acceleration > 0) { if (Dir > 0) { rb.rotation += MovY * RotationControl * (rb.velocity.magnitude / Speed); } else { rb.rotation -= MovY * RotationControl * (rb.velocity.magnitude / Speed); } } float trustForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.down)) * 2.0f; Vector2 relForce = Vector2.up * trustForce; rb.AddForce(rb.GetRelativeVector(relForce)); if (rb.velocity.magnitude > Speed) { rb.velocity = rb.velocity.normalized * Speed; } }
void FixedUpdate() { if (carBehaviour.IsCarOff()) { return; } //float h = 0; //float v = 0; //if (shouldMove) //{ // var heightMovement = Screen.height * 0.5f; // var mousePosition = Input.mousePosition.y; // h = Mathf.Clamp((mousePosition - heightMovement) / (heightMovement), -1, 1); // v = 1; //} else //{ // v = 0; //} float h = 0; float v = 1; //rb.AddForce(transform.right * aceleration * v); //rb.AddTorque(h * steering, ForceMode2D.Impulse); Vector2 speed = transform.right * (v * aceleration); rb.AddForce(speed); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(maxTurnAngle * h, Vector3.forward), steering * Time.fixedDeltaTime); //float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)); //if (direction >= 0.0f) //{ // rb.rotation += h * steering * (rb.velocity.magnitude / maxSpeed); //} //else //{ // rb.rotation -= h * steering * (rb.velocity.magnitude / maxSpeed); //} float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.down)) * 4f; Vector2 relativeForce = Vector2.up * driftForce; rb.AddForce(rb.GetRelativeVector(relativeForce)); if (rb.velocity.magnitude > maxSpeed) { rb.velocity = rb.velocity.normalized * maxSpeed; } else if (v == 0) { rb.velocity = Vector3.Lerp(rb.velocity, Vector3.zero, this.breaking * Time.fixedDeltaTime); } currentSpeed = rb.velocity.magnitude; }
private void FixedUpdate() { Vector2 vel = transform.right * (movX * acceleration); rb.AddForce(vel); float dir = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)); if (acceleration > 0) { rb.rotation += movY * rotationControl * (rb.velocity.magnitude / speed); } else { rb.rotation -= movY * rotationControl * (rb.velocity.magnitude / speed); } float thrustForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.down)) * 2.0f; Vector2 relativeForce = Vector2.up * thrustForce; rb.AddForce(rb.GetRelativeVector(relativeForce)); if (rb.velocity.magnitude > speed) { rb.velocity = rb.velocity.normalized * speed; } }
private void FixedUpdate() { Vector2 Direction = transform.position - Target.position; Direction.Normalize(); float cross = Vector3.Cross(Direction, transform.right).z; rb.angularVelocity = RotationControl * cross; Vector2 Vel = transform.right * (MovX * Acceleration); rb.AddForce(Vel); float Dir = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)); float trustForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.down)) * 2.0f; Vector2 relForce = Vector2.up * trustForce; rb.AddForce(rb.GetRelativeVector(relForce)); if (rb.velocity.magnitude > Speed) { rb.velocity = rb.velocity.normalized * Speed; } }
private void ProcessPhysics() { var steering = _currentVehicle.Performance.Steering; Vector2 speed = HandlingObject.transform.up * (_v * _currentVehicle.Performance.Acceleration); _vehicleRb.AddForce(speed); // Speed vector * Car forward vector in global space // Shows how is speed vector equals car forward direction // 0 when moving sidewais, positive when moving forward, negative when moving backward float direction = Vector2.Dot(_vehicleRb.velocity, _vehicleRb.GetRelativeVector(Vector2.up)); var curAngle = HandlingObject.transform.rotation.eulerAngles.z; _debugLabel.text = "Angle: " + curAngle + " H: " + _h; // Moving forward if (direction >= 0.0f) { if (_h >= 0 && (curAngle <= _turnAngle || curAngle > 180) || _h <= 0 && (curAngle >= 360 - _turnAngle || curAngle < 180)) { _vehicleRb.AddTorque((_h * steering) * (_vehicleRb.velocity.magnitude / 10.0f)); } else { if (curAngle > _turnAngle && curAngle < 180) { _vehicleRb.AddTorque(-steering * (_vehicleRb.velocity.magnitude / 10.0f)); } else if (curAngle < 360 - _turnAngle && curAngle > 180) { _vehicleRb.AddTorque(steering * (_vehicleRb.velocity.magnitude / 10.0f)); } } } // Moving backward, we shouldn't do that else { _vehicleRb.AddTorque((-_h * steering) * (_vehicleRb.velocity.magnitude / 10.0f)); } Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle = _vehicleRb.angularVelocity > 0 ? -90 : 90; Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; Debug.DrawLine((Vector3)_vehicleRb.position, (Vector3)_vehicleRb.GetRelativePoint(rightAngleFromForward), Color.green); float driftForce = Vector2.Dot(_vehicleRb.velocity, _vehicleRb.GetRelativeVector(rightAngleFromForward.normalized)); Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f); Debug.DrawLine((Vector3)_vehicleRb.position, (Vector3)_vehicleRb.GetRelativePoint(relativeForce), Color.red); _vehicleRb.AddForce(_vehicleRb.GetRelativeVector(relativeForce)); }
void FixedUpdate() { float h = 0; float v = 0; if (shouldMove) { var heightMovement = Screen.height * 0.5f; var mousePosition = Input.mousePosition.y; h = Mathf.Clamp((mousePosition - heightMovement) / (heightMovement), -1, 1); v = 1 - (h / 2); //Move backwards var point = Input.mousePosition.x - (Screen.width * 0.5f); if (point < offsetBackwards) { v = -v; } } else { v = 0; } //transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(maxTurnAngle * h, Vector3.forward), steering * Time.fixedDeltaTime); if (shouldMove) { Vector2 speed = transform.right * (v * aceleration); rb.AddForce(speed); float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)); if (direction >= 0.0f) { rb.rotation += h * steering * (rb.velocity.magnitude / maxSpeed); } else { rb.rotation -= h * steering * (rb.velocity.magnitude / maxSpeed); } float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.down)) * 4f; Vector2 relativeForce = Vector2.up * driftForce; rb.AddForce(rb.GetRelativeVector(relativeForce)); } if (rb.velocity.magnitude > maxSpeed) { rb.velocity = rb.velocity.normalized * maxSpeed; } else if (v == 0) { rb.velocity = Vector3.Lerp(rb.velocity, Vector3.zero, this.breaking * Time.fixedDeltaTime); } currentSpeed = rb.velocity.magnitude; }
private void FixedUpdate() { float h = -Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector2 speed = transform.up * (v * acceleration); rb.AddForce(speed); float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)); if (direction >= 0.0f) { rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f); //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f)); } else { rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f); //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f)); } Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle; if (rb.angularVelocity > 0) { steeringRightAngle = -90; } else { steeringRightAngle = 90; } Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; // Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green); float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized)); Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f); // Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red); rb.AddForce(rb.GetRelativeVector(relativeForce)); float currentSpeed = Vector3.Magnitude(rb.velocity); // test current object speed if (currentSpeed > maximumSpeed) { float brakeSpeed = currentSpeed - maximumSpeed; // calculate the speed decrease Vector3 normalisedVelocity = rb.velocity.normalized; Vector3 brakeVelocity = normalisedVelocity * brakeSpeed; // make the brake Vector3 value rb.AddForce(-brakeVelocity); // apply opposing brake force } }
/// <summary> /// Physics related calculations done in FixedUpdate /// </summary> void FixedUpdate() { if (!canMove) //Checing if game is On or not { return; } Vector2 speed = transform.up * gameData.GetSnakeSpeed() * Time.deltaTime; rb.AddForce(speed); float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)); currentEulerAngles = new Vector3(headTransform.rotation.x, headTransform.rotation.y, direction); //moving the value of the Vector3 into Quanternion.eulerAngle format currentRotation.eulerAngles = currentEulerAngles; //apply the Quaternion.eulerAngles change to the gameObject headTransform.rotation = currentRotation; if (direction >= 0.0f) { rb.rotation += Config.steeringDirection * gameData.GetSnakeSteering() * (rb.velocity.magnitude / gameData.GetTurnRateFactor()); } else { rb.rotation -= Config.steeringDirection * gameData.GetSnakeSteering() * (rb.velocity.magnitude / gameData.GetTurnRateFactor()); } Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle; if (rb.angularVelocity > 0) { steeringRightAngle = -90; } else { steeringRightAngle = 90; } // Find a Vector2 that is 90 degrees relative to the local forward direction Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; // Calculate an opposite force to the drift and apply this to generate sideways traction float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized)); // Calculate an opposite force to the drift and apply this to generate sideways traction Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * gameData.GetDriftForce()); rb.AddForce(rb.GetRelativeVector(relativeForce)); }
void FixedUpdate() { float h = -Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector2 speed = transform.up * (v * acceleration); rb.AddForce(speed); // Engine sound if (engineSound.clip != null) { engineSound.pitch = 1 + Mathf.Abs(v / 2); } float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)); if (direction >= 0.0f) { rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f); //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f)); } else { rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f); //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f)); } Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle; if (rb.angularVelocity > 0) { steeringRightAngle = -90; } else { steeringRightAngle = 90; } Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green); float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized)); Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f); Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red); rb.AddForce(rb.GetRelativeVector(relativeForce)); ShowPlayerShooterOrDirectionIndicator(); }
void FixedUpdate() { float h = -Input.GetAxis(horAxis); float v = Input.GetAxis(verAxis); //Debug.Log(v); Vector2 speed = transform.up * (v * acceleration); rb.AddForce(speed); if (rb.velocity.magnitude > maxSpeed) { rb.velocity = rb.velocity.normalized * maxSpeed; } float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)); if (direction >= 0.0f) { //rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f); rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f)); } else { //rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f); rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f)); } Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle; if (rb.angularVelocity > 0) { steeringRightAngle = -90; } else { steeringRightAngle = 90; } Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green); float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized)); Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f); Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red); rb.AddForce(rb.GetRelativeVector(relativeForce)); }
void FixedUpdate() { //using these feels more natural & sort of simulates jerk (velocity, acceleration, jerk, snap, crackle, pop) h = -Input.GetAxis("Horizontal"); v = Input.GetAxis("Vertical"); //move forward & back Vector2 speed = transform.up * (v * acceleration); body.AddForce(speed); //turn left & right float direction = Vector2.Dot(body.velocity, body.GetRelativeVector(Vector2.up)); if (direction >= 0.0f) { body.rotation += h * steering * (body.velocity.magnitude / 5.0f); //body.AddTorque((h * steering) * (body.velocity.magnitude / 10.0f)); } else { body.rotation -= h * steering * (body.velocity.magnitude / 5.0f); //body.AddTorque((-h * steering) * (body.velocity.magnitude / 10.0f)); } //calculate drag Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle; if (body.angularVelocity > 0) { steeringRightAngle = -90; } else { steeringRightAngle = 90; } Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; driftForce = Vector2.Dot(body.velocity, body.GetRelativeVector(rightAngleFromForward.normalized)); Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f); body.AddForce(body.GetRelativeVector(relativeForce)); //drawing debug lines topGear = body.velocity.magnitude; Debug.DrawLine((Vector3)body.position, (Vector3)body.GetRelativePoint(rightAngleFromForward), Color.green); Debug.DrawLine((Vector3)body.position, (Vector3)body.GetRelativePoint(relativeForce), Color.red); }
void FixedUpdate() { if (!isLocalPlayer) { return; } float h = -Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector2 speed = transform.up * (v * acceleration); rb.AddForce(speed); float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)); if (direction >= 0.0f) { rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f); } else { rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f); } Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle; if (rb.angularVelocity > 0) { steeringRightAngle = -90; } else { steeringRightAngle = 90; } Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green); float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized)); Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f); Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red); rb.AddForce(rb.GetRelativeVector(relativeForce)); }
private void FixedUpdate() { qtdDirecao = 0f; aceleracao = 0f; if (Input.GetKey(KeyCode.A)) { qtdDirecao = -1f; } else if (Input.GetKey(KeyCode.D)) { qtdDirecao = 1f; } if (Input.GetKey(KeyCode.W)) { aceleracao = -1f; } else if (Input.GetKey(KeyCode.S)) { aceleracao = 1f; } velocidade = aceleracao * forcaAceleracao; direcao = Mathf.Sign(Vector2.Dot(rb2d.velocity, rb2d.GetRelativeVector(Vector2.up))); rb2d.rotation += qtdDirecao * forcaDirecao * rb2d.velocity.magnitude * direcao; rb2d.AddRelativeForce(Vector2.up * velocidade); rb2d.AddRelativeForce(-Vector2.right * rb2d.velocity.magnitude * qtdDirecao / 2); }
void FixedUpdate() { steeringAmount = -variableJoystick.Horizontal; speed = variableJoystick.Vertical * accelerationPower; direction = Mathf.Sign(Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up))); rb.rotation += steeringAmount * steeringPower * rb.velocity.magnitude * direction; rb.AddRelativeForce(Vector2.up * speed); rb.AddRelativeForce(-Vector2.right * rb.velocity.magnitude * steeringAmount / 2); if (speed == 0 && !playingIdle) { playingIdle = true; playing = false; sound.clip = engineSounds[1]; sound.loop = true; sound.Play(); } else if (speed != 0 && !playing) { playingIdle = false; playing = true; sound.loop = true; sound.clip = engineSounds[2]; sound.Play(); } }
// Moving method I've found from online tutorials void move() { //X = Input.GetAxis("Horizontal"); Vector2 speed = transform.up * (Y * acc); rb.AddForce(speed); float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)); rotations.Add(X); if (acc > 0) { if (direction > 0) { rb.rotation -= X * steering * (rb.velocity.magnitude / MaxSpeed); } else { rb.rotation += X * steering * (rb.velocity.magnitude / MaxSpeed); } } if (rb.velocity.magnitude > MaxSpeed) { rb.velocity = rb.velocity.normalized * MaxSpeed; } }
void Poop() { if (isDying) { return; } if (mutations.Contains(Mutation.Bomberman)) { var giant = mutations.Contains(Mutation.Giant); var numBombs = giant ? 2 : Random.Range(2, 6); var scale = giant ? .5f : .2f; var offset = body.GetRelativeVector(Vector2.down) * collider.size.y * transform.localScale.y * 1.2f; // Poop bombs for (int i = 0; i < numBombs; i++) { var bombInstance = Instantiate(bomb); bombInstance.transform.position = body.position + offset + Random.insideUnitCircle * .1f; bombInstance.scale = scale; } } }
void FixedUpdate() { // Inverted horizontal value so that it maps to clockwise (positive) and counter-clockwise (negative) h = -Input.GetAxis("Horizontal"); Debug.DrawLine(new Vector2(0, 0), new Vector2(-h * 5, 0), Color.blue); v = Input.GetAxis("Vertical"); // Steering if (steerable) { rb.rotation += h * steerPower; } acceleration = transform.up * v * power; rb.AddForce(acceleration); // Calculate the sideways drift velocity driftForce = new Vector2(transform.InverseTransformVector(rb.velocity).x, 0); Debug.DrawLine(rb.position, rb.GetRelativePoint(driftForce * 5), Color.red); // Calculate an opposing friction force to counteract drift, limited by a maximum type grip frictionForce = Vector2.ClampMagnitude(driftForce * -1 * surfaceFriction, maxTyreFriction); Debug.DrawLine(rb.position, rb.GetRelativePoint(frictionForce * 5), Color.green); // TODO // Improve the friction response to reduce drift at low speeds and stabilise drift at higher speeds // Apply the friction force to counteract drift, i.e. so wheels 'roll' forwards/backwards but not sideways rb.AddForce(rb.GetRelativeVector(frictionForce), ForceMode2D.Impulse); // Draw skidmarks when drift force exceeds maximum friction skidmarks.emitting = driftForce.sqrMagnitude > frictionForce.sqrMagnitude * skidmarkThreshold ? true : false; }
// Update is called once per frame void FixedUpdate() { // Debug.DrawRay(transform.position,transform.up-transform.right, Color.cyan ); if (!hitBorder) { updateSensors(); var(accelarationInput, steeringInput) = neuralNetwork.RunNetwork(leftSensor, frontSensor, rightSensor); float acceleration = accelarationInput * accelerationRate; float steeringPower = steeringInput; //Input.GetAxis("Horizontal"); // print("Vertical: " + Input.GetAxis("Vertical") + ", Horizontal: " + Input.GetAxis("Horizontal")); //float acceleration = Random.Range(0f, 1f) * accelerationRate; //float steeringPower = Random.Range(-1f, 1f);//Input.GetAxis("Horizontal"); // get the object steering direction float direction = Mathf.Sign(Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up))); rb.rotation += steeringPower * steeringRate * rb.velocity.magnitude * -direction; // accelerate object according to its local transform value rb.AddRelativeForce(Vector2.up * acceleration); rb.AddRelativeForce(Vector2.right * rb.velocity.magnitude * steeringPower / 2); calculateFitness(); //update position value lastPosition = transform.position; timeLived += Time.deltaTime; } else { Death(); } }
void FixedUpdate() { // Steering if (steerable) { //rb.AddTorque(h * steerPower); //rb.rotation += h * steerPower; rb.SetRotation(rb.rotation + h * steerPower); // TODO // Figure out a way to use rb.MoveRotation() so that interpolation is respected // Using this will mean that the Friction Joint 2D will be overriden, so a work-around is needed } acceleration = transform.up * v * power; rb.AddForce(acceleration); // Calculate the sideways drift velocity driftForce = new Vector2(transform.InverseTransformVector(rb.velocity).x, 0); //Debug.DrawLine(rb.position, rb.GetRelativePoint(driftForce * 5), Color.red); // Calculate an opposing friction force to counteract drift, limited by a maximum type grip frictionForce = Vector2.ClampMagnitude(driftForce * -1 * surfaceFriction, maxTyreFriction); //Debug.DrawLine(rb.position, rb.GetRelativePoint(frictionForce * 5), Color.green); // TODO // Improve the friction response to reduce drift at low speeds and stabilise drift at higher speeds // Apply the friction force to counteract drift, i.e. so wheels 'roll' forwards/backwards but not sideways rb.AddForce(rb.GetRelativeVector(frictionForce), ForceMode2D.Impulse); // Draw skidmarks when drift force exceeds maximum friction skidmarks.emitting = driftForce.sqrMagnitude > frictionForce.sqrMagnitude * skidmarkThreshold ? true : false; }
void FixedUpdate() { //vertical movement speed = Input.GetAxis("Vertical") * accelerationPower; //keyboard input //update with UI button if (go) { speed = accelerationPower; } else if (rev) { speed = -accelerationPower; } if (!doors)//dont move if door is open { RB2.AddRelativeForce(Vector2.up * speed); } //horizontal movement steeringAmount = -Input.GetAxis("Horizontal"); //keyboard input //update with UI button if (left) { steeringAmount = steeringPower; } else if (right) { steeringAmount = -steeringPower; } direction = Mathf.Sign(Vector2.Dot(RB2.velocity, RB2.GetRelativeVector(Vector2.up))); RB2.rotation += steeringAmount * steeringPower * RB2.velocity.magnitude * direction; RB2.AddRelativeForce(-Vector2.right * RB2.velocity.magnitude * steeringAmount / 2); }
void FixedUpdate() { // rotation targetTransform = target.transform; rotateToTarget(); var carinfo = GetComponent <Car_info>(); // forward motion and drift (same as player car), always accelerate Vector2 speed = transform.up * (1 * acceleration); rb.AddForce(speed); float drift = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.left)) * 2.0f; Vector2 relativeForce = Vector2.right * drift; Debug.DrawLine(rb.position, rb.GetRelativePoint(relativeForce), Color.green); rb.AddForce(rb.GetRelativeVector(relativeForce)); if (rb.velocity.magnitude > maxSpeed) { rb.velocity = rb.velocity.normalized * maxSpeed; } carinfo.drift = drift; curSpeed = rb.velocity.magnitude; RaycastHit2D hit = CheckRaycast(); if (hasWeapon == true && weapon == 0) { if (hit.collider.tag == "Car") { GameObject go = (GameObject)Instantiate(missile, transform.position + (transform.up * 5), transform.rotation); hasWeapon = false; } } if (hasWeapon == true && weapon == 1) { Invoke("dropOil", Random.Range(0, 10)); hasWeapon = false; } }
private void CarMovement() { // Get the horizontal and vertical input float horizontalInput = -Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); // Set the player's speed based on it's input and acceleration Vector2 speed = transform.up * (verticalInput * acceleration); rb.AddForce(speed); // Set the direction float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)); // Rotate the riggidbody based on input if (direction >= 0.0f) { rb.rotation += horizontalInput * steering * (rb.velocity.magnitude / 5.0f); } else { rb.rotation -= horizontalInput * steering * (rb.velocity.magnitude / 5.0f); } Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle; if (rb.angularVelocity > 0) { steeringRightAngle = -90; } else { steeringRightAngle = 90; } // Set the player's drift based on it's current steering Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized)); Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f); rb.AddForce(rb.GetRelativeVector(relativeForce)); }
public override void AgentAction(float[] vectorAction, string textAction) { int action = Mathf.FloorToInt(vectorAction[0]); switch (action) { case 1: //agent.transform.position = agent.transform.position + 1f * Vector3.up; rbBoat.AddRelativeForce(Vector2.up * speed); break; case 2: //agent.transform.position = agent.transform.position + 1f * Vector3.down; rbBoat.AddRelativeForce(Vector2.down * speed); break; case 3: //agent.transform.position = agent.transform.position + 1f * Vector3.RotateTowards; direction = Mathf.Sign(Vector3.Dot(rbBoat.velocity, rbBoat.GetRelativeVector(Vector2.up))); rbBoat.rotation += -0.5f * steeringPower * rbBoat.velocity.magnitude * direction; //rbBoat.AddRelativeForce(Vector3.right * rbBoat.velocity.magnitude / 2); break; case 4: direction = Mathf.Sign(Vector3.Dot(rbBoat.velocity, rbBoat.GetRelativeVector(Vector2.up))); rbBoat.rotation += 0.5f * steeringPower * rbBoat.velocity.magnitude * direction; // rbBoat.AddRelativeForce(Vector3.right * rbBoat.velocity.magnitude / 2); break; } curDist = Math.Sqrt(Math.Pow((goal.transform.position.x - agent.transform.position.x), 2) + Math.Pow((goal.transform.position.y - agent.transform.position.y), 2)); float calDist = Convert.ToSingle(preDist - curDist); AddReward(calDist / 50); Ray2D ray = new Ray2D(agent.transform.position, Vector2.zero); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 10f); Debug.DrawRay(ray.origin, ray.direction * 10f, Color.red); //Debug.Log(hit.distance); }
// Use this for calculate public override void OnCalculate() { Rigidbody2D rigidbody2D = _Rigidbody2D.value; if (rigidbody2D != null) { _Result.SetValue(rigidbody2D.GetRelativeVector(_RelativeVector.value)); } }
void FixedUpdate() { steeringAmount = -Input.GetAxis("Horizontal"); speed = Input.GetAxis("Vertical") * accelerationPower; direction = Mathf.Sign(Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up))); rb.rotation += steeringAmount * steeringPower * rb.velocity.magnitude * direction; rb.AddRelativeForce(Vector2.up * speed); rb.AddRelativeForce(-Vector2.right * rb.velocity.magnitude * steeringAmount / 2); }
// Update is called once per frame void FixedUpdate() { Vector2 speed = transform.up * (v * acceleration) * 20; RigidBodycar.AddForce(speed); float direction = Vector2.Dot(RigidBodycar.velocity, RigidBodycar.GetRelativeVector(Vector2.up)); if (direction >= 0.0f) { RigidBodycar.rotation += h * steering * (RigidBodycar.velocity.magnitude / 5.0f); //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f)); } else { RigidBodycar.rotation -= h * steering * (RigidBodycar.velocity.magnitude / 5.0f); //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f)); } Vector2 forward = new Vector2(0.0f, 0.5f); float steeringRightAngle; if (RigidBodycar.angularVelocity > 0) { steeringRightAngle = -90; } else { steeringRightAngle = 90; } Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward; Debug.DrawLine((Vector3)RigidBodycar.position, (Vector3)RigidBodycar.GetRelativePoint(rightAngleFromForward), Color.green); float driftForce = Vector2.Dot(RigidBodycar.velocity, RigidBodycar.GetRelativeVector(rightAngleFromForward.normalized)); Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f); Debug.DrawLine((Vector3)RigidBodycar.position, (Vector3)RigidBodycar.GetRelativePoint(relativeForce), Color.red); RigidBodycar.AddForce(RigidBodycar.GetRelativeVector(relativeForce)); }
//moves the bullet forward depending on the direction of the car void Update() { rb.velocity = rb.GetRelativeVector(Vector2.up * m_Speed); GameManager gM = GameObject.FindObjectOfType <GameManager>(); if (gM.destroyAllBullets) { Destroy(gameObject); } }
static int GetRelativeVector(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 2); Rigidbody2D obj = LuaScriptMgr.GetNetObject <Rigidbody2D>(L, 1); Vector2 arg0 = LuaScriptMgr.GetNetObject <Vector2>(L, 2); Vector2 o = obj.GetRelativeVector(arg0); LuaScriptMgr.PushValue(L, o); return(1); }