Hello I cant modify Rb.vel.mag in unity - c#

Hello I'm a newbie and I have an error I searched google and there came up results but they used to work I remember but I guess it updated anyway whenever I write this:
m_Rigidbody.velocity.magnitude = m_Max_SpeedBACK;
(Yes, I didn't forget to make the float itself)
And it shows this error
enter image description here
It said I'm not allowed to send img but anyway I want to have this if written simply
(rb.speed = float)
Can anyone help, thanks?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class my_Char_Cont : MonoBehaviour
{
public Rigidbody m_Rigidbody;
public float m_Thrust_Forth = 20f;
public float m_Thrust_Behind = -20f;
public float m_time = 10f;
public float m_timeNeg = -10f;
public float m_Max_SpeedFORTH = 4f;
public float m_Max_SpeedBACK = 2f;
// Start is called before the first frame update
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
bool forwardPress = Input.GetKey("w");
bool backPress = Input.GetKey("s");
bool leftShiftPress = Input.GetKey("left shift");
//---------------------------------------------------------------------------------------------------//
if (forwardPress)
{
m_time = m_time + Time.deltaTime;
if (m_time >= 4)
{
m_time = 3.9f;
}
m_Rigidbody.AddForce(transform.forward * m_Thrust_Forth * m_time);
}
else
{
m_time = 0;
}
if (backPress)
{
m_timeNeg = m_timeNeg + Time.deltaTime;
if (m_timeNeg >= 4)
{
m_timeNeg = 3.9f;
}
m_Rigidbody.AddForce(transform.forward * m_Thrust_Behind * m_timeNeg);
if (m_Rigidbody.velocity.magnitude >= m_Max_SpeedBACK)
{
m_Rigidbody.velocity.magnitude = m_Max_SpeedBACK;
}
}
else
{
m_timeNeg = 0;
}
}
}

The velocity can be read, but not written like that. As #Ruzihm already explained.
If you want to limit the velocity while keeping the direction, do this:
if (m_Rigidbody.velocity.magnitude >= m_Max_SpeedBACK)
{
m_Rigidbody.velocity = m_Rigidbody.velocity.normalized * m_Max_SpeedBACK;
}
normalized returns the vector, normalized so that the length (magnitude) is 1. But to clarify: it is not "(1,1,1)" - the direction stays. Example: (0.802, 0.267, 0.534) has a magnitude of 1.
Therefore, you can assign the velocity to a value, that equals the normalized velocity * your max allowed magnitude.

Related

Set behaviours(script a) controlled by booleans(script b) based on Time.time (flocking system)

