- Home /
Problems with using gravity and also MoveTowards/LookAt functions
Hello again!
I`ve got a new Question:
I got very big problems with moving an object between two points. I got a start and a end vector (3d space) and i want to code that there is a gameObject moving between this two points.
I use MoveTowards and Look at to orientate the gameobject between this two target points.On the other side i have to set up that the gameobject isGrounded. But every time i push the play button, the gameobject flies between this two target points and isnt grounded.
Can someone help me please? How can i code that? the problem is that even if i use the vector3 coordinates the y coordinate will be calculatet too. So i thinkt i have to code that this gameobject has to use only the x and the z axis.
(the two targets (start and end) can be placed on any point on the landscape - the gameobject should even move between them)
Thank you so much for your time!
the code is in javascript:
private var wolf:GameObject;
private var changePosition:boolean
private var characterController : CharacterController
var gravity:int
private var moveDirection:Vector3
private var posA:Vector3
private var posB:Vector3
private var currentPos:Vector3
private var targetPos:Vector3
private var grounded:boolean = false
public var changeTarget:boolean
var targetEnd:Transform
var targetStart:Transform
(some variables left because theyre not important)
function Start () { //For Random Movement inside the MoveArea / enemyCol = enemyCollider.GetComponent(SphereCollider); /
wolf = GameObject.FindGameObjectWithTag("Wolf");
targetPos = targetEnd.transform.position;
changePosition = true;
characterController = transform.GetComponent(CharacterController);
posA = enemyCollider.transform.position;
}
function Update () { var step = speed * Time.deltaTime; posB = targetEnd.transform.position; currentPos = wolf.transform.position;
grounded = characterController.isGrounded;
if(grounded)
{
if(insideCollider == true)
{
wolf.transform.position = Vector3.MoveTowards(wolf.transform.position, characterPlayer.transform.position, step);
wolf.transform.LookAt(Vector3(characterPlayer.position.x, transform.position.y, characterPlayer.position.z));
speed = 10;
animation.Blend("walk", 5);
animation.CrossFade("run");
}
if(insideCollider == false)
{
if(changeTarget == true)
{
if(targetPos == targetEnd.transform.position)
{
targetPos = targetStart.transform.position;
} else {
targetPos = targetEnd.transform.position;
}
changeTarget = false;
}
if(turnMovement == true)
{
wolf.transform.Translate(Vector3.forward * Time.deltaTime * 1);
wolf.transform.localEulerAngles.y = wolf.transform.localEulerAngles.y + rotationSpeed;
targetAngle = Mathf.Atan((currentPos.y - targetPos.y) / (currentPos.x - targetPos.x)) * 180/Mathf.PI;
if(wolf.transform.localEulerAngles.y <= targetAngle + rotationSpeed * 2 || wolf.transform.localEulerAngles.y >= targetAngle - rotationSpeed * 2)
{
turnMovement = false;
}
}
if(turnMovement == false)
{
//wolf.transform.position = Vector3.MoveTowards(currentPos, targetPos, step);
wolf.transform.LookAt(targetPos);
}
speed = 3;
animation.Blend("run", 5);
animation.CrossFade("walk");
}
}else{
applyGravity();
}
//For Random Movement inside the MoveArea
/*
if(changePosition == true)
{
animalLocation();
}
*/
Debug.Log(grounded);
} function applyGravity () { Debug.Log("grounded");
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
}
Answer by awest · Jul 14, 2014 at 12:41 PM
To use only 2 axis, you can set up the Vector3 variables with one axis as the others y. Then MoveTowards that.
var Vect3 = Vector3(wolf.transform.position.x, characterPlayer.transform.position.y, wolf.transform.position.z);
As far as it shooting across the screen. you don't show where you set the Vector3 characterPlayer that you use as an end point. If it's heading towards the object that the script is attached to, you can just use transform without a GameObject referance.
You also don't show where you defined speed before you use it, but I think that's less likely the case.
CharacterController checks for grounded using collision at the bottom of the capsule so Check your scene to make sure it isn't hitting any colliders, even of parent or child objects.
Answer by THDevelopment · Jul 15, 2014 at 07:58 AM
uhm i want to move my main object between two vector3.points - the following code should move it between the two points. (it works but the problem is that this gameObject isnt grounded when it is moving)
wolf.transform.position = Vector3.MoveTowards(currentPos, targetPos, step); wolf.transform.LookAt(Vector3(characterPlayer.position.x, transform.position.y, characterPlayer.position.z));
you can see i use Vector3.MoveTowards and transform.LookAt. Basically im not sure if it is possible to move this object on the ground if i use this two functions, because it interpolates the height of the two target Points (vector3) i have made.
on the wolf, there is a character controller which i gave a gravity in my code.
my question:
is it possible to make a object grounded, if it uses two target points i set up in my scene? i have to call the x and the z axis and not the y axis if i want to give him the target.coordinates
like wolf.transform.position = Vector3(targetPoint.x, ?????, targetPoint.z);
i dont know how to declarate that the wolf shouldnt orientate the y position of the target points - but how can i do that?
I think i understand the problem now. $$anonymous$$oveTowards isn't great for that. A simpler solution than trying to get those calculations would be to use a rigidbody on the wolf and set the velocity with the target position. Then you can use gravity.
Your answer
Follow this Question
Related Questions
Applying force to a rigidbody 2 Answers
Make box tip over edge of platform 1 Answer
Enemy Ai Problem with the collider and gravity 1 Answer
Rigidbody--Addforce on a Spherical Platform(A Globe) 2 Answers