draw sphere cast unity with program
C#
void RenderVolume(Vector3 p1 , Vector3 p2 , float radius , Vector3 dir, float distance )
{
if (!shape)
{ // if shape doesn't exist yet, create it
shape = GameObject.CreatePrimitive(PrimitiveType.Cube).transform;
Destroy(shape.GetComponent<Collider>()); // no collider, please!
shape.GetComponent<Renderer>().material = mat; // assign the selected material to it
}
Vector3 scale ; // calculate desired scale
float diam = 2 * radius; // calculate capsule diameter
scale.x = diam; // width = capsule diameter
scale.y = Vector3.Distance(p2, p1) + diam; // capsule height
scale.z = distance + diam; // volume length
shape.localScale = scale; // set the rectangular volume size
// set volume position and rotation
shape.position = (p1 + p2 + dir.normalized * distance) / 2;
shape.rotation = Quaternion.LookRotation(dir, p2 - p1);
shape.GetComponent<Renderer>().enabled = true; // show it
}
void HideVolume()
{ // hide the volume
if (shape) shape.GetComponent<Renderer>().enabled = false;
}
private Transform shape ;
public float range = 10; // range of the capsule cast
private float freeDistance = 0;
void Update()
{
if (Input.Get$$anonymous$$ey("p"))
{ // while P pressed...
RaycastHit hit;
CharacterController charContr = GetComponent<CharacterController>();
var radius = charContr.radius;
// find centers of the top/bottom hemispheres
Vector3 p1 = transform.position + charContr.center;
var p2 = p1;
var h = charContr.height / 2 - radius;
p2.y += h;
p1.y -= h;
// draw CapsuleCast volume:
RenderVolume(p1, p2, radius, transform.forward, range);
// cast character controller shape range meters forward:
if (Physics.CapsuleCast(p1, p2, radius, transform.forward,out hit, range))
{
// if some obstacle inside range, save its distance
freeDistance = hit.distance;
}
else {
// otherwise shows that the way is clear up to range distance
freeDistance = range;
}
}
if (Input.Get$$anonymous$$eyUp("p"))
{
HideVolume(); // hide volume when P is released
}
}
Also in C#:
- Title
- c# array Reverse method
- Category
- C#
- Title
- how to look for substring in string in c#
- Category
- C#
- Title
- unity get list length
- Category
- C#
- Title
- C# downloadstirng download old
- Category
- C#
- Title
- how to reference the position of a game object unity
- Category
- C#
- Title
- Unity if object doens't exist
- Category
- C#
- Title
- instantiate object in circle
- Category
- C#
- Title
- unity serializefield
- Category
- C#
- Title
- unity accessing 2d pointlight from c# script
- Category
- C#
- Title
- unity make a int arry with preset values
- Category
- C#
- Title
- c# yield
- Category
- C#
- Title
- call a function c#
- Category
- C#
- Title
- unity c# public all codes
- Category
- C#
- Title
- how to get array of children transform
- Category
- C#
- Title
- c# long to int
- Category
- C#
- Title
- c sharp list indexer
- Category
- C#
- Title
- native-googlesignin configuration is null!?
- Category
- C#
- Title
- find gameobject with tag
- Category
- C#
- Title
- order by C#
- Category
- C#
- Title
- error CS0542
- Category
- C#
- Title
- reference to gameobject in different scene unity
- Category
- C#
- Title
- center an image horizontally and vertically
- Category
- C#
- Title
- c# checksum
- Category
- C#
- Title
- c# getter setter
- Category
- C#
- Title
- Unity C# instantiate prefab
- Category
- C#
- Title
- c# convert dictionary to anonymous object
- Category
- C#
- Title
- divide string in chunks c#
- Category
- C#
- Title
- open zip file in c#
- Category
- C#
- Title
- unity monobehaviour
- Category
- C#
- Title
- array syntax c#
- Category
- C#
- Title
- how to make a datatable in c#
- Category
- C#
- Title
- unity main texture not working
- Category
- C#
- Title
- c# directories loop
- Category
- C#
- Title
- sort datatable c#
- Category
- C#
- Title
- C# Cast double to float
- Category
- C#
- Title
- c# print
- Category
- C#
- Title
- set int to null c#
- Category
- C#
- Title
- copy a list in c# unity
- Category
- C#
- Title
- change partial view based on select asp.net core
- Category
- C#
- Title
- set object to random color unity
- Category
- C#
- Title
- if entity.is Transient() Update Mvc 5 c#
- Category
- C#
- Title
- c# clear list items
- Category
- C#
- Title
- c# fileupload example
- Category
- C#
- Title
- get day month year from date c#
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- how to declare variables in c#
- Category
- C#
- Title
- c# random number between 0 and 1
- Category
- C#
- Title
- How to get number of months between 2 dates c#
- Category
- C#
- Title
- web api startup add imemory cache object
- Category
- C#
- Title
- choose random gameobject from a gameobject list
- Category
- C#
- Title
- c# region tag
- Category
- C#
- Title
- import regex c#
- Category
- C#
- Title
- unity instantiate
- Category
- C#
- Title
- how to create a list in c# unity
- Category
- C#
- Title
- c# arrays of arrays
- Category
- C#
- Title
- how do loops on C#
- Category
- C#
- Title
- prefab gets character transform
- Category
- C#
- Title
- How to look at an object unity
- Category
- C#
- Title
- degree to radians c#
- Category
- C#
- Title
- f string C#
- Category
- C#
- Title
- unity normalize vector2
- Category
- C#
- Title
- roulette algorithm genetic algorithm
- Category
- C#
- Title
- declare string array c# without size
- Category
- C#
- Title
- how to get the last element in an array in c#
- Category
- C#
- Title
- how to find the area of a circle
- Category
- C#
- Title
- No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.'
- Category
- C#
- Title
- how to reference function in unity
- Category
- C#
- Title
- c# random string
- Category
- C#
- Title
- unity calculate path
- Category
- C#
- Title
- bytes size c#
- Category
- C#
- Title
- unity how to rotate something to point to something else
- Category
- C#
- Title
- unity get data from firebase
- Category
- C#
- Title
- how to set progress openedge driver name for odbc connection c#
- Category
- C#
- Title
- c# null conditional
- Category
- C#
- Title
- c# generic abstract method
- Category
- C#
- Title
- set decimal point c#
- Category
- C#
- Title
- never lose focus textbox c#
- Category
- C#
- Title
- assign datasource to dropdownlist in c#
- Category
- C#
- Title
- change sprite of gameobject unity
- Category
- C#
- Title
- query associative table ef6
- Category
- C#
- Title
- how to make error sound c#
- Category
- C#
- Title
- array to list C
- Category
- C#
- Title
- unity set material
- Category
- C#
- Title
- Unity C# make object face away
- Category
- C#
- Title
- winforms c# add data to datagridview with a button
- Category
- C#
- Title
- add mime type for woff in web.config
- Category
- C#
- Title
- c# auto property set default value
- Category
- C#
- Title
- Move player on planets in unity 2d
- Category
- C#
- Title
- vb.net drag window without titlebar
- Category
- C#
- Title
- c# remove last character from string
- Category
- C#
- Title
- what type of variable is true or false in c#
- Category
- C#
- Title
- unity mouse click position
- Category
- C#
- Title
- how to mock abstract httpcontext using moq .net core
- Category
- C#
- Title
- c# thread sleep
- Category
- C#
- Title
- jumping with character controller unity
- Category
- C#
- Title
- object escape player unity
- Category
- C#
- Title
- c# skip following code in loop
- Category
- C#
- Title
- onmouseclick unity
- Category
- C#
- Title
- c# float to string
- Category
- C#
- Title
- get web config key value in c# razor view
- Category
- C#