Home > other >  Unable to acquire role controller ways to change the position object
Unable to acquire role controller ways to change the position object

Time:09-30

Want to do a simple transfer effect, build a simple scenario, two platform, a capsule body (add the character controller components), a small square is responsible for the ACTS as a transfer matrix
As is shown in



Transfer matrix of the script is simple, it is whether collision object name is players
Public class Teleporter: MonoBehaviour
{
CharacterController cc.

Private void OnTriggerEnter (starts other)
{
If (other. GameObject. Name=="Player")
{
//the Debug Log (" collide ");//collision test whether
Cc=other. GetComponent (a);
The Debug Log (cc. GameObject. Name);//test whether cc was given the role of the player controller component
Cc. The transform. The position=new Vector3 (15, 1, 0);
The Debug Log (cc) transform) position. X);//test if cc changed position
}
}
}

Results after the operation found that players will not be changed, and the console to play out the information display cc indeed change the position x axis

CodePudding user response:

Test your code, no problem, you check the mobile location is correct, whether there is square and the controller is the trigger

CodePudding user response:

1/f, residual reference both failed Pi response:
tested your code, no problem, you check the mobile location is correct, whether there is square and the controller is the trigger


Position must be correct, the debug also show a collision happens, the cc also won the new coordinates, but the player is not being moved to a new location, squares must be the trigger, I set the trigger role

CodePudding user response:

After many tests and find information, found that after using CharacterController component, if used in a script. Move command to control the role, so this way of mobile cannot be other CharacterController variable gain control, like I this case, the transfer matrix of the script in the cc, although the collision (players) of the information, but does not change its position,
CharacterController use and copy the official code, didn't I think of is this hidden bug
Public class ExampleClass: MonoBehaviour
{
CharacterController CharacterController;
Public float speed=6.0 f;
Public float jumpSpeed=8.0 f;
Public float gravity=20.0 f;
Private Vector3 moveDirection=Vector3. Zero;
Void the Start ()
{
CharacterController=GetComponent (a);
}
Void the Update ()
{
If (characterController. IsGrounded)
{
MoveDirection=new Vector3 (Input. GetAxis (" Horizontal "), 0.0 f, Input. GetAxis (" Vertical "));
MoveDirection *=speed;
If (Input. GetButton (" Jump "))
{
MoveDirection. Y=jumpSpeed;
}
}
MoveDirection. Y -=gravity * Time. DeltaTime;
CharacterController. Move (moveDirection * Time. DeltaTime);
}
}

Later changed to use the transform. Translate commands to control the role, can be smoothly method of array transmitting the
But that there is a problem, since the use of the transform. Translate, why does it take characterController why?

CodePudding user response:

reference weixin_40527966 reply: 3/f
after many tests and find information, found that after using CharacterController component, if used in a script. Move command to control the role, so this way of mobile cannot be other CharacterController variable gain control, like I this case, the transfer matrix of the script in the cc, although the collision (players) of the information, but does not change its position,
CharacterController use and copy the official code, didn't I think of is this hidden bug
Public class ExampleClass: MonoBehaviour
{
CharacterController CharacterController;
Public float speed=6.0 f;
Public float jumpSpeed=8.0 f;
Public float gravity=20.0 f;
Private Vector3 moveDirection=Vector3. Zero;
Void the Start ()
{
CharacterController=GetComponent (a);
}
Void the Update ()
{
If (characterController. IsGrounded)
{
MoveDirection=new Vector3 (Input. GetAxis (" Horizontal "), 0.0 f, Input. GetAxis (" Vertical "));
MoveDirection *=speed;
If (Input. GetButton (" Jump "))
{
MoveDirection. Y=jumpSpeed;
}
}
MoveDirection. Y -=gravity * Time. DeltaTime;
CharacterController. Move (moveDirection * Time. DeltaTime);
}
}

Later changed to use the transform. Translate commands to control the role, can be smoothly method of array transmitting the
But that there is a problem, since the use of the transform. Translate, why does it take characterController why?

Can you start code is not most direct assignment location, this should not affect it

CodePudding user response:

residual Pi crashing in both reference 4 floor response:
but you the first code is not direct assignment location, this should not affect


Yes, in the transfer matrix is indeed a direct assignment, the debug of cc. The transform. The position. X is changed, but the player role actually did not change
You can try to build a simple scenario (I this is to test the simple structures)

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - to switch to the transform. The Translate is no problem, but this not collision objects, characters and through the wall directly
  • Related