- Home /
How to make that a GameObject look with a Look At() in the opposite direction to the force exerted on it?
For example, if you increase in the "X and Y axes" that you look at a side opposite to that assigned force
It is to make an oblique 2D shot for a game like the one in the image
Answer by Sabre-Runner · Jul 07, 2018 at 05:30 AM
If you have the force vector and/or you have the direction of movement as a vector X, you can use them to determine the direction in which you want to look. Then use Quaternion.SetFromToRotation to create a rotation from your current look vector to the desired one. Then apply it to your object's rotation quaternion.
Answer by ElectrikVocal095 · Jul 07, 2018 at 08:24 AM
Thanks you for answering! Finally I designed this C# script, and then playing with the variables I could give that attack effect.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtForce : MonoBehaviour
{
Rigidbody2D rb;
public float forceInX, forceInY;
public int RotateDegree;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
transform.Rotate (new Vector3(0,0,-RotateDegree));
}
void Update ()
{
rb.AddForce (new Vector2(forceInX,forceInY), ForceMode2D.Impulse);
}
}
But I still do not know how to rotate a GameObject depending of the direction of this :(
If you already know the force exactly (If it's not physics based) then just take the reverse vector to that and do a SetFromToRotation from the force vector to its opposite.