What I want to achieve is :
I have a 3D game - with player in third person - and using cinemachine Camera - imagine it like PUBG where you can look any where - the direction of the player does not change until the player is moving - the direction of character change in the same direction as camera
I want to make some sort of Shadow character as enemy , it will face the player - and mimic every movement exactly - not harmful
so when the player move forward - the other copy also move toward player - thus blocking his movement
so I want to achieve this using RigidBody - as Physics is needed to solve this puzzle
I tried different approaches- until I got to this code - it works OK - until I change the camera direction and then every thing is not working any more
public Transform cam ;
float speed = 10f; //How fast the player can move
float turnSpeed = 100; //How fast the player can rotate
public Animator animator; //Animation Control
Rigidbody rigidBody; //Make sure you have a rigidbody
void Start()
{
//Initialize appropriate components
animator = GetComponent<Animator> ();
rigidBody = GetComponent<Rigidbody>();
cam = Camera.main.transform;
}
//No need for update function right now, physics work better in Fixed Update
void FixedUpdate()
{
//right is shorthand for (1,0,0) or the x value forward is short for (0,0,1) or the z value
Vector3 dir = (-cam.forward * Input.GetAxis("Vertical"))+ (cam.right * Input.GetAxis("Horizontal")) ;
dir.y = 0;//Keeps character upright against slight fluctuations
if (Input.GetAxis("Horizontal") != 0 ||Input.GetAxis("Vertical") != 0)
{
//rotate from this /........to this............../.........at this speed
rigidBody.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (dir), turnSpeed * Time.deltaTime);
rigidBody.velocity = transform.forward * speed;
animator.SetFloat ("Blend", 10);//Walk or run Blend works well here
} else
{
rigidBody.velocity = new Vector3(0,0,0).normalized;
animator.SetFloat ("Blend", 0);//Idle Blend works well here
}
}
}