- Home /
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
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?
Your answer
![](https://koobas.hobune.stream/wayback/20220613142322im_/https://answers.unity.com/themes/thub/images/avi.jpg)