I am working on a flocking system in Unity and am new to c#. I am working with 2 scripts - 1 that manages the overall flock (FlockTest) and the other that manages particle behaviour (FlockParticleBehaviour). I have followed a tutorial which has public boolean values that control seeking behaviour in FlockParticleBehaviour through FlockTest. In play mode, I can toggle these booleans to change the goal seeking behaviour. However, I want to automate this toggling based on time (To add it to an AR session). I have added an if statement to void Update() in the FlockTest and when I hit play, the seekGoal and obedient boolean boxes switch on and off but nothing happens to the particles. I have tried using an invoke method which didn't work(no errors but boxes dont switch on and off) and thought about trying a coRoutine but I am not sure this will work since I don't want to stop and start my script. I am at a loss as to how to get the particles obeying the boolean in update. Am I meant to be referencing in my particle behaviour script's flock function? Very new so would love some help if anyone knows a better way forward!
FlockTest script (contains if statement)
using System.Collections.Generic;
using UnityEngine;
public class FlockTest : MonoBehaviour
{
public GameObject[] particles;
public GameObject particlePrefab;
public int particleCount = 10;
public Vector3 range = new Vector3(5,5,5);
public Vector3 innerLimit = new Vector3(1,1,1);
public bool seekGoal = true;
public bool obedient = true;
public bool willful = false;
[Range(0, 200)]
public int neighbourDistance =50;
[Range(0,2)]
public float maxForce = 0.5f;
[Range(0,5)]
public float maxvelocity = 2.0f;
// Start is called before the first frame update
void Start()
{
int time = (int)Time.time;
particles = new GameObject[particleCount];
for(int i = 0; i < particleCount; i++)
{
Vector3 particlePos = new Vector3(Random.Range(-range.x, range.x), Random.Range(-range.y, range.y), Random.Range(-range.z, range.z));
particles[i] = Instantiate(particlePrefab, this.transform.position + particlePos, Quaternion.identity) as GameObject;
particles[i].GetComponent<FlockParticleBehaviour>().manager = this.gameObject;
}
}
void Update()
// the toggles in the inspector are changing but nothing is happening with the particles.
{
int time = (int)Time.time;
if(time == 3f) {
seekGoal = false;
obedient = false;
willful = true;
}
if(time == 6f)
{
seekGoal = true;
obedient = true;
willful = false;
}
}
}
FlockParticleBehaviour script
using System.Collections.Generic;
using UnityEngine;
public class FlockParticleBehaviour : MonoBehaviour
{
public GameObject manager;
public Vector3 location = Vector3.zero;
public Vector3 velocity;
Vector3 goalPos = Vector3.zero;
Vector3 currentForce; //this is a current force position. pushes particle around by adding all the other forces
// Start is called before the first frame update
void Start()
{
velocity = new Vector3(Random.Range(0.01f, 0.1f), Random.Range(0.01f, 0.1f), Random.Range(0.01f, 0.1f));
location = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z);
}
Vector3 seek(Vector3 target)
{
return(target - location);
}
void applyForce(Vector3 f)
{
Vector3 force = new Vector3(f.x, f.y, f.z);
if(force.magnitude > manager.GetComponent<FlockTest>().maxForce)
{
force = force.normalized;
force *= manager.GetComponent<FlockTest>().maxForce;
}
this.GetComponent<Rigidbody>().AddForce(force);
if(this.GetComponent<Rigidbody>().velocity.magnitude > manager.GetComponent<FlockTest>().maxvelocity)
{
this.GetComponent<Rigidbody>().velocity = this.GetComponent<Rigidbody>().velocity.normalized;
this.GetComponent<Rigidbody>().velocity *= manager.GetComponent<FlockTest>().maxvelocity;
}
Debug.DrawRay(this.transform.position, force, Color.white);
}
Vector3 align()
{
float neighbourdist = manager.GetComponent<FlockTest>().neighbourDistance;
Vector3 sum = Vector3.zero;
int count = 0;
foreach (GameObject other in manager.GetComponent<FlockTest>().particles)
{
if(other == this.gameObject) continue;
float d = Vector3.Distance(location, other.GetComponent<FlockParticleBehaviour>().location);
if (d < neighbourdist) {
sum += other.GetComponent<FlockParticleBehaviour>().velocity;
count++;
}
}
if (count >0)
{
sum /= count;
Vector3 steer = sum - velocity;
return steer;
}
return Vector3.zero;
}
Vector3 cohesion()
{
float neighbourdist = manager.GetComponent<FlockTest>().neighbourDistance;
Vector3 sum = Vector3.zero;
int count = 0;
foreach (GameObject other in manager.GetComponent<FlockTest>().particles)
{
if(other == this.gameObject) continue;
float d = Vector3.Distance(location, other.GetComponent<FlockParticleBehaviour>().location);
if(d < neighbourdist)
{
sum += other.GetComponent<FlockParticleBehaviour>().location;
count++;
}
}
if (count > 0)
{
sum /= count;
return seek(sum);
}
return Vector3.zero;
}
void flock()
{
location = this.transform.position;
velocity = this.GetComponent<Rigidbody>().velocity;
if(manager.GetComponent<FlockTest>().obedient && Random.Range(0,50) <=1)
{
Vector3 ali = align();
Vector3 coh = cohesion();
Vector3 gl;
if(manager.GetComponent<FlockTest>().seekGoal)
{
gl = seek(goalPos);
currentForce = gl + ali +coh;
}
else
currentForce = ali + coh;
currentForce = currentForce.normalized;
}
if(manager.GetComponent<FlockTest>().willful && Random.Range(0,50)<=1)
{
if(Random.Range(0,50)<1) //change direction
currentForce = new Vector3(Random.Range(0.01f, 0.1f), Random.Range(0.01f, 0.1f),Random.Range(0.01f, 0.1f));
}
applyForce(currentForce);
}
// Update is called once per frame
void Update()
{
flock();
goalPos = manager.transform.position;
}
}
Several points:
it is much easier and cleaner to set your flock manager directly as FlockTest, not GameObject to avoid GetComponent calls.
I cannot understand what you want to achieve by calling (int)Time.time and comparing it later with 3 and 6. Time.time returns the number of seconds that passed from the start of the application. So your code in Update method of FlockTest script will not have any chance to be called after the seventh second of your game passed. So obedient will always be true and willful will always be false after the seventh second.
Your Random.Range(0, 50) <= 1 is quite a low chance. It will return an int value from 0 to 49, so it is only a 2% chance that your changes in FlockTest will apply to FlockParticleBehaviour instance. Is it what you wanted to get? You can try to remove this random from the if statement to make this chance 100% and check if this is an issue.
Right now it seems like the chance of changing something is too low to see it in several seconds of the game. As I've said above, after the seventh second your bool values will never change.

