C# Guide - ashBabu/Utilities GitHub Wiki
Set/Get position
In the Hierarchy window, create an object whose transform parameters are to be controlled via a C# script. Then in the Asset tab, right click -->create-->C# script. Attach it to the Gameobject in the hierarchy window by dragging & dropping.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cs_script_name : MonoBehaviour
{
public Vector3 startPosition; // this will add a field *startPosition* in Inspector Window
// Start is called before the first frame update
void Start()
{
transform.position = startPosition;
}
// Update is called once per frame
void Update()
{
}
}
Keyboard input
void Update()
{
//if space key pressed down
//print a message
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("space Key");
}
//if ey key held down
//print message
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Holding E");
}
//if f key is lifted up
//print f
if (Input.GetKeyUp(KeyCode.F))
{
Debug.Log("f");
}
}
Mapping Keyboard input to Movement
[SerializeField] // this will add a field *_speed* in Inspector Window under the script name
private float _speed;
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
transform.Translate(new Vector3(horizontalInput, 0, 0)) * _speed * Time.deltaTime);
}
Rotations (when pressing a key)
[SerializeField] // this will add a field *cubePrefab* in Inspector Window under the script name
private GameObject cubePrefab;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space));
{
Instantiate(cubePrefab, Vector3.zero, Quaternion.Euler(0, 30, 0)); // Game object called cubePrefab to be placed at (0, 0, 0) with an rotation of 30 around Y axis
}
}
Look Rotation
[SerializeField] // this will add a field *_speed* in Inspector Window under the script name
private Transform _sphere; // In the inspector window, drag the game object created in hierarchy window and drop it on the sphere (It will initially show None (Transform))
void Update()
{
// direction = destination - source
Vector3 directionToFace = _sphere.position - transform.position;
Debug.DrawRay(transform.position, diretionToFace, Color.green); // to draw a ray
// access our current rotation = Quaternion Look Rotation
transform.rotation = Quaternion.LookRotation(directionToFace);
}
Quaternion Slerp
This basically does spherical interpolation
[SerializeField] // this will add a field *sphere* in Inspector Window under the script
private Transform _sphere; // In the inspector window, drag the game object created in hierarchy window and drop it on the sphere (It will initially show None (Transform))
void Update()
{
// direction = destination - source
Vector3 directionToFace = _sphere.position - transform.position;
Debug.DrawRay(transform.position, diretionToFace, Color.green); // to draw a ray
// access our current rotation = Quaternion Look Rotation
Quaternion targetRotation = Quaternion.LookRotation(directionToFace);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2); // carries out spherical interpolation from target.roation to targetRotation with a speed of 2 m/s)
}