- Home /
Create new plane and Raycast in C#?
Hello I am trying to translate a script im using from Javascript to C# as i need it to talk to other c# scripts I've made and I will be using mainly C# for the rest of the game. I've attempted to translate them, the rest of the script is fine, its just two lines that I am stuck with. So I assume it is meant to be Ray ray. I'm just not sure.
playerPlane = new Plane(Vector3.forward, transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
clickPosition = ray.GetPoint(hitdist);
}
Here is what they were before
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Answer by Calum-McManus · Jan 14, 2014 at 04:39 PM
Give this a go!
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = new Ray(Camera.main.transform.position, Input.mousePosition);
I didn't get any errors so far so I hope this fixes what ever issue you where having!
Ok that fixed that thank you, but I obviously haven't translated it all as I have a stream of errors now, mainly to do with the transform i use later on.
If you just tell me what you want this to do I'll get it sorted, but I need an idea of what it does first :)
Ok thank you. So much to learn :S
Basically I have a click to move script written in javascript so that when i click the player moves to that location. The clamping is so it doesn't move outside the screen when you click past a certain point. There is also a flip bit so that the sprite looks like it is facing the opposite direction.
I have a targeting script written in c# which i need to talk to this script when i am aware is difficult to make happen in different languages.
I'll edit out the code from the last comment and paste what ive already done in js here.
Thanks again :)
private var clickPosition:Vector3; private var playerDistance:float;
//Clamping Variables var leftClamp:float = -6; var rightClamp:float = 6; var topClamp:float = 3; var bottomClamp:float = -4; var stop:int = 0;
function Update () {
playerDistance = Vector3.Distance(clickPosition, transform.position);
//Tells the Player object to stop when it reaches its location
if(playerDistance < 0.1){ // prevents shaking when it reaches location
moveSpeed.moveSpeed = 0;
}
else if(playerDistance > 0.1){
moveSpeed.moveSpeed = 2;
}
//When right mouse button is clicked its moves the player twards that location
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1))
{
var playerPlane = new Plane(Vector3.forward, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist:float = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
clickPosition = ray.GetPoint(hitdist);
}
}
if(playerDistance > 0.1){ // Prevents code running when it doesn't need to
transform.position += (clickPosition - transform.position).normalized * moveSpeed.moveSpeed * Time.deltaTime;
}
transform.position.z = 0;
//Clamping, stops the player from moving outside the camera view
if(transform.position.x <= leftClamp){
transform.position.x = leftClamp;
clickPosition.y = transform.position.y;
}
if(transform.position.x >= rightClamp){
transform.position.x = rightClamp;
clickPosition.y = transform.position.y;
}
if(transform.position.y >= topClamp){
transform.position.y = topClamp;
clickPosition.x = transform.position.x;
}
if(transform.position.y <= bottomClamp){
transform.position.y = bottomClamp;
clickPosition.x = transform.position.x;
}
//Flipping the Sprite to face the direction of movement
if(clickPosition.x < transform.position.x){
transform.localScale.x = -1;
}
if(clickPosition.x > transform.position.x){
transform.localScale.x = 1;
}
}
Ok so I cant test it with out the project xD But so far this seems to get no errors and from my understanding does what you want :)
using UnityEngine;
using System.Collections;
public class test : $$anonymous$$onoBehaviour
{
private Vector3 clickPosition;
private float playerDistance;
//Clamping Variables
private float leftClamp = -6;
private float rightClamp = 6;
private float topClamp = 3;
private float bottomClamp = -4;
private int stop = 0;
private float moveSpeed = 0;
void Update(){
playerDistance = Vector3.Distance(clickPosition, transform.position);
//Tells the Player object to stop when it reaches its location
if(playerDistance < 0.1f){ // prevents shaking when it reaches location
moveSpeed = 0;
}
else if(playerDistance > 0.1f){
moveSpeed = 2;
}
//When right mouse button is clicked its moves the player twards that location
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1))
{
Plane playerPlane= new Plane(Vector3.forward, transform.position);
Ray ray= Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist)) {
clickPosition = ray.GetPoint(hitdist);
}
}
if(playerDistance > 0.1f){ // Prevents code running when it doesn't need to
transform.position += (clickPosition - transform.position).normalized * moveSpeed * Time.deltaTime;
}
Vector3 Dir = transform.position;
Dir.z = 0;
//Clamping, stops the player from moving outside the camera view
if(Dir.x == rightClamp){
Dir.x = rightClamp;
clickPosition.y = transform.position.y;
}
if(transform.position.y >= topClamp){
Dir.y = topClamp;
clickPosition.x = transform.position.x;
}
if(Dir.y >= transform.position.x){
Dir.x = 1;
}
}
}
Awesome thank you very much :D Only problem is clamping doesnt work at the moment, so the player can walk outside the screen, once outside the clamp it just cant move along the y axis.
Your answer
Follow this Question
Related Questions
Translate this statement from .js to C# 2 Answers
How to make this line work in C#? 1 Answer
translate javascript for loop to C# 2 Answers
Can someone translate this to Javascript? 1 Answer
c# to JavaScript how? 3 Answers