Inserting UnityScript Raycasting code into C# script ?

hey , heres my camera script in C# …

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 = 50f;
	private Vector3 prevVelocity = Vector3.zero;
	private Vector3 currentVelocity = Vector3.zero;
	void FixedUpdate()
	{
		currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
		currentVelocity.y = 0;
		prevVelocity = currentVelocity;
	}
	
	void LateUpdate()
	{
		
		float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 170.0f);
		camera.fieldOfView = Mathf.Lerp(55, 72, speedFactor);
		float currentDistance = Mathf.Lerp(45.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;
				
		transform.position = newPosition;
		transform.LookAt(newTargetPosition);
	
		}
}

//// And im trying to insert this Raycasting script inside it but its in UnityJavascript and for the life of me I cannot seem to combine the two without errors :frowning:

function Update () {
 
   var up = transform.TransformDirection(Vector3.up);
   var hit : RaycastHit;    
   Debug.DrawRay(transform.position, -up * 10, Color.green);
 
   if(Physics.Raycast(transform.position, -up, hit, 10)){
      Debug.Log("Hit");    
     if(hit.collider.gameObject.name == "floor"){
      Destroy(GetComponent(Rigidbody));
      }
   }
}

I just want the camera to rise up to stop it going through the terrain ,as two seperatye scripts attached to main camera it works ( I get the raycast warning) but cannot raise the camera up ,(I presume one script is overpowering the other ?) Can anyone help with this please I am totally stuck here ,I seem to have tried everything but failed :frowning:

You most likely can’t raise your camera because your movement code relies on the velocity variable of its rigidbody, but you are destroying it if the camera is less than 10 units above the floor.

If you want to restrict movement you should do it with a variable, not by removing components.

Vector3 up = transform.TransformDirection(Vector3.up); 
RaycastHit hit;
Debug.DrawRay(transform.position, -up * 10, Color.green);

if(Physics.Raycast(transform.position, -up, out hit, 10.0f))
{ 
    Debug.Log("Hit");
    if(hit.collider.gameObject.name == "floor")
    { 
         canMove = false;
    } 
} 


//Add something like 'bool canMove' to your script and add this if statement before you assign your new position
if (canMove)
    transform.position = newPosition;

All this time one of my scripts was somehow messing with the others ^ ^ no wonder it took a while to pin down :slight_smile: Merry Christmas guys :slight_smile:

Lol it turns out all this time one of my camera scripts was freezing the rotation values in other scripts,even when it was not attached ^^ .So I got there in the end :stuck_out_tongue: