I have an object that has a script that rotates it towards the player.
The model does this, but flips around 180 degrees. Flipping the object in the scene does nothing, it just still follows the player backwards.
It is probably something to do with the actual model, But is there anything I can do in the script to fix it so the model turns around 180 degrees on the y axis?
inputMovement = objPlayer.transform.position - transform.position;
inputRotation = inputMovement; // face the direction we are moving
The script is a modified version from daverisers evac city tutorial, here it is.
using UnityEngine;
using System.Collections;
public class AIScript : MonoBehaviour {
//game objects
private GameObject objPlayer;
private GameObject objCamera;
//input variables
private Vector3 inputRotation;
private Vector3 inputMovement;
//identitiy variables
public float moveSpeed = 100f;
private bool thisIsPlayer;
//calculation variables
private Vector3 tempVector;
private Vector3 tempVector2;
//enemy stuff
private bool AttackState;
// Use this for initialization
void Start ()
{
objPlayer = (GameObject) GameObject.FindWithTag("Player");
objCamera = (GameObject) GameObject.FindWithTag("MainCamera");
if(gameObject.tag == "Player")
{
thisIsPlayer = true;
}
}
// Update is called once per frame
void Update ()
{
FindInput();
ProcessMovement();
HandleAnimation();
if (thisIsPlayer == true)
{
HandleCamera();
}
}
void FindInput ()
{
if(thisIsPlayer == true)
{
FindPlayerInput();
}else{
FindAIinput();
}
}
void FindPlayerInput ()
{
//find vector to move
inputMovement = new Vector3( Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical") );
//find vector to the mouse
tempVector2 = new Vector3( Screen.width * 0.5f, 0 , Screen.height * 0.5f); // the position of the middle of screen
tempVector = Input.mousePosition; // find the position of the mouse on screen
tempVector.z = tempVector.y; // input mouse position gives us 2D coordinates, I am moving the Y coordinate to the Z coordinate in temp Vector and setting the Y coordinate to 0, so that the vector will read the input along the X and Z axis, and not the X and Y axis.
tempVector.y = 0;
inputRotation = tempVector - tempVector2; // the direction we want to face/aim/shoot is from the middle of the screen to where the mouse is pointing.
}
void FindAIinput()
{
inputMovement = objPlayer.transform.position - transform.position;
inputRotation = inputMovement; // face the direction we are moving
AttackState = false;
if(Vector3.Distance(objPlayer.transform.position, transform.position) < 1.2f)
{
AttackState = true;
inputMovement = Vector3.zero;
}
}
void ProcessMovement ()
{
rigidbody.AddForce (inputMovement.normalized * moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(inputRotation);
transform.eulerAngles = new Vector3( 0, transform.eulerAngles.y + 180, 0);
transform.position = new Vector3(transform.position.x, 0 , transform.position.z);
}
void HandleCamera ()
{
objCamera.transform.position = new Vector3(transform.position.x, 20 , transform.position.z);
objCamera.transform.eulerAngles = new Vector3(90, 0 , 0);
}
void HandleAnimation ()
{
if(inputMovement.magnitude > 0)
{
animation.Play("Walk", PlayMode.StopAll);
}
else if (thisIsPlayer == false AttackState == true)
{
animation.Play("attack", PlayMode.StopAll);
}else{
animation.Stop();
}
}
}
transform.eulerAngles = new Vector3( 0, transform.eulerAngles.y + 180, 0);
from your code.
You already set its rotation on the previous line. This line here makes it flip 180 degrees on its Y axis which is probably what’s making your transform move backwards.