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 HassanBajwa · Apr 09, 2014 at 10:56 AM · pathswipeswordmousemove

Move Sword(GameObject) along a path with mouse with same speed

I have sword which move along a path made by mouse move. but sword move with the speed of mouse. I want it to move with constant speed or you may add a variable to maintain its speed. Help me in this regard. My code is below

 private var dragging : boolean = false;
 private var moving : boolean = false;
 private var countDrag : int = 0;
 private var countMove : int = 0;
 private var mousePosition : Vector3;
 private var mousePoint : Vector3;
 private var pointCurrent : Vector3;
 private var pointStore : Vector3;
 private var posStoreX : Array = [];
 private var posStoreZ : Array = [];
 private var arrayPathMarker : GameObject[] = new GameObject[40];
 var objectPathMarker : GameObject;
 var score : int = 0;
 var trn : GameObject;
 var rotSpeed: float = 1000;
 static var fireok : boolean = false;
 var firefmpl : firefromplayer;
 var obj1:GameObject;
 var projectile : GameObject;
 var explosioneffect : GameObject;
 var target : Transform;
 var jumpSound : AudioClip;
 
 function Start () {
     dragging = false;
     trn = GameObject.FindWithTag("Score");
 }
 
 function Update () {
     transform.Rotate(0, rotSpeed * Time.deltaTime + 8, 0, Space.World);
     if(fireok == true)
     {
     fire();
     }
 }
 
 function firefromplayer(fire : boolean){
     print("before if working" + fire);
     if(fire == true)
     {
         fire();
     }
 }
 
 function fire(){
         var rayHit : RaycastHit;
         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), rayHit)) {
         // where did the raycast hit in the world - position of rayhit
         if (dragging) {
         //print ("rayHit.point : " + rayHit.point + " (mousePoint)");
         }
         mousePoint = rayHit.point;
         var playerObject = GameObject.FindWithTag("Player");
         var playerPos:Vector3 = playerObject.transform.position;
         if (Input.GetMouseButtonDown(0)) {
             OnTouchBegin(playerPos);
         }
         else if (Input.GetMouseButton(0)) {
             OnTouchMove(mousePoint);
         }
         else if (Input.GetMouseButtonUp(0)) {
             OnTouchEnd(mousePoint);
             fireok = false;
             firefmpl.attack = true;
             
         }
         }
 }
 
 function OnTouchBegin (pointCurrent : Vector3) {
     countDrag = 0;
     posStoreX.Clear();
     posStoreZ.Clear();
     AddSplinePoint(pointCurrent);
     dragging = true;
     moving = false;    
 }
 
 function OnTouchMove (pointCurrent : Vector3) {
     if ((dragging) && (countDrag <= 40)) {
         //print("countDrag " + countDrag);
         AddSplinePoint(pointCurrent);
     } else {
         dragging = false;
         moving = true;
     }
 }
 
 var endingpoint : Vector3;
 function OnTouchEnd (pointCurrent : Vector3) {
     dragging = false;
     moving = true;
     endingpoint = pointCurrent;
     audio.PlayOneShot(jumpSound);
     //print(endingpoint);
 //    transform.Translate(endingpoint * 2 * Time.deltaTime);
 }
 
 function AddSplinePoint (pointStore : Vector3) {
     // store co-ordinates
     posStoreX[countDrag] = pointStore.x;
     posStoreZ[countDrag] = pointStore.z;
     
     // show path : Instantiate and load position into array as gameObject
     if(countDrag < 40)
     {
     arrayPathMarker[countDrag] = Instantiate(objectPathMarker, Vector3(pointStore.x, 0, pointStore.z), transform.rotation);
     }
 //    print (arrayPathMarker[countDrag].transform.position);
     // next position
     countDrag ++;
 }
 
 function OnTriggerEnter (other : Collider) {
         this.rigidbody.velocity *= 0.2f;
         if (other.gameObject.tag == "Enemy"){
       Destroy (other.gameObject);
       }
           if (other.gameObject.tag == "EnemyFire" || other.gameObject.tag == "EnemyOrc" || other.gameObject.tag == "EnemyWarrior"){
           target = other.gameObject.transform;
           var position:Vector3=Vector3(target.position.x,target.position.y,target.position.z);
           var clone : GameObject =  Instantiate(explosioneffect,position,Quaternion.identity);
         clone.gameObject.SetActive(true);
         other.gameObject.SetActive(false);
         yield WaitForSeconds(1f);
         clone.gameObject.SetActive(false);
         this.gameObject.transform.position.x = -1.46405f;
         this.gameObject.transform.position.z = -1.295876f;
       }
         if (other.gameObject.tag == "Coin"){
         var chk :int=1;
         trn.SendMessage("AddScore",chk,SendMessageOptions.DontRequireReceiver);
 //        score = score + 1;
 //          print(score);
         Destroy(other.gameObject);
     }
 }
 function FixedUpdate () {
     // move gameObject
     Move();
 }
 
 function Move(){
     if (moving) {
         // remove path marker
         if(countDrag < 40)
     {
         Destroy (arrayPathMarker[countMove]);
     }    
         // move gameObject along path
         
             if(countDrag < 40)
     {
           //transform.Translate(endingpoint);
           transform.position = Vector3(posStoreX[countMove],0.5, posStoreZ[countMove]);
           //transform.Translate(Vector3.forward * 5 * Time.deltaTime);
         //yield WaitForSeconds(1);
         
     }    
         // next position
         countMove ++;
         if (countMove >= posStoreX.length) {moving = false;}
           // stop at end of path
     } else {
         countMove = 0;
     }    
 }
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
0

Answer by ConradXD · Jun 16, 2015 at 05:43 AM

This is C# script right? And btw, how can i code it so that when i move the mouse regularly, it still makes the camera look from side to side and the sword only swings when I left click and move my mouse?

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

22 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 avatar image avatar image

Related Questions

HELP! How to make an object move until colliding with another object. 2 Answers

Instantiate a prefab every certain distance while swipping (iOS) 1 Answer

Mouse Position to Screen Coordinate Problems 2 Answers

Do On swipe finger? 1 Answer

Swipe detection script help? 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