unity 2d rotate towards direction
C#
/// <summary>
/// Rotate a gameobject to face a direction in 2D space with offet
/// </summary>
/// <param name="target"></param>
/// <param name="RotationSpeed"></param>
/// <param name="offset"></param>
private void RotateGameObject(Vector3 target, float RotationSpeed, float offset)
{
//https://www.youtube.com/watch?v=mKLp-2iseDc
//get the direction of the other object from current object
Vector3 dir = target - transform.position;
//get the angle from current direction facing to desired target
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
//set the angle into a quaternion + sprite offset depending on initial sprite facing direction
Quaternion rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));
//Roatate current game object to face the target using a slerp function which adds some smoothing to the move
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, RotationSpeed * Time.deltaTime);
}
Also in C#:
- Title
- c# loop through object
- Category
- C#
- Title
- how to allow user import image c#
- Category
- C#
- Title
- iframe set html content c#
- Category
- C#
- Title
- application variable in asp.net core
- Category
- C#
- Title
- What is a class in c#
- Category
- C#
- Title
- clear combobox c#
- Category
- C#
- Title
- reference to gameobject in different scene unity
- Category
- C#
- Title
- unity t-flip flop
- Category
- C#
- Title
- use newtonsoft json to clone object
- Category
- C#
- Title
- get day month year from date c#
- Category
- C#
- Title
- c# print
- Category
- C#
- Title
- vb.net drag window without titlebar
- Category
- C#
- Title
- wpf make size fill all grid
- Category
- C#
- Title
- unity making homing rocket
- Category
- C#
- Title
- C# colours
- Category
- C#
- Title
- c# Authorization has been denied for this request
- Category
- C#
- Title
- create new object from generic c#
- Category
- C#
- Title
- c# input integer
- Category
- C#
- Title
- how to find object by ag unity
- Category
- C#
- Title
- binding c#
- Category
- C#
- Title
- functions unity
- Category
- C#
- Title
- how to trigger event when a com device is connected in c#
- Category
- C#
- Title
- how to create a variable in C#
- Category
- C#
- Title
- get user directory of file in c#
- Category
- C#
- Title
- mvc c# w3schools
- Category
- C#
- Title
- c# getter setter
- Category
- C#
- Title
- create line in unity
- Category
- C#
- Title
- unity gameobject.find
- Category
- C#
- Title
- move towards target unity
- Category
- C#
- Title
- c# number suffixes
- Category
- C#
- Title
- write last line txt file c#
- Category
- C#
- Title
- ping with c#
- Category
- C#
- Title
- how to set progress openedge driver name for odbc connection c#
- Category
- C#
- Title
- how to add system.messaging c#
- Category
- C#
- Title
- how to make error sound c#
- Category
- C#
- Title
- Move player on planets in unity 2d
- Category
- C#
- Title
- how to get the last element in an array in c#
- Category
- C#
- Title
- sqlite connection c#
- Category
- C#
- Title
- iformfile blobclient
- Category
- C#
- Title
- c# double question mark
- Category
- C#
- Title
- c# check to see if dictionary key exists
- Category
- C#
- Title
- unity how to get transform scale
- Category
- C#
- Title
- first sentence letter capital in c#
- Category
- C#
- Title
- c# loop through two dimensional array
- Category
- C#
- Title
- convert from xls to xlsx C#
- Category
- C#
- Title
- c# making a folder
- Category
- C#
- Title
- how to detected WindowCloseEvent in other window wpf
- Category
- C#
- Title
- C# foreach loop async but wait at end
- Category
- C#
- Title
- out parameters c#
- Category
- C#
- Title
- change partial view based on select asp.net core
- Category
- C#
- Title
- c# get hwid
- Category
- C#
- Title
- C# random.Next error
- Category
- C#
- Title
- xamarin hide back button
- Category
- C#
- Title
- c# inotifypropertychanged best practices
- Category
- C#
- Title
- c# list tuple
- Category
- C#
- Title
- c# fold list
- Category
- C#
- Title
- array copy c#
- Category
- C#
- Title
- bold caption latex
- Category
- C#
- Title
- how do i limit the amount of prefabs in unity using c# script
- Category
- C#
- Title
- c# reverse list
- Category
- C#
- Title
- c# convert double to int
- Category
- C#
- Title
- resize image c#
- Category
- C#
- Title
- c# inline initialize dictionary
- Category
- C#
- Title
- arry in c#
- Category
- C#
- Title
- top down movement unity
- Category
- C#
- Title
- convert string array to int C#
- Category
- C#
- Title
- .net identity seed users and roles
- Category
- C#
- Title
- wpf textblock line break code behind
- Category
- C#
- Title
- c# switct case
- Category
- C#
- Title
- convert number of days into months c#
- Category
- C#
- Title
- C# public 2d array
- Category
- C#
- Title
- c# get last character of string
- Category
- C#
- Title
- c# datafield change cell background color
- Category
- C#
- Title
- c# how to initialize an array
- Category
- C#
- Title
- c# append to file
- Category
- C#
- Title
- how to make an object move in unity
- Category
- C#
- Title
- c# find duplicates in list of strings
- Category
- C#
- Title
- whats a string
- Category
- C#
- Title
- unity round
- Category
- C#
- Title
- unity how to make a ui disappear
- Category
- C#
- Title
- How to search for a string from readline in c#
- Category
- C#
- Title
- unity deactivate scripts in list
- Category
- C#
- Title
- c# return multiple value unity
- Category
- C#
- Title
- transform object according to its parent unity
- Category
- C#
- Title
- c# get list of all class fields
- Category
- C#
- Title
- c sharp substring
- Category
- C#
- Title
- c sharp check if key in dictionary
- Category
- C#
- Title
- c# creating exceptions
- Category
- C#
- Title
- 2d character controller unity
- Category
- C#
- Title
- c# windows grab screenshot
- Category
- C#
- Title
- unity onselect gizmos wireframe
- Category
- C#
- Title
- enums c#
- Category
- C#
- Title
- wpf binding ancestor codebehind
- Category
- C#
- Title
- how to initiate a varaible in cs
- Category
- C#
- Title
- unity c# write line
- Category
- C#
- Title
- how to generate random unique id in c#
- Category
- C#
- Title
- c# winform remove button border
- Category
- C#
- Title
- unity3d raycast
- Category
- C#
- Title
- how to find the type of a object c#
- Category
- C#
- Title
- asp.net model display name
- Category
- C#