简体   繁体   中英

Rotating sprite to face vector in unity

I have a zombie gameObject in unity along with a tower gameObject. I have tryed a bunch of things to get the enemy to face the location of a nested object inside the tower and nothing has worked.

I've already tried using Mathf.Acos to no effect. The closest i got was using this code; but it still obscures the intended direction slightly and i haven't found a pattern yet.

Vector2 direction = centralTower.transform.position + centralTower.transform.parent.transform.position - gameObject.transform.position;

transform.up = direction;
gameObject.transform.position += transform.up;

I know the first line looks like a mouthful but basically it can be simplified to this

direction = (TowerPosition + TowerChild(attakcingPoint)Position) - enemyPosition

BTW: central Tower variable refers to the attacking Point

I really don't have any idea what is happening and I've been researching angles for days now to no effect.

一个类似的问题提供了答案transform.right = target.position - transform.position;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM