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);
}