Monday, September 24, 2018

Damping Code in Cars in Unity3D



using UnityEngine;
using System.Collections;

public class CarCamera : MonoBehaviour
{
public Transform target = null;
public float height = 1f;
public float positionDamping = 3f;
public float velocityDamping = 3f;
public float distance = 4f;
public LayerMask ignoreLayers = -1;

private RaycastHit hit = new RaycastHit();

private Vector3 prevVelocity = Vector3.zero;
private LayerMask raycastLayers = -1;

private Vector3 currentVelocity = Vector3.zero;

void Start()
{
raycastLayers = ~ignoreLayers;
}

void FixedUpdate()
{
currentVelocity = Vector3.Lerp(prevVelocity, target.root.GetComponent<Rigidbody>().velocity, velocityDamping * Time.deltaTime);
currentVelocity.y = 0;
prevVelocity = currentVelocity;
}

void LateUpdate()
{
float speedFactor = Mathf.Clamp01(target.root.GetComponent<Rigidbody>().velocity.magnitude / 70.0f);
GetComponent<Camera>().fieldOfView = Mathf.Lerp(55, 72, speedFactor);
float currentDistance = Mathf.Lerp(7.5f, 6.5f, speedFactor);

currentVelocity = currentVelocity.normalized;

Vector3 newTargetPosition = target.position + Vector3.up * height;
Vector3 newPosition = newTargetPosition - (currentVelocity * currentDistance);
newPosition.y = newTargetPosition.y;

Vector3 targetDirection = newPosition - newTargetPosition;
if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
newPosition = hit.point;

transform.position = newPosition;
transform.LookAt(newTargetPosition);

}
}

Arrays Code in Unity3D C#

using UnityEngine;
using System.Collections;

public class ArraysAFirstLook : MonoBehaviour
{
public int[] scores = new int[10];

public string[] strings = new string[10];

public float[] floats = new float[10];

public class MyClass
{

}
public MyClass[] MyClasses = new MyClass[10];

public GameObject[] MyGameObjects;

public int ArrayLength;

public int[] Primes = new int[]{ 1, 3, 5, 7, 11, 13, 17 };

void Start()
{
float[] DynamicFloats = new float[ArrayLength];
Debug.Log(MyGameObjects.Length);
for (int i = 0; i < MyGameObjects.Length; i++)
{
MyGameObjects [i].name = i.ToString();
}

foreach (GameObject go in MyGameObjects)
{
Debug.Log(go.name);
}

int[] scores = new int[10];
int s = 0;

while (s < 10)
{
scores [s] = Random.Range(0, 100);
int score = scores [s];
print(score);
s++;
}
}
}

Flat Hexgrid in Unity3D

using UnityEngine;

namespace MyGrid.Grids.Examples
{
public class FlatBrickTest : GLMonoBehaviour
{
private readonly Vector2 CellDimensions = new Vector2(360, 180);

public SpriteCell cellPrefab;
public GameObject root;

private FlatHexGrid<SpriteCell> grid;
private IMap3D<FlatHexPoint> map;

public void Start()
{
BuildGrid();
}

public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePosition = Input.mousePosition;
Vector2 worldPosition = GridBuilderUtils.ScreenToWorld(root, mousePosition);
FlatHexPoint hexPoint = map[worldPosition];

if (grid.Contains(hexPoint))
{
grid[hexPoint].HighlightOn = !grid[hexPoint].HighlightOn;
}
}
}

private void BuildGrid()
{
const int width = 7;
const int height = 9;

grid = FlatHexGrid<SpriteCell>.FatRectangle(width, height);

map = new FlatBrickMap(CellDimensions)
.AnchorCellMiddleCenter()
.WithWindow(ExampleUtils.ScreenRect)
.AlignMiddleCenter(grid)
.To3DXY()
;

foreach (FlatHexPoint point in grid)
{
SpriteCell cell = Instantiate(cellPrefab);
Vector2 worldPoint = map[point];

cell.transform.parent = root.transform;
cell.transform.localScale = Vector3.one;
cell.transform.localPosition = worldPoint;

cell.Color = ExampleUtils.Colors[point.GetColor3_7()];
cell.name = point.ToString();

grid[point] = cell;
}
}
}
}

Camera Preview in Unity3D


Up Down Left Right Motion in Unity3D



 using UnityEngine;
 using System.Collections;

 public class PlayerControllers: MonoBehaviour {
   
     public Animator anim;
     public float speed;

     void Start () {
         anim = GetComponent<Animator> ();
     }

     void Update () {

         if (Input.GetKey (KeyCode.D))
         {
             transform.Translate(Vector2.right * speed);
         }
       
         if (Input.GetKey (KeyCode.A))
         {
             transform.Translate(-Vector2.right * speed);
         }
         if (Input.GetKey (KeyCode.W))
         {
             transform.Translate(Vector2.up * speed);
         }
         if (Input.GetKey (KeyCode.S))
         {
             transform.Translate(-Vector2.up * speed);
         }

         if (Input.GetKey (KeyCode.A)) {
             anim.SetBool ("Left", true);
             anim.SetBool ("Right", false);
             anim.SetBool ("Up", false);
             anim.SetBool ("Down", false);
         }
         if (Input.GetKey (KeyCode.S)) {
             anim.SetBool ("Down", true);
             anim.SetBool ("Up", false);
             anim.SetBool ("Left", false);
             anim.SetBool ("Right", false);
           
         }
         if (Input.GetKey (KeyCode.D)) {
             anim.SetBool ("Right", true);
             anim.SetBool ("Left", false);
             anim.SetBool ("Up", false);
             anim.SetBool ("Down", false);
         }
         if (Input.GetKey (KeyCode.W)) {
             anim.SetBool ("Up", true);
             anim.SetBool ("Down", false);
             anim.SetBool ("Left", false);
             anim.SetBool ("Right", false);
         }
       
         if(Input.GetKey (KeyCode.A)){
             anim.SetBool("Player_Left", true);
         }
         else{
             anim.SetBool ("Player_Left", false);
         }
       
         if(Input.GetKey (KeyCode.S)){
             anim.SetBool("Player_Down", true);
         }
         else{
             anim.SetBool ("Player_Down", false);
         }
       
       
         if(Input.GetKey (KeyCode.D)){
             anim.SetBool("Player_Right", true);
         }
         else{
             anim.SetBool ("Player_Right", false);
         }
       
       
         if(Input.GetKey (KeyCode.W)){
             anim.SetBool("Player_Up", true);
         }
         else{
             anim.SetBool ("Player_Up", false);
         }
     }
 }

Inspector in Unity3D

The Inspector window (sometimes referred to as “the Inspector”) displays detailed information about the currently selected GameObject, including all attached components and their properties, and allows you to modify the functionality of GameObjects in your Scene.


GameObjects in Unity3D

1). A GameObject always has a Transform component attached (to represent position and orientation) and it is not possible to remove this.

2). The other components that give the object its functionality can be added from the editor’s Component menu or from a script.

3). GameObjects are the fundamental objects in Unity that represent characters, props and scenery.

A simple Cube GameObject with several Components.