How can I calculate a moving object speed?

The goal is to show both transform and speed movement speed in text1 and text2.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Follow : MonoBehaviour
{
public Transform targetToFollow;
public Text text;
public Text text1;
public Text text2;
public float lookAtRotationSpeed;
public float moveSpeed;
private float minMoveSpeed = 0f;
private Vector3 originPos;
// Start is called before the first frame update
void Start()
{
originPos = targetToFollow.position;
}
// Update is called once per frame
void Update()
{
Vector3 lTargetDir = targetToFollow.position - transform.position;
lTargetDir.y = 0.0f;
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);
var distance = Vector3.Distance(transform.position, targetToFollow.position);
text.text = "Transform Distance From Target " + distance.ToString();
float ms = moveSpeed;
if (distance > 5f)
{
ms = moveSpeed + 0.5f; //move faster
}
else if (distance < 1.5f)
{
ms = Mathf.Max(minMoveSpeed, ms - 0.3f); // move slower
}
else
{
default value that equal moveSpeed here
transform.position = Vector3.MoveTowards(transform.position, targetToFollow.position, Time.deltaTime * ms);
}
if(distance < 0.5f && originPos == targetToFollow.position)
{
ms = 0f;
}
transform.position = Vector3.MoveTowards(transform.position, targetToFollow.position, Time.deltaTime * ms);
originPos = targetToFollow.position;
}
}
Now I'm setting to text the transform distance from the target.
But I want to add now to text1 and text2 the speed movement of the transform and the target.
In the end there will be 3 texts ui's that will show the distance and movement speeds.
I tried like this for the target speed :
var speedPerSec = Vector3.Distance(originPos, targetToFollow.position) / Time.deltaTime;
text1.text = "Target Speed Per Second " + speedPerSec.ToString();
but when the target is moving the text of the speed the speedPerSec.toString() is blinking fast and not smooth like the distance in the first text.
maybe it's blinking because it's showing the speed a lot after the zero point like : 0.00656555 ?
Since you want to track the speed of multiple objects (your transform and the target) the easiest way is to introduce a separate object handling the tracking and use an array of these.
[Serializable]
private class SpeedTracking
{
public Transform trackedObject;
public Text uiOutput;
private Vector3 _position;
private float _speed;
public void Update(float elapsedTime, float changeThreshold)
{
Vector3 newPos = trackedObject.position;
float newSpeed = Vector3.Distance(newPos, _position) / elapsedTime;
if ((newSpeed == 0 && _speed != 0) || Mathf.Abs(newSpeed - _speed) > changeThreshold)
{
_speed = newSpeed;
uiOutput.text = $"speed: {newSpeed:F3}";
}
_position = newPos;
}
}
private const float SPEED_CHANGE_THRESHOLD = 0.001F;
[SerializeField]
private SpeedTracking[] _trackings;
void OnEnable()
{
if (_trackings.Any(t => t.trackedObject == null || t.uiOutput == null))
{
Debug.LogError("at least one invalid tracking found");
enabled = false;
}
}
void Update()
{
//[...]
UpdateTrackings();
}
void UpdateTrackings()
{
foreach (SpeedTracking tracking in _trackings)
{
tracking.Update(Time.deltaTime, SPEED_CHANGE_THRESHOLD);
}
}
I also added a format to your output string (F3) showing only 3 decimal digits. (This alone would solve the flickering, but why update the ui more often than necessary?)
If you only want to know how much velocity of object in specific time
you can do something like this.
public class VelocityDisplayer : MonoBehaviour
{
public Transform Object; // your target gameobject
public Text View; // your text object
private Vector3 _position; // last position of target
private void OnEnable() {
_position = Object.transform.position;
}
private void Update() {
var dt = Time.deltaTime;
var current = Object.transform.position;
var delta = Vector3.Distance(current, _position);
var velocity = delta / dt;
View.text = (velocity).ToString("#,##0.000");
// replace current position
_position = current;
}
}
but this show actual speed which depend on game world/physics of object not your setup movement speed variable
PS1. I prefer to use update because is UI scope
PS2. I prefer to split new class to track object

