- Home /
Using MoveToward but is no working.
every time i shoot, it wont shoot to the mouse position.
it only shoot forward from the shooting point.
my friends have use this movetoward and it work, what is the error of my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwipeTrail : MonoBehaviour {
public Rigidbody bullet;
public Transform firePoint;
public float speed;
// Touch.Began - when first press the finger in the screen
// Touch.Ended - when lifted up the finger from the screen
void Update ()
{
speed = speed * Time.deltaTime;
// if the touching is more than 0, the touch pahse is Moved (when sliding the finger across the screen
if ((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) || Input.GetMouseButton (0)) {
Plane objPlane = new Plane (Camera.main.transform.forward * -1, this.transform.position);
Ray mRay = Camera.main.ScreenPointToRay (Input.mousePosition);
float rayDistance;
if (objPlane.Raycast (mRay, out rayDistance)) {
this.transform.position = mRay.GetPoint (rayDistance);
Rigidbody rocketInstance;
rocketInstance = Instantiate (bullet, firePoint.position, firePoint.rotation) as Rigidbody;
rocketInstance.transform.position = Vector3.MoveTowards (firePoint.position, this.transform.position, speed);
}
}
}
// void OnTriggerEnter(Collider col)
// {
// Debug.Log ("haha");
// if (col.tag == "mons") {
// Destroy (col.gameObject);
// }
//
// }
}
Answer by FlaSh-G · Mar 12, 2018 at 01:09 PM
Vector3.MoveTowards is a very simple function with no magic attached.
What it does:
it takes two Vector3s a and b and a float as a distance. It returns a new Vector3 that is "distance" meters away from a, towards b.
If the distance between a and b is shorter than the given distance, it will return b.
What it does NOT:
Change any object's position, or change anything in your scene in general
Remember what it did last time it was called
Do something over some amount of time
So... you're using the function once every time you hit something with your touch-based ray. The moment that happens, the object will be placed somewhere between firePoint.position and transform.position. And then that's it, the object stays there.
If you want a thing to constantly move, you need to call a function like MoveTowards in every frame until the destination is reached. This works in Update, but Coroutines also work well.
@FlaSh-G can i know what is the end point of the code from my code for the $$anonymous$$oveToward because i cant find it. firePoint is at the cube there as a starting point, mousePosition as final destination.
I don't really understand your question, to be honest.
This is the implementation of Vector3.$$anonymous$$oveTowards, maybe it helps.
public static Vector3 $$anonymous$$oveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
{
Vector3 a = target - current;
float magnitude = a.magnitude;
Vector3 result;
if (magnitude <= maxDistanceDelta || magnitude < 1.401298E-45f)
{
result = target;
}
else
{
result = current + a / magnitude * maxDistanceDelta;
}
return result;
}
Answer by tormentoarmagedoom · Mar 12, 2018 at 12:42 PM
Good day.
Do you recieve any error when shoot?
Explore to be sure all parameters are correct before execute each function, like this:
Debug.Log (firePoint.position);
Debug.Log (this.transform.position);
Debug.Log (Speed);
rocketInstance.transform.position = Vector3.MoveTowards (firePoint.position, this.transform.position, speed);
So you will be able to see in the console if all paratmeters are good registered before executing the comand.
And as i see from the scripting API, the paramteres must be (Origin position, Destination position, MaxDistance float) and i think you have them swaped.
If correct, accept the ansewr!
Bye!
@tormentoarmagedoom firePoint is at the cube there as a starting point, mousePosition as final destination.
Your answer
Follow this Question
Related Questions
The shooter wont shoot towards mouse position in unity3d 0 Answers
Bullet not shooting enemy center in unity 1 Answer
Make an object move towards the mouse and then keep going 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers