Hello Unity! I was wondering if someone could tell me how to make a 2D sprite face away from my player. Also, could you show me how to make my sprite wiggle as it moves.
Use world X axis to detect if player is to enemy’s right or left and use SpriteRenderer.flipX to flip a sprite. Simple wiggling animation can be done by scaling a sprite on one of the axes.
Could you show me an example of that? Like, a piece of sample code? I am a noob at this so I don’t really know how to write code in Unity.
something like this ? for left faced sprite
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//script name
public class facingAway : MonoBehaviour {
//here will be link to the player
public GameObject Player;
//we need link to sprite renderer, variable for this link
SpriteRenderer sr;
void Start () {
//make link to the player in the scene, the player should have tag Player
Player = GameObject.FindWithTag ("Player");
//make link to the sprite renderer component
sr = GetComponent <SpriteRenderer> ();
}
void Update () {
//check, is player left ?
//second condition after && is only to prevent the flipping every frame
//we dont need set flip to true if it is already true
if (Player.transform.position.x < transform.position.x && !sr.flipX) {
//flip sprite on x
sr.flipX = true;
}
//check, is player right ?
if (Player.transform.position.x > transform.position.x && sr.flipX) {
sr.flipX = false;
//flip sprite on x
}
}
}
or you can scale gameobject to oposite
SWEET!! Thanks!
Also, would there be a way to rotate the sprite based on the position of the player instead of flipping the sprite? That is what I meant to say.
there is not much difference
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class facingAway : MonoBehaviour {
public GameObject Player;
public bool isLeft;
void Start () {
//make link to player in the scene, should have tag Player
Player = GameObject.FindWithTag ("Player");
}
void Update () {
//check, is player left ?
//and object is looking left
if (Player.transform.position.x < transform.position.x && isLeft) {
//switch bool (which shows the looking direction) to opposite
isLeft = !isLeft;
//scale on x to opposite, like turn
transform.localScale = new Vector3 (transform.localScale.x *-1, transform.localScale.y, transform.localScale.z);
}
//check, is player right ?
//and object is looking right
if (Player.transform.position.x > transform.position.x && !isLeft) {
//switch bool (which shows the looking direction) to opposite
isLeft = !isLeft;
//scale on x to opposite, like turn
transform.localScale = new Vector3 (transform.localScale.x *-1, transform.localScale.y, transform.localScale.z);
}
}
}
or you will something like negative LookAt ? Sorry, I am not good at quaternions ![]()
You should calculate directions vector to player (subtraction), calculate angle (mathf.atan2*matf.rad2deg) and rotate.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotateObject : MonoBehaviour {
public GameObject Player;
float angle;
Vector3 dir;
void Start () {
Player = GameObject.FindWithTag ("Player");
}
void Update () {
Vector2 dir = Player.transform.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
}
Vector2 dir = Player.transform.position - transform.position;
transform.rotation = Quaternion.LookRotation(dir.normalized);
if you want to use the Quaternion way, and if you want to smoothly rotate use lerp e.g
Quaternion mRotation = Quaternion.LookRotation(dir.normalized);
transform.rotation = Quaternion.Lerp(transform.rotation, mRotation, TurningSpeed * Time.deltaTime);
Thanks guys!