How can I add a slowdown effect when object is getting close to a target?

using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.AI;
public class Waypoints : UnityEngine.MonoBehaviour
{
public List<Transform> waypoints = new List<Transform>();
public float movementSpeed = 5.0f;
public float slowdownSpeed = 1f;
public float rotationSpeed = 2.0f;
public float waypointDistance = 0.1f;
public float slowdownDistance = 7f;
public bool moveBackward = false;
public bool moveLoop = false;
public bool includeTransformPosition = false;
private Transform targetWaypoint;
private int targetWaypointIndex = 0;
private int lastWaypointIndex;
private bool includeTransform = true;
private GameObject go;
// Use this for initialization
void Start()
{
go = new GameObject();
go.transform.position = transform.position;
if (moveBackward && waypoints.Count > 2)
{
lastWaypointIndex = 0;
targetWaypoint = waypoints[waypoints.Count - 1];
}
else
{
lastWaypointIndex = waypoints.Count - 1;
targetWaypoint = waypoints[targetWaypointIndex]; //Set the first target waypoint at the start so the enemy starts moving towards a waypoint
}
}
// Update is called once per frame
void Update()
{
if (includeTransformPosition && includeTransform)
{
waypoints.Insert(0,go.transform);
includeTransform = false;
}
else
{
if (includeTransformPosition == false)
{
waypoints.Remove(go.transform);
includeTransform = true;
}
}
float movementStep = movementSpeed * Time.deltaTime;
float rotationStep = rotationSpeed * Time.deltaTime;
Vector3 directionToTarget = targetWaypoint.position - transform.position;
Quaternion rotationToTarget = Quaternion.LookRotation(directionToTarget);
transform.rotation = Quaternion.Slerp(transform.rotation, rotationToTarget, rotationStep);
float distance = Vector3.Distance(transform.position, targetWaypoint.position);
CheckDistanceToWaypoint(distance);
if(slowdownDistance < 7f)
{
movementSpeed -= movementSpeed * Time.deltaTime;
}
transform.position = Vector3.MoveTowards(transform.position, targetWaypoint.position, movementStep);
}
void CheckDistanceToWaypoint(float currentDistance)
{
if (currentDistance <= waypointDistance)
{
targetWaypointIndex++;
UpdateTargetWaypoint();
}
}
void UpdateTargetWaypoint()
{
if (targetWaypointIndex > lastWaypointIndex)
{
targetWaypointIndex = 0;
}
targetWaypoint = waypoints[targetWaypointIndex];
}
}
At this part I'm trying to slowdown the movement speed but it's not changing the speed at all :
if(slowdownDistance < 7f)
{
movementSpeed -= movementSpeed * Time.deltaTime;
}
What I'm trying to do when the transform start to move increase the speed slowly to some constant speed and then when the transform is getting closer to the waypoint then if the distance is less then 7 decrease the speed down to 0 so the object will stop at the waypoint then after X seconds move back the transform to the transform original position(go.transform) with the same increasing decreasing speed movement.
but I can't even make the first simple slowdown.
you set slowdownDistance to 7, i dont see you ever reducing it below 7 but you have if statement that executes only it it is under 7 do you reduce it elsewhere?
You probably planned to compare slowdownDistance to some distance rather than comparing it to its own value.
If you want to slow down the whole scene you could use a:
while(distance < range)
{
Time.timeScale = 1 - (distance / value);
}
with this you can change the value and so when the player come close time will be slown down mostly.
With the same idea you can do:
while(distance < range)
{
speed = speedInitial - (distance / value);
}
If you want to change just the speed of the player

Player Object spins continuously when moving joystick

