Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Danteeva · Jan 14, 2014 at 03:46 PM · c#javascriptraycastplanetranslate

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);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Danteeva · Jan 14, 2014 at 05:26 PM 0
Share

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.

avatar image Calum-McManus · Jan 14, 2014 at 07:14 PM 0
Share

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 :)

avatar image Danteeva · Jan 14, 2014 at 07:24 PM 0
Share

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;
 }
 

}

avatar image Calum-McManus · Jan 15, 2014 at 12:07 PM 0
Share

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;
     }
     
 }
 }
avatar image Danteeva · Jan 15, 2014 at 02:20 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges