public class BallScript : MonoBehaviour
{
Vector3 targetPos = new Vector3(0f, 0f, 0.7f);
public void FallInHole()
{
transform.position += new Vector3(0f, 0f, 0.7f);
transform.position = Vector3.Lerp(transform.position, targetPos, (Time.deltaTime * 3) / Vector3.Distance(targetPos.transform.position));
my attempt is to get the object to move in two directions at different times upon entering a trigger. This method is called from another script. error cs 1061 on line 8, 124 vector 3 does not contain a definition for transform.
finally fixed it so no errors occur. here is my new script
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
public class BallScript : MonoBehaviour
{
Vector3 targetPos = new Vector3(13f, 0f, 0.7f);
public void FallInHole()
{
transform.position += new Vector3(0f, 0f, 0.7f);
transform.position = Vector3.Lerp(transform.position, targetPos, (Time.deltaTime));
My intent is to get the ball to go to the bottom of the pocket then roll to the center of the table(which is at 13,0,0.7) when I had it as 0,0,0.7 I noticed some skipped movement but when I change it to this ball does not go to the new vector coordinates. no problem though getting it to sink to the bottom of the pocket.
I wonder though, am I telling the ball to move a targeted position of 13,0,0.7 or am I telling it to move 13 units on the x axis?
Also, what is up with the stuff at the top? I didn’t write that in.
Vector3.Lerp (and all other lerp functions) isn’t supposed to be used with Time.deltaTime, as it doesn’t move a Vector3 over time.
The t parameter in all lerp functions is supposed to be a number between 0 and 1, where:
0 = exactly the first value (the first Vector3 parameter).
1 = exactly the second value (the second Vector3 parameter).
0.5 = exactly halfway between the two values.
You probably want to use Vector3.MoveTowards instead.
You can also remove this line…
transform.position += new Vector3(0f, 0f, 0.7f);
…As this is just setting the position of the object to a fixed position (presumably every frame).
Visual Studio will auto-import namespaces if you insert a type name under a namespace in your code. You probably did so at some point.
You can safely remove all the namespaces that are grey-d out, which means they’re not being used.
Thanks for the reply your advice worked but it didn’t move exactly the way I wanted. I did, however, come up with an idea based on your response. I’m going to have a 2nd trigger located below the first trigger. trigger1 sends the ball to trigger 2 and trigger 2 sends the ball towards the center of the table. a little complicated I know and probably unnecessary but I have to go with what I know which isn’t much yet. After reading and learning I came up with this for trigger 2:
public class Trigger2: MonoBehaviour
{
Vector3 targetPos = new Vector3(0f, 0f, 0.7f);
public float speed = 5f;
public maxDistanceDelta (13f, 0f, 0.7f);
void OnTriggerEnter(Collider ballCollider)
{
if (ballCollider.gameObject.CompareTag("Ball"))
{
transform.position = Vector3.MoveTowards (targetPos,(speed * maxDistanceeDelta * Time.deltaTime) / Vector3.Distance(targetPos.transform.position);
}
}
}
Getting error 1001 identifier expected for line 5. I have read up on identifiers but maybe you can dumb it down for me.
My take is it just means I have to name maxDistanceDelta something to be referred to later.