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

How do I make an object face forward while it is orbiting another object?

$
0
0
I have a simple enemy in my game which orbits the player if it comes within a certain distance of them, the enemy orbits correctly and everything else carries on correctly. But I cannot figure out how to make the enemy appear that it is facing forward, so the enemy rotates facing the player and looking very strange. So how can I make the enemy face the way it is moving while orbiting? Here is the movement section of my code using UnityEngine; using System.Collections; public class MineLayerAI : MonoBehaviour { public GameObject target; public Transform Target; public float rotationSpeed = 3.5F; public float moveSpeed = 3.5F; public Transform enemyTransform; public Rigidbody MinePrefab; public float WaitLayTime = 2.0f; public bool LayTime = false; public float OrbitSpeed = 50.0f; public bool wallHit = true; void Awake(){ enemyTransform = transform; if(!target) { target = GameObject.FindWithTag("Player"); } if(!Target) { Target = GameObject.FindWithTag("Player").transform; } } void Update () { float currentDistance = Vector3.Distance(transform.position, Target.position); if(currentDistance > 2.5) { //rotate towards player enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime); //move towards player enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime; } else { if(wallHit == true) { //rotate towards player enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime); //orbits around the player transform.RotateAround (Target.position, Vector3.up, OrbitSpeed * Time.deltaTime); } if(wallHit == false) { //rotate towards player enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime); //orbits around the player transform.RotateAround (Target.position, Vector3.down, OrbitSpeed * Time.deltaTime); } } Thank you in advance for any help Jack Perry

Viewing all articles
Browse latest Browse all 133

Trending Articles



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