- Home /
Help making script!!
hi, I'm not very good with scripting(seems to me like I still have a lot to learn). I need to make a script that will allow a rigidbody(ball) to go back to a certain spot(in front of my player) if it is pushed by the AI or moved too far from my player. Can anybody teach me how to do this in a script or make me a script doing what I want?
What's with the dramatic "!!"? I haven't been here long and I've seen so many posts like this it has become extremely easy to ignore them. I have one word for you
TUTORIALS
Find them! Learn!
Give a meaningful title to your question and use the tags properly.
Answer by Meater6 · Jul 03, 2011 at 04:50 PM
Well, this answer will get you only the concept, no script, you will have to make it yourself. You can do it like this:
Create a transform where you want your sphere to be
Lerp the position of the sphere to the transform's position
NOTE: I have never tried this method before. So it might not work. There are many more ways how to do it, I just didn't show them because they are more complicated, and you said:
I'm not very good with scripting
If it doesn't work, and you have also looked at other scripts like this one, I will make a simple example script for you.
EDIT: Here's an example script to make it work. Remember, for a noobie like you, the BEST place to learn is the Unity scripting reference, http://unity3d.com/support/documentation/ScriptReference/index.html. Your example script:
//this script is tested
//this script will make the object it is put on move to its "target"
//move to...
var target : Transform;
//really the speed
var smooth : float = 10.0;
//distance the ball can be away from the target without moving
var radius : float = 1.0;
function Update ()
{
//check distance
if(Vector3.Distance(transform.position,target.position) > radius)
{
var lerp : Vector3 = Vector3.zero;
//Lerp function, useful yes?
lerp = Vector3.Lerp(transform.position,target.position, smooth * Time.deltaTime);
//set position: THE most important part
transform.position = lerp;
}
}
I'm not sure if I actually did what you said. but thanks. you got me on the right track!
here's my script:
var Ball : Transform;
var separationDistance = 3.0;
var returnPoint : Transform;
function Update () {
distance = Vector3.Distance(Ball.position, transform.position);
if(distance < separationDistance)
Ball.transform.Translate(returnPoint.position);
}
Well, you can't just say
Ball.transform.Translate(returnPoint.position);
and expect it to work. Translate does not work like that. :P Here, since you really seem to be having trouble, I'll post a script the makes a object move to a transform.
Your answer
Follow this Question
Related Questions
iTween Help Please MoveTo not working 1 Answer
Good refernce material for logic behind ai 0 Answers
some problems with my ai 1 Answer
simple AI - Beginner question 2 Answers
Enemy AI monster problem 3 Answers