- Home /
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 :(
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 :(
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)); } } }
If someone could convert this to C# that would be a massive help :) I know what I need to do just keep getting syntax mess ups :( the scripts work together nicely :)
O$$anonymous$$G lol the obvious just clicked ^^ I guess I need to read a value from the raycaster script to tell the cameras $$anonymous$$ Y position to be in the carcamera script ,If thats correct I feel like such an Uber numpty if Im right ,I'll get back tou you all ,brain is fried but I rhink im on it lol :)
Try this for C# conversion. I haven't checked if it works more than it doesn't give compilation errors:
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")
{
Destroy(GetComponent<Rigidbody>());
}
}
hmm I get this ?
Assets/llll.cs(1,9): error CS0116: A namespace can only contain types and namespace declarations.
Btw will this work ?
I guess I need to read a value from the raycaster script to tell the cameras $$anonymous$$ Y position to be in the carcamera script ,grab a value from raycaster script ,if true then raise camera in carcamera script ? . Is it this obvious or am I missing something lol ?
Answer by javanoob · Dec 15, 2012 at 09:27 PM
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 :p
Answer by MibZ · Dec 13, 2012 at 04:48 PM
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;
Im completely lost lol ,everytime I try to add anything the the Carcamera script I get various errors :(
I tried adding this bit
// remove these lines first ? // using UnityEngine; //using System.Collections; //public class example : $$anonymous$$onoBehaviour { // remove these lines first ?
public ScriptName script; void Example() { script = GetComponent("ScriptName") as ScriptName; script.DoSomething(); } }
And I get errors ,I just wanted to read a value from the raycaster script because adding to the C# script for me seems impossible ,I have tried 20 different things but it will not accept any extra code :( .I am truly lost now and thinking giving up is the best option :( (maybe an in car view ins$$anonymous$$d) lazt way out but I am totally lost ,I have tried sphere colliders attached to the camera and nothing seems to wotk ?is this a Unity bug with colliders ?
All I want to do is add collison detection to the Car Camera script from the Unity Car Tutorial ,and everything im trying is failing ,the closest I got was the two seperate scripts approach .Anyone have a clue how to do this ?
Files don't just refuse code being added, and if you want to learn how to code giving up isn't a good option.
To start with, do you have a script named "ScriptName"?
using UnityEngine; using System.Collections;
public class CarCamera : $$anonymous$$onoBehaviour
{
bool can$$anonymous$$ove;
public Transform target = null;
public float height = 1f;
public float positionDamping = 3f;
public float velocityDamping = 3;
public float distance = 50f;
public Layer$$anonymous$$ask ignoreLayers = -1;
private RaycastHit hit = new RaycastHit();
private Vector3 prevVelocity = Vector3.zero;
private Layer$$anonymous$$ask raycastLayers = -1;
private Vector3 currentVelocity = Vector3.zero;
void Start(){
raycastLayers = ~ignoreLayers;
}
void FixedUpdate()
{
currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
currentVelocity.y = 0;
prevVelocity = currentVelocity;
}
void LateUpdate()
{
float speedFactor = $$anonymous$$athf.Clamp01(target.root.rigidbody.velocity.magnitude / 170.0f);
camera.fieldOfView = $$anonymous$$athf.Lerp(55, 72, speedFactor);
float currentDistance = $$anonymous$$athf.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;
if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
newPosition = hit.point;
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")
{
can$$anonymous$$ove = false;
}
}
//Add something like 'bool can$$anonymous$$ove' to your script and add this if statement before you assign your new position
if (can$$anonymous$$ove)
transform.position = newPosition;
// transform.position = new Vector3(transform.position.x, 7f, transform.position.z); //added c codr that worked
transform.position = newPosition;
transform.LookAt(newTargetPosition);
}
}
Thank you :)I managed to get the code into the carcamera script and it registers a hit when the raycast hits :) just got to work out how to raise the camera slowly now :) ,BUt thanks 10000000x trying to get the Java into the C# was driving me nuts lol :p Im hoping the next bit will be easy now its all in one script :)
No problem! Learning program$$anonymous$$g is difficult, especially when you're trying to convert one language to another. (Especially when it's from JavaScript to something else :p) $$anonymous$$eep at it dude!
Thanks $$anonymous$$ibz ,im getting it slowly and loving it :) Ever since I was 12 years Old I wanted to make AAA games ,and Unity plus people like you make it all possible :) I'm a 3D artist by hobby and I really want to make something nice :)
Answer by javanoob · Dec 15, 2012 at 09:32 PM
All this time one of my scripts was somehow messing with the others ^ ^ no wonder it took a while to pin down :) Merry Christmas guys :)
Its a random thing ,I give up :( months of random issues mean I am just never gonna be good at this .
Your answer
Follow this Question
Related Questions
RTS camera help 1 Answer
Raycasting on camera corners 3 Answers
How to get Camera Collision Detection on Terrain 1 Answer
Ray camera to Terrain goes Wrong. 1 Answer
Make the terrain ignore Raycast if in between Camera and Player? 1 Answer