I have created a twin stick movement system for mobile. I have a character moving under the influence of one joystick correctly. The other joystick, the design I want is:
When the shoot JS is moved, look in that direction.
When the shoot JS is released, shoot in the last aimed direction.
What's happening is, the character shoots continuously when the game starts and if I move the ShootJS, the character spins in circles. I'm completely flummoxed as to why this is happening.
Here is my code. Thanks in advance to anybody for any help you provide.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
using Ludiq;
using Bolt;
public class PlayerJSControlSc : MonoBehaviour
{
public GameObject player;
public NavMeshAgent nav;
public Text stateText;
public float moveSpeed;
public Animator animator;
public FloatingJoystick moveJS;
public FloatingJoystick shootJS;
public float rotationSpeed = 10;
public int ammo;
public int mag;
public Transform shotSpawn;
public GameObject bullet;
public float reloadTime;
public Text ammoCount;
[HideInInspector]
int currentMag;
// Start is called before the first frame update
void Start()
{
stateText.text = "";
nav = player.GetComponent<NavMeshAgent>();
animator = player.GetComponent<Animator>();
moveJS = GameObject.Find("Floating JS_Move").GetComponent<FloatingJoystick>();
shootJS = GameObject.Find("Floating JS_Shoot").GetComponent<FloatingJoystick>();
}
// Update is called once per frame
void Update()
{
movePlayer();
playerShoot();
ammoCount.text = currentMag+ "/" + ammo;
}
public void movePlayer()
{
//float x = Input.GetAxis("Horizontal");
//float y = Input.GetAxis("Vertical");
float x = moveJS.Horizontal;
float y = moveJS.Vertical;
nav.velocity = new Vector3(x * moveSpeed, 0, y * moveSpeed);
if (nav.velocity.x != 0 || nav.velocity.z != 0)
{ animator.SetBool("isRunning", true); }
else { animator.SetBool("isRunning", false); }
}
public void playerShoot()
{
bool isAiming = false;
float x = shootJS.Horizontal; float z = shootJS.Vertical;
if (x != 0 || z != 0)
{
isAiming = true;
/* Vector3 lookDir = new Vector3(x, 0, z);
Quaternion lookRotation = Quaternion.LookRotation(lookDir, Vector3.up);
float step = rotationSpeed * Time.deltaTime;
player.transform.rotation = Quaternion.RotateTowards(lookRotation, transform.rotation, step);*/
float myAngle = Mathf.Atan2(x, z) * Mathf.Rad2Deg;
float bodyRotation = myAngle + player.transform.eulerAngles.y;
player.transform.Rotate( 0,myAngle,0,Space.World);
}
else { shoot();isAiming = false; }
}
void shoot()
{
if (currentMag > 0)
{
Instantiate(bullet, shotSpawn.position, shotSpawn.rotation);
currentMag--;
}
else if (currentMag=0)
{
StartCoroutine(reload());
}
else
return;
}
IEnumerator reload()
{
ammo = ammo - mag;
currentMag = mag;
yield return new WaitForSeconds(reloadTime);
}
}
well you are calling every frame playerShoot() -> not moving -> shoot().
And in the same way also playerShoot() -> not moving -> player.transform.Rotate( 0,myAngle,0,Space.World);.
I think what you rather should do is
doing the shoot only in the very first frame where both inputs are 0
for the move rather set the rotation instead of increasing it
Something like
private bool lastFrameMoved;
public void playerShoot()
{
float x = shootJS.Horizontal;
float z = shootJS.Vertical;
if (x != 0 || z != 0)
{
var lookDir = new Vector3(x, 0, z);
var lookRotation = Quaternion.LookRotation(lookDir, Vector3.up);
player.transform.rotation = lookRotation;
lastFrameMoved = true;
}
else
{
if(lastFrameMoved)
{
shoot();
}
lastFrameMoved = false;
}
}
I solved the look rotation in Bolt (because it helps me visualize). I'll solve the shoot part soon. I'm using the wonderful Joystick Pack by Fenerax Studios.
https://assetstore.unity.com/publishers/32730

Updating X,Y,Z floats and specific While Loop Scenario C#

