0% found this document useful (0 votes)
18 views

Unity Snaps

Uploaded by

Janakiraman NPR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unity Snaps

Uploaded by

Janakiraman NPR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#unity #movetowards

```C#
transform.position = Vector2.MoveTowards(transform.position,
player.transform.position, speed * Time.deltaTime);
```

#unity #rotate
```C#
transform.Rotate(Vector3.forward * turnSpeed * Input.GetAxisRaw("Horizontal") *
Time.deltaTime);
```

#unity #getobject
```C#
player = FindObjectOfType<Player>();
```

#unity #collision
```C#
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Hole")
{
Destroy(gameObject);
}

if (other.tag == "Player" || other.tag == "Enemy")


{
speed = 0;
transform.parent = player.transform;
}
}
/*ATTACH IN THE OBJECT THAT IS NOT OTHER*/
```

#unity #movement
```C#
private void Update()
{
HorizontalMovement();
}

private void HorizontalMovement()


{
inputAxis = Input.GetAxis("Horizontal");
velocity.x = Mathf.MoveTowards(velocity.x, moveSpeed * inputAxis, moveSpeed
* Time.deltaTime);
}

private void FixedUpdate()


{
Vector2 position = rigidbody.position;
position += velocity * Time.fixedDeltaTime;

rigidbody.MovePosition(position);
}
```

#unity #cameraright
```C#
private void LateUpdate()
{
Vector3 cameraPosition = transform.position;
cameraPosition.x = Mathf.Max(cameraPosition.x, player.position.x);
transform.position = cameraPosition;
}
```

#unity #jumpforce
```C#
public float jumpForce => (2f * maxJumpHeight) / (maxJumpTime / 2f);
public float gravity => (-2f * maxJumpHeight) / Mathf.Pow((maxJumpTime /
2f),2);
```

#unity #raycast
```C#

private static LayerMask layermask = LayerMask.GetMask("Default");


public static bool Raycast(this Rigidbody2D rigidbody, Vector2 direction)
{
if (rigidbody.isKinematic)
{
return false;
}

float radius = 0.25f;


float distance = 0.375f;

RaycastHit2D hit =
Physics2D.CircleCast(rigidbody.position, radius, direction, distance,
layermask );

return hit.collider != null && hit.rigidbody != rigidbody;


```

#unity #colorchange

```C#
float timeLeft;
Color targetColor;

void Update()
{
if (timeLeft <= Time.deltaTime)
{
// transition complete
// assign the target color
renderer.material.color = targetColor;

// start a new transition


targetColor = new Color(Random.value, Random.value, Random.value);
timeLeft = 1.0f;
}
else
{
// transition in progress
// calculate interpolated color
renderer.material.color = Color.Lerp(renderer.material.color, targetColor,
Time.deltaTime / timeLeft);

// update the timer


timeLeft -= Time.deltaTime;
}
}
```

You might also like