I am on my third day of Unity training and am currently doing a roll tutorial [/ a>. I am trying to add a restart button that teleports the ball back to its original position / origin. Everything works well, but the ball will continue to move after a restart, but I want it to stay in place and not move by itself.
I mean, if I move left at maximum speed and press “R” to restart, then it will continue to move left quickly for some time, but I want to ensure that the ball does not continue to move by itself after restarting it position (perhaps the speed simply decreases so slowly that it is invisible, in which case I would like it to slow down faster).
Here, my code that I just tried and “hoped” will work, but does not. I think the problem I have here is that the new value of the Horizontal / axisVertical variable is again assigned to the next frame, the value of which is taken from Input.GetAxis, which from what I heard cannot be changed.
This means that the motion of the GeForce is greater than 0/0/0 and AddForce will be activated again in the next frame.
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public float movementSpeed = 7f;
private Rigidbody rigidSphere;
void Start() {
rigidSphere = GetComponent<Rigidbody>();
}
void FixedUpdate() {
float axisHorizontal = Input.GetAxis("Horizontal");
float axisVertical = Input.GetAxis("Vertical");
Vector3 movementForce = new Vector3(axisHorizontal, 0.0f, axisVertical);
rigidSphere.AddForce(movementForce * movementSpeed);
if (Input.GetKeyDown(KeyCode.R)) {
transform.position = new Vector3(0, 0, 0);
rigidSphere.AddForce(0, 0, 0);
axisHorizontal = 0;
axisVertical = 0;
}
}
}
I tried using Google to change the value that GetAxis takes, but others claim it cannot be done. Therefore, I have no idea how to make him not move forever ...
And I also had a “bonus” question that arose during these two days when I studied Unity, but they are not worth a separate question, perhaps if they already answered earlier (I could not find any documentation that would help me with this) and then just give me a link where I can find the answer to my problem.
, , (W/A/S/D, / Input Manager)? , "W" , .
, , , , , "W" , , , GetAxis 0. google , , - Gravity Input Manager - , , , , - .
, - .