I have 2 problems I want to get through.
1st problem. Changing X,Y,Z movement and updating it.
I have a driving mechanic in my game where if the user presses wasd the car moves. The problem is that I want the speed to be reduced from 15f to 5f when S(backwards) is pressed and held until released.
Here is my code below of my player script.
Please refer to void playerwalk() and backwards motion()
public class Move : MonoBehaviour
{
public float speed;
private Rigidbody rb;
private int count;
public Text countText;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
}
void playerWalk()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 75f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 15f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, -z);
}
void backwardMotion()
{
if (Input.GetKeyDown(KeyCode.S))
{
var z = Input.GetAxis("Vertical") * Time.deltaTime * 5f;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Collectable"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
}
// Update is called once per frame
void Update ()
{
backwardMotion();
playerWalk();
}
}
This currently is not affecting anything and the void backwardsmotion() seems to have no effect.
Do I need a while loop?
What is the syntax for do until in C#?
2nd Problem : Do until loop?
To those of you who haven't seen my recent posts, I also have a flashlight mechanic on my car model where if you press L, the flashlights will turn on(toggle) and if you press S the backlights will as you reverse.
Here is my flashlight game mechanic code:
Please refer to Void switchoffLights()
public class headLights : MonoBehaviour
{
public Light frontlights;
public Light frontlights2;
public Light frontlights3;
public Light frontlights4;
public Light backlights;
public Light backlights2;
public Light backlights3;
public Light backlights4;
// Use this for initialization
void Start()
{
frontlights.enabled = !frontlights.enabled;
frontlights2.enabled = !frontlights2.enabled;
frontlights3.enabled = !frontlights3.enabled;
frontlights4.enabled = !frontlights4.enabled;
}
void switchoffLight()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
if (frontlights && frontlights2 && frontlights3 && frontlights4 != null)
{
frontlights.enabled = !frontlights.enabled;
frontlights2.enabled = !frontlights2.enabled;
frontlights3.enabled = !frontlights3.enabled;
frontlights4.enabled = !frontlights4.enabled;
}
}
if (Input.GetKeyDown(KeyCode.S))
{
if (backlights && backlights2 && backlights3 && backlights4 != null)
{
backlights.enabled = !backlights.enabled;
backlights2.enabled = !backlights2.enabled;
backlights3.enabled = !backlights3.enabled;
backlights4.enabled = !backlights4.enabled;
}
}
}
}
However the backlights do not turn off when the S key is released.
I could not clearly understand what was going on with other codes and answers which is why I am asking here.
Thank you in advance! I am only just starting for my grade 10 programming class so please explain like you would to a younger person.
You are not applying any change to transform or rotation when pressing
if (Input.GetKeyDown(KeyCode.S))
{var z = Input.GetAxis("Vertical") * Time.deltaTime * 5f;
}
// Where this z go ?????
// Add this or whatever you want "transform.Translate(0, 0, -z);"
For Second question Use GetKeyUp() in another condition or use getkey in same condition
For the first question, you declared var z twice but they are actually individual values.
var z = Input.GetAxis("Vertical") * Time.deltaTime * 15f; // playerWalk()
and
var z = Input.GetAxis("Vertical") * Time.deltaTime * 5f; // backwardMotion()
refer to two different variables with the same name z but each only live in the method it is declared in. The z in playerWalk() and the z in backwardMotion() has nothing to do with each other. The z in backwardMotion() essentially serves no purpose as the variable is declared but never used anywhere in the method.
The correct way to achieve what you want will be:
void playerWalk()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 75f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 15f;
if (Input.GetKeyDown(KeyCode.S))
{
// If "S" is pressed, the z speed would be reduced to 5f.
z = Input.GetAxis("Vertical") * Time.deltaTime * 5f;
}
transform.Rotate(0, x, 0);
transform.Translate(0, 0, -z);
}
For the second question, there are three way to get input: Input.GetKeyDown, Input.GetKey and Input.GetKeyUp.
Input.GetKeyDown is called only once on the frame you pressed the key.
Input.GetKey is called every frame the key is held down.
Input.GetKeyUp is called only once on the frame you released the key.
In your case, your car backlight would be turned on when it detects a Input.GetKeyDown(KeyCode.S) but nothing lets it turn back off when it detects a release.
The solution would be to simply check for a release and turn off the headlight:
if (Input.GetKeyUp(KeyCode.S)) // Detects a release of "S" key
{
if (backlights && backlights2 && backlights3 && backlights4)
{
backlights.enabled = false;
backlights2.enabled = false;
backlights3.enabled = false;
backlights4.enabled = false;
}
}

Categories

Resources