Completely new to unity and coding and I’m having trouble figuring this out on my own. I am at the “incrementing a score” lecture for on the unity 3d course.
Any script I add for “OnCollisionEnter” ends up detecting mass amounts of hits for any bump. Meaning I bump a wall and get 10-20 messages in the debug.log. (regardless of whether the script is on the object or player)
In the first lessons it was noted that you could keep pressing forward against the wall and only register one hit, but this was not the case for me. Holding forward continues to add dozens of messages to the console. I figured I would understand more by this point to be able “fix” the issue on my own, but now implementing a “scorer” script that is counting mass amounts of imprecise hits, seems like it will compound problems later.
Any insight anyone could provide would be super appreciated, I can provide more info if needed. I’ve pasted my relevant scripts below, including my “Mover” script which I modified slightly to suit a “vehicle” as the “player.” This is currently the only script that has been modified from the course instruction but the modification was only to make the player rotate instead of sliding horizontally and only rotate the player if moving forward or backward.
My “ObjectHit” script
public class ObjectHit : MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
Debug.Log("Crash Happened");
GetComponent<MeshRenderer>().material.color = Color.red;
}
}
My “Scorer” script
public class Scorer : MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
Debug.Log ("You crashed");
}
}
My “Mover” script
public class Mover : MonoBehaviour
{
[SerializeField] float moveSpeed = 10f;
[SerializeField] float rotateSpeed = 200f;
// Start is called before the first frame update
void Start()
{
PrintInstructions();
}
// Update is called once per frame
void Update()
{
MovePlayer();
}
void PrintInstructions()
{
Debug.Log("Welcome to the Game");
Debug.Log("Keyboard? Use WASD");
Debug.Log("Controller? Right and Left trigger = Gas/Brakes. Left stick = Turn");
Debug.Log("Don't Crash!");
}
void MovePlayer()
{
float zValue = Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime;
float yValue = Input.GetAxis ("Horizontal") * rotateSpeed * Time.deltaTime;
transform.Translate(0,0,zValue);
//Only rotate if moving
if( Input.GetAxis("Vertical") != 0)
transform.Rotate(0f, yValue, 0f);
}
}