rotate to face 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
- create new object from generic c#
- Category
- C#
- Title
- instantiate offset unity
- Category
- C#
- Title
- c# retrieve files in folder
- Category
- C#
- Title
- unity object array
- Category
- C#
- Title
- unity transfoprm position y change
- Category
- C#
- Title
- c# list.foreach
- Category
- C#
- Title
- decimals not stored in azure tables
- Category
- C#
- Title
- unity get max occurrence in list
- Category
- C#
- Title
- asp.net core task iactionresult
- Category
- C#
- Title
- collision detector unity c# 2d
- Category
- C#
- Title
- how to a equall to b in c#
- Category
- C#
- Title
- string to enum c#
- Category
- C#
- Title
- replace all ponctuation characters c#
- Category
- C#
- Title
- vb.net tostring numeric format string
- Category
- C#
- Title
- unity docs player input
- Category
- C#
- Title
- unity making homing rocket
- Category
- C#
- Title
- move file from one folder to another c#
- Category
- C#
- Title
- how to move towards an object unity
- Category
- C#
- Title
- how to trigger event when a com device is connected in c#
- Category
- C#
- Title
- c# contextswitchdeadlock
- Category
- C#
- Title
- enums as numbers c#
- Category
- C#
- Title
- c# convert byte to char
- Category
- C#
- Title
- c sharp exit while loop
- Category
- C#
- Title
- c# input integer
- Category
- C#
- Title
- unity 2d joystick controls
- Category
- C#
- Title
- c# double question mark
- Category
- C#
- Title
- c sharp index of substring
- Category
- C#
- Title
- c# catch two exceptions in one block
- Category
- C#
- Title
- while loop in c#
- Category
- C#
- Title
- get random value from list c#
- Category
- C#
- Title
- asp list box
- Category
- C#
- Title
- get waht is differnt between two arrays c#
- Category
- C#
- Title
- iterate through dictionary c#
- Category
- C#
- Title
- c# Authorization has been denied for this request
- Category
- C#
- Title
- linq query select top 1 c#
- Category
- C#
- Title
- how to check if a value is inside an array c#
- Category
- C#
- Title
- how to cjeck if a string has a word c#
- Category
- C#
- Title
- exception handling c# stack overflow
- Category
- C#
- Title
- c# windows grab screenshot
- Category
- C#
- Title
- unity get component
- Category
- C#
- Title
- how to add system.messaging c#
- Category
- C#
- Title
- decimal to string whole number c#
- Category
- C#
- Title
- how to find object by ag unity
- Category
- C#
- Title
- configure 1 to 1 relation ef
- Category
- C#
- Title
- idbset sqlquery
- Category
- C#
- Title
- unity pro version free download
- Category
- C#
- Title
- validating file upload asp.net core mvc
- Category
- C#
- Title
- how to stop player rotating when hit by object
- Category
- C#
- Title
- c# making a folder
- Category
- C#
- Title
- c# regex to find number between parenthesis
- Category
- C#
- Title
- when do i need to end a sentence with ; in c#
- Category
- C#
- Title
- C# extend array
- Category
- C#
- Title
- c# list all files in a directory and subdirectory
- Category
- C#
- Title
- how to start an if statement in c#
- Category
- C#
- Title
- or in unity c#
- Category
- C#
- Title
- comments unity c#
- Category
- C#
- Title
- access object property C#
- Category
- C#
- Title
- delegate function c#
- Category
- C#
- Title
- c# arraylist to listview
- Category
- C#
- Title
- Unity if object doens't exist
- Category
- C#
- Title
- convert base64 string to string c#
- Category
- C#
- Title
- c# get every point in a line in matrix
- Category
- C#
- Title
- C# downloadstirng download old
- Category
- C#
- Title
- uwp file open picker
- Category
- C#
- Title
- how to join array indexes with comma in c#
- Category
- C#
- Title
- c# regex replace
- Category
- C#
- Title
- how to make if statement c#
- Category
- C#
- Title
- c# how to use inovke
- Category
- C#
- Title
- override Microsoft.AspNetCore.Authorization.AuthorizeAttribute
- Category
- C#
- Title
- select a whole row out of a 2d array C#
- Category
- C#
- Title
- how to store some variables on the device in unity
- Category
- C#
- Title
- defining vectors in c#
- Category
- C#
- Title
- c# relaxed boolean cast
- Category
- C#
- Title
- get enum value c#
- Category
- C#
- Title
- c# creating exceptions
- Category
- C#
- Title
- to list c#
- Category
- C#
- Title
- razor preview
- Category
- C#
- Title
- use newtonsoft json to clone object
- Category
- C#
- Title
- unity hide mouse first person
- Category
- C#
- Title
- how to open new form on button click in c# xamarin
- Category
- C#
- Title
- vb.net check if datatable has rows
- Category
- C#
- Title
- how to name GameObject in c#
- Category
- C#
- Title
- c sharp add item to dictionary
- Category
- C#
- Title
- iformfile blobclient
- Category
- C#
- Title
- c# reverse list
- Category
- C#
- Title
- c# remove time in datetime
- Category
- C#
- Title
- c# read excel file
- Category
- C#
- Title
- c# find duplicates in list of strings
- Category
- C#
- Title
- c# how to take from a float
- Category
- C#
- Title
- unity variable from another script
- Category
- C#
- Title
- how to detect if a key is pressed in c#
- Category
- C#
- Title
- c# print expression tree
- Category
- C#
- Title
- console application in c# ms crm
- Category
- C#
- Title
- how to reference function in unity
- Category
- C#
- Title
- c# convert Unix time in seconds to datetime
- Category
- C#
- Title
- unity get decimal part of float
- Category
- C#
- Title
- c# unity rotate first person controller script
- Category
- C#
- Title
- how to pass object as test case in nunit c#
- Category
- C#
- Title
- c# function return
- Category
- C#
- Title
- c# integer to bit string
- Category
- C#