Quantcast
Channel: Questions in topic: "quaternion.slerp"
Viewing all articles
Browse latest Browse all 133

Rotating gameObject to aim at target using Quaternion

$
0
0
![alt text][1] As shown in the above image, I have a Scene where a player and an enemy face each other with a goal to hurt one another using the gun. Here's what actually happens in the scene: Player manually aims the gun at the enemy using two sliders; one controls the Y-axis rotation (rotate left or right) and the other controls the X-axis rotation (rotate up or down). I intentionally made the velocity of the projectile to be low in order to force the player to aim the gun barrel at an angle -- and this results in a parabolic motion of the projectile. Once the player's projectile is consumed (i.e., hits something/anything), it is the enemy's turn to counterattack. I set a boolean variable to 'true' to let the enemy's script know that it's time for the enemy's gun to fire: canReact = true; And the enemy's script is as follows: ... private bool canReact = false; private bool shotFired = false; private GameObject gunBase; private GameObject gunMount; private GameObject target; // target is the player private Vector3 gunBaseRelativePos; private Vector3 gunMountRelativePos; private Quaternion gunBaseRotation; private Quaternion gunMountRotation; void FixedUpdate() { if (canReact == true) { if (shotFired == false) { gunBase = GameObject.FindGameObjectWithTag("enemy_gun_base"); gunMount = GameObject.FindGameObjectWithTag("enemy_gun_mount"); target = GameObject.Find("Player"); gunBaseRelativePos = target.transform.position - gunBase.transform.position; gunMountRelativePos = target.transform.position - gunMount.transform.position; baseRotation = Quaternion.LookRotation(gunBaseRelativePos); mountRotation = Quaternion.LookRotation(gunMountRelativePos); gunBase.transform.rotation = Quaternion.Slerp(gunBase.transform.rotation, baseRotation, Time.deltaTime); gunMount.transform.rotation = Quaternion.Slerp(gunMount.transform.rotation, mountRotation, Time.deltaTime); // Notice here that I'm only instantiating the projectile, not firing. // I'm only concerned about getting the Gun's rotation right first. clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody; // using the 'shotFired' flag to prevent the enemy's gun from shooting continuously shotFired = true; } // below code tells the camera to look at & follow the projectile while it is present in the scene if (clone != null) { camera.transform.LookAt(clone.transform); camera.transform.position = clone.transform.position - distance; } else { // projectile has been destroyed, so reset the flag shotFired = false; } } } Basically, what I'm trying to accomplish using the above code is to have the enemy's gun take an aim at the player -- and this movement would involve two steps: 1) rotate the 'gun base' around Y-axis, and 2) rotate the 'gun mount' around X-axis for the firing angle. When I run the scene, the 'gun base' rotates slightly to the LEFT (it should actually rotate to the RIGHT) and the 'gun mount' (which controls the angle) doesn't move at all. So, the obvious questions are: 1) How do I make the 'gun base' rotate correctly so that it faces the direction of the player? 2) How do I make the 'gun mount' to rotate so that it actually fires its projectile at an angle given a velocity of the projectile? When I test only the firing process, the projectiles shoots in a straight line like laser beam. [1]: /storage/temp/42987-ground.png

Viewing all articles
Browse latest Browse all 133

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>