Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Beta_Artist · Sep 27, 2011 at 05:42 PM · movementvector3airandom

Random direction with Mouse Click...

I'm trying to make it to where the every time I click the target the picks a predetermined direction and moves that way. Is there something missing? Is my "programming grammar" correct?

 var speed = 5;
 var deer = 10;
 var tap = 100;
 var num = Random.Range(2,-2);
 
 
 function Update () {
     transform.position += transform.forward * Time.deltaTime * speed;
 }    
 
 function OnMouseEnter() {
     speed = 15;
     TotalScoreScript.Points += deer + 10;
 }
 
 function OnMouseExit () {
     speed = 5;
 }
 
 function OnMouseDown () {
     TotalScoreScript.Points += tap + 100;
 
     if(num=(-2)){
         transform.Rotate(Vector3.up, -135 );
     }
     if(num=(-1)){
         transform.Rotate(Vector3.up, -45 );
     }
     if(num=1){
         transform.Rotate(Vector3.up, 45 );
     }
     if(num=2){
         transform.Rotate(Vector3.up, 135 );
     }
     else(num=0){
         transform.Rotate(Vector3.up, 0 );
     }    
 }
     
Comment
Add comment · Show 1
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 BerggreenDK · Sep 27, 2011 at 05:44 PM 0
Share

how is it not behaving as you want? can you explain the problem too?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BerggreenDK · Sep 27, 2011 at 05:52 PM

If I understand your algoritm / idea, you want to make this script run an "animal NPC/AI" ?

and when I mouseover, the animal will panic simulated with speed 15? and mouse down = shot/fire? resulting in the animal changing direction?

For the "OnMouseDown". Lets put that in a function called "ChangeDirection" instead. Then you can call it from more than one event if needed.

 function ChangeDirection()
 {
 
    num = Random.Range(2,-2); // as I understand this one, its your direction? you could call it dir or direction to make it more logical?
 
    // now instead of multiple IF... THEN... use a SWITCH... CASE to make it more easy
    // if no CASE is a match for the variable, then DEFAULT is choosen. You can ommit a default too.
 
    var rotate;
    switch(num)
    {
      case -2:
         rotate = -135;
      break;
 
      case -1:
         rotate = -45;
      break;
 
      case 1:
         rotate = 45;
      break;
 
      case 2:
         rotate = 135;
      break;
 
      default:
         rotate = 0;
      break;
    }   
 
    // if direction should be changed
    if(rotate!=0) 
    {
      transform.Rotate(Vector3.up, rotate );
    }
 
    // add score
    TotalScoreScript.Points += tap + 100;
 }

suddently you can very easily also change the speed of the animal.

Comment
Add comment · Show 3 · 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 Beta_Artist · Sep 27, 2011 at 06:46 PM 0
Share

I changed the code with no errors but the object won't change direction.

var speed = 5; var deer = 10; var tap = 100; var rotate;

function Update () { transform.position += transform.forward Time.deltaTime speed; }

function On$$anonymous$$ouseEnter() { speed = 15; TotalScoreScript.Points += deer + 10; }

function On$$anonymous$$ouseExit () { speed = 5; }

function On$$anonymous$$ouseDown () { TotalScoreScript.Points += tap + 100; }

function ChangeDirection() { dir = Random.Range(2,-2);

 switch(dir){
     case -2:
         rotate = -135;
     break;
     
     case -1:
         rotate = -45;
     break;
     
     case 1:
         rotate = 45;
     break;
     
     case 2:
         rotate = 135;
     break;
     
     default:
         rotate = 0;
     break;
     }
     
 if(rotate!=0){
     transform.Rotate(Vector3.up, rotate);
 }

}

avatar image BerggreenDK · Sep 28, 2011 at 09:23 AM 0
Share

remember to call the function from the event... ChangeDirection should be called when you do the On$$anonymous$$ouseOver/down or whatever trigger you want.

avatar image BerggreenDK · Sep 28, 2011 at 09:24 AM 0
Share

also try to insert Debug.Log("some debug text or variable"); into your code, to see if the events are triggered correct.

avatar image
0

Answer by jonas.du · Sep 28, 2011 at 09:53 AM

By default, Random.Range returns a float. This means the values you get from Random.Range(-2, 2) is a floating point number between -2 and 2, for example 1.5437. As a result, your will hardly ever be == 1.
Two ways to solve the problem, use

 var num:int = Random.Range(-2, 2);

This way you get an integer instead of a float.
The other way would be a range check.

P.s to test for equality use '==' not '='

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

AI repeats movement and isnt randomised 0 Answers

Random movment, like mobs in wow 1 Answer

2D Random AI 1 Answer

Switch Case for Basic AI.... 1 Answer


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