Dive Board



DiveFPSController.js ...

DiveFPSController.js ...

by Ianmcmill on Jan 26th, 2014 17:19 PM

Hello,

as I am fairly new to coding and just scratching the surface of C# I have some problems with this Javascript from the demo project.
I know I may not have the right to criticise any code but in this case I can't help my self. Maybe I am just to much of a nub but is here anybody else that struggles with this FPS javaScript ? There is no intending, no formatting, rare "comments".

I tried to convert it to C# but I clearly lack the competence.
At least I got the intends and formating halfway decent....and got rid of this "die" function.
Anyways this throughs some errors. As the auther seems not to care about the readability, is here anybody that could help me out ?

<br />using UnityEngine;<br />using System&#46;Collections;<br /><br /><br />&#91;RequireComponent(typeof(CharacterController))&#93;<br />public class DiveFPSController &#58; MonoBehaviour <br />{<br />	Vector3 inputMoveDirection;<br />	bool inputJump = false;<br />	bool grounded = false;<br />	float downmovement = 0&#46;1f;<br />	Vector3 velocity;<br /><br />	public float max_speed = 0&#46;1f;<br />	public float max_speed_air = 0&#46;13f;<br />	public float max_speed_ground = 0&#46;1f;<br />	public float acceleration = 10;<br />	public float acceleration_air = 10;<br />	public float gravity = -0&#46;18f;<br />	public float friction = 0&#46;8f;<br />	public GameObject cameraObject;<br />	private CharacterController controller;<br />	public Vector3 groundNormal;<br />	public float jumpspeed = 0&#46;16f;<br />	public bool stopmovingup = false;<br />	public float fallkillspeed = -0&#46;38f;<br />	public CollisionFlags collisionFlags; <br /><br />	public GameObject ground_gameobject=null;<br />	public Vector3 last_ground_pos;<br />	private float jumpcommand = 0;<br />	public bool floating = false;<br /><br />	public int autowalk=0;<br />	public float inhibit_autowalk = 1;<br />	public int reload_once=0;<br /><br />	void Awake ()<br />	{<br />		controller = GetComponent&lt;CharacterController&gt;();<br />		Debug&#46;Log(&quot;Controller Slopelimit&quot;+controller&#46;slopeLimit);<br />	}<br /><br />	void JumpUp ()<br />	{<br />		jumpcommand=1;<br />	}<br /><br />	void Start ()<br />	{<br />		reload_once=0;<br />	}<br /><br />	void OnControllerColliderHit (ControllerColliderHit hit)<br />	{<br />		if (hit&#46;normal&#46;y &gt; 0 &amp;&amp; hit&#46;moveDirection&#46;y &lt; 0) <br />		{<br />			groundNormal = hit&#46;normal;<br />			grounded=true;	<br />			ground_gameobject=hit&#46;gameObject;<br />			<br />			//print(&quot;Landed on&#58; ground&quot;+ground_gameobject&#46;name);<br />			stopmovingup=false;<br />		}<br />	}<br /><br />        float fadeduration = 2&#46;0f; // fade duration in seconds<br /><br />	void Update ()<br />	{<br />		Debug&#46;DrawLine (transform&#46;position, transform&#46;position+groundNormal, Color&#46;red);<br />		//print(&quot;GroundNormal y&quot; +groundNormal&#46;y);<br /><br />		Vector3 directionVector = new Vector3(Input&#46;GetAxis(&quot;Horizontal&quot;), 0, Input&#46;GetAxis(&quot;Vertical&quot;));<br /><br />		if (directionVector != Vector3&#46;zero) <br />		{<br />			// Get the length of the directon vector and then normalize it<br />			// Dividing by the length is cheaper than normalizing when we already have the length anyway<br />			float directionLength = directionVector&#46;magnitude;<br />			directionVector = directionVector / directionLength;<br />			<br />			// Make sure the length is no bigger than 1<br />			directionLength = Mathf&#46;Min(1, directionLength);<br />			<br />			// Make the input vector more sensitive towards the extremes and less sensitive in the middle<br />			// This makes it easier to control slow speeds when using analog sticks<br />			directionLength = directionLength * directionLength;<br />			<br />			// Multiply the normalized direction vector by the modified length<br />			directionVector = directionVector * directionLength;<br />		}<br /><br />		// Apply the direction to the CharacterMotor<br />		inputMoveDirection =  directionVector;<br />		inputJump = Input&#46;GetButton(&quot;Jump&quot;);<br />  <br />		if(jumpcommand==1)<br />		{<br />			inputJump=true;	<br />			jumpcommand=0;<br />		}<br />	<br />	<br />		if (floating)<br />		{<br />			velocity&#46;y=0&#46;1f;<br />		}<br /><br />		if (inputJump&amp;&amp;grounded)<br />		{<br />			velocity&#46;y+=jumpspeed;<br />			velocity&#46;z*=1&#46;5f;<br /><br />			//print (&quot;Jump&quot;);<br />			grounded=false;<br />		}<br />	<br />	<br />		if (grounded)<br />		{<br />			velocity+=inputMoveDirection*acceleration*Time&#46;deltaTime;<br />		}<br />		else<br />		{<br />			velocity+=inputMoveDirection*acceleration_air*Time&#46;deltaTime;<br />		}<br />	<br />		Vector3 translation = new Vector3(velocity&#46;x,0,velocity&#46;z);<br />	<br />		translation = Vector3&#46;Lerp(Vector3(0,0,0),translation,friction);<br />		<br />		velocity&#46;x=translation&#46;x;<br />		velocity&#46;z=translation&#46;z;<br />		velocity&#46;y = velocity&#46;y+gravity*Time&#46;deltaTime;<br />		//print (&quot;Time deltatime &quot;+Time&#46;deltaTime);<br />		<br />		<br />		if (grounded)<br />		{<br />			velocity&#46;y=0;<br />		//print (&quot;Vel y &quot;+velocity&#46;y);<br />		}<br />	<br />		if (grounded)<br />		{<br />			max_speed=max_speed_ground;<br />		}<br /><br />		if (!grounded)<br />		{<br />			max_speed=max_speed_air;<br />		}<br /><br />		if (translation&#46;magnitude&gt;max_speed)<br />		{<br />			translation=translation/translation&#46;magnitude;<br />			translation=translation*max_speed;<br />		}<br /><br />		velocity&#46;x=translation&#46;x;<br />		velocity&#46;z=translation&#46;z;<br />		translation&#46;y=velocity&#46;y;<br />	<br />		Quaternion yrotation_camera= Quaternion&#46;Euler(0, cameraObject&#46;transform&#46;rotation&#46;eulerAngles&#46;y, 0);<br />		//transform&#46;position+=yrotation_camera*translation;<br /><br />		Vector3 platformdelta;<br />		<br />		if(ground_gameobject!=null) <br />		{<br />			print(&quot;ground object ungleich null&quot;);<br />			platformdelta=ground_gameobject&#46;transform&#46;position-last_ground_pos;<br />		}<br />	<br />		//if (!grounded)platformdelta=Vector3&#46;zero;<br />	<br />	<br />		//MAKE A MOVE!<br />		<br />		collisionFlags=controller&#46;Move(yrotation_camera*translation+platformdelta);<br />		<br />		<br />		if (collisionFlags &amp; CollisionFlags&#46;CollidedAbove)<br />		{<br />			if (stopmovingup==false)<br />			{<br />				print (&quot;ControllerColliderHit&quot;);	<br />				velocity&#46;y=0;	<br />				stopmovingup=true;<br />			}<br />		}<br />	<br />		if (ground_gameobject!=null &amp;&amp; !grounded)<br />		{<br />			ground_gameobject=null;<br />		}<br /><br />		if(ground_gameobject!=null)<br />		{<br />			last_ground_pos=ground_gameobject&#46;transform&#46;position;	<br />		}<br />	}<br /><br /><br />	void LateUpdate ()<br />	{<br />		//groundNormal=Vector3&#46;zero;<br />	}<br />	void OnGUI ()<br />	{<br />	//	GUI&#46;Label ( new Rect(10, 10, 400, 20), &quot;isgrounded &quot;+grounded+   &quot; velocity&#46;y &quot;+velocity&#46;y+&quot; collsionbelow &quot;+ (collisionFlags &amp; CollisionFlags&#46;Below) );<br />		Event e = Event&#46;current;<br /><br />		if (e&#46;isKey) <br />		{<br />			Debug&#46;Log(&quot;Detected key code&#58; &quot; + e&#46;keyCode);<br />			GUI&#46;Label ( new Rect(10, 10, 400, 20), &quot;Keycode =&quot;+ e&#46;keyCode );<br />		}<br />							<br />	}<br /><br /><br />	void OnTriggerEnter (  Collider other   )<br />	{<br /><br />		if (other&#46;name == &quot;Float&quot;) <br />		{<br />			floating=true; 	 <br />			inhibit_autowalk=0&#46;1f;<br />		}<br />	}<br /><br />	void OnTriggerExit (  Collider other   )<br />	{<br />		if (other&#46;name == &quot;Float&quot;) <br />		{<br />			floating=false; 	 <br />			inhibit_autowalk=1;<br />		}<br />	}<br />}<br />


Assets/_Scripts/DiveFPSController.cs(131,44): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected


Assets/_Scripts/DiveFPSController.cs(131,39): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments


Assets/_Scripts/DiveFPSController.cs(131,39): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector3'


Assets/_Scripts/DiveFPSController.cs(181,77): error CS0165: Use of unassigned local variable `platformdelta'


Assets/_Scripts/DiveFPSController.cs(184,17): error CS0029: Cannot implicitly convert type `UnityEngine.CollisionFlags' to `bool'

Ianmcmill

Posts: 10

Joined: 19.12.2013


Re: DiveFPSController.js ...

by henny1234 on Jun 15th, 2022 06:54 AM

I have read some of your posts and they have been quite beneficial for me in my life. If you want to play music related games, you can try heardle, it will make you more comfortable

henny1234

Posts: 4

Joined: 15.06.2022


STATISTICS


Total posts: 171420


Total topics: 41564


Total members: 44823


Newest member: Daniel G.