Let's say you have a shooter game and its just plain to hard. Your Enemies are perfect shots and can hit you straight on every time. Making the game a little more fair would be nice, and one way doing so is by dumbing down your enemies AI a bit, and making them a less accurate with aiming.
My method of aiming and inaccuracy is done like this. I use this for cannons, turrets, etc.
-------------------------------------------------------------------------------------------------------------
var target = GameObject.Find("My Target");
var randPos = Random.Range(0.9, 1.1);
var tarPos = Vector3(target.transform.position.x * randPos, target.transform.position.y * randPos, target.transform.position.z * randPos);
var rotate = Quaternion.LookRotation(tarPos - transform.position);
function Update(){
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime);
}
-------------------------------------------------------------------------------------------------------------
Explanation:
var target = GameObject.Find("My Target");
I use this simple way of obtaining a target for this example. Finds a game object in the scene named "My Target". Read more here.
var randPos = Random.Range(0.9, 1.1);
When ever the variable is called, It will produce a random float number between the minimum and maximum numbers you set. Read more from original content here.
var tarPos = Vector3(target.transform.position.x * randPos, target.transform.position.y * randPos, target.transform.position.z * randPos);
var rotate = Quaternion.LookRotation(tarPos - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime);
My method of aiming and inaccuracy is done like this. I use this for cannons, turrets, etc.
-------------------------------------------------------------------------------------------------------------
var target = GameObject.Find("My Target");
var randPos = Random.Range(0.9, 1.1);
var tarPos = Vector3(target.transform.position.x * randPos, target.transform.position.y * randPos, target.transform.position.z * randPos);
var rotate = Quaternion.LookRotation(tarPos - transform.position);
function Update(){
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime);
}
-------------------------------------------------------------------------------------------------------------
Explanation:
var target = GameObject.Find("My Target");
I use this simple way of obtaining a target for this example. Finds a game object in the scene named "My Target". Read more here.
var randPos = Random.Range(0.9, 1.1);
When ever the variable is called, It will produce a random float number between the minimum and maximum numbers you set. Read more from original content here.
var tarPos = Vector3(target.transform.position.x * randPos, target.transform.position.y * randPos, target.transform.position.z * randPos);
Gets the targets vector 3 coordinates but multiplies each position by the random range. Doing this returns an inaccurate position.
Returns an inaccurate rotation due to multiplying the original target position with the random range. Read more from original content here.
function Update()
Called every frame. Meaning it will keep a constant rotation towards it's target. This also means the random position variable is being called every frame, which means it will be constantly changing. As will the rotation.
Sets the models rotation from the currents rotation to the rotate position variable. This is done over Time.deltaTime. Read more here.
If you have any questions or suggestions please leave them in the comments below. Some feedback would be nice. If I need to be more specific or clear please also say.
Comments
Post a Comment