Question by
TomatenPower · Oct 04, 2015 at 01:17 AM ·
c#scripting problemcompiler error
Operator `*=' cannot be applied to operands of type `UnityEngine.Vector3' and `method group'
Hi, I was following a tutorial for a 3rd person controller and have that error but it's not in the video. This is the script:
public static TPMotor Instance;
public float ForwardSpeed = 5f;
public float BackwardSpeed = 3f;
public float StrafingSpeed = 4f;
public float JumpSpeed = 3f;
public float Gravity = 9.807f;
public float TerminalVelocity = 9f;
public float SlideThreshold = 0.6f;
public float MaxControllableSlideMagnitude = 0.4f;
private Vector3 slideDirection;
public Vector3 MoveVector { get; set; }
public float VerticalVelocity { get; set;}
void Awake () {
Instance = this;
}
public void UpdateMotor () {
SnapAlignCharacterWithCamera ();
ProcessMotion ();
}
void ProcessMotion () {
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection (MoveVector);
// Normalize MoveVector if Magnitude > 1
if (MoveVector.magnitude > 1) {
MoveVector = Vector3.Normalize (MoveVector);
}
// Apply sliding if possible
ApplySlide ();
// Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed; <--- this
// Reapply verticalVelocity to MoveVector.y
MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);
// Apply gravity
ApplyGravity ();
// Move the Character in World Space
TPController.characterController.Move(MoveVector * Time.deltaTime);
}
void ApplyGravity () {
if (MoveVector.y > -TerminalVelocity) {
MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
if (TPController.characterController.isGrounded && MoveVector.y < - 1) {
MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
}
}
}
void ApplySlide () {
if (!TPController.characterController.isGrounded) {
return;
}
slideDirection = Vector3.zero;
RaycastHit hitInfo;
if (Physics.Raycast (transform.position + Vector3.up, Vector3.down, out hitInfo)) {
if (hitInfo.normal.y < SlideThreshold){
slideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
}
}
if (slideDirection.magnitude < MaxControllableSlideMagnitude) {
MoveVector += slideDirection;
} else {
MoveVector = slideDirection;
}
}
public void Jump () {
if (TPController.characterController.isGrounded) {
VerticalVelocity = JumpSpeed;
}
}
void SnapAlignCharacterWithCamera () {
if (MoveVector.x != 0 || MoveVector.z != 0) {
transform.rotation = Quaternion.Euler( transform.eulerAngles.x,
Camera.main.transform.eulerAngles.y,
transform.eulerAngles.z );
}
}
float MoveSpeed () {
var moveSpeed = 0f;
switch (TPAnimator.Instance.MoveDirection) {
case TPAnimator.Direction.Stationary: {
moveSpeed = 0;
break;
}
case TPAnimator.Direction.Fordward: {
moveSpeed = ForwardSpeed;
break;
}
case TPAnimator.Direction.Backward: {
moveSpeed = BackwardSpeed;
break;
}
case TPAnimator.Direction.Left: {
moveSpeed = StrafingSpeed;
break;
}
case TPAnimator.Direction.Right: {
moveSpeed = StrafingSpeed;
break;
}
case TPAnimator.Direction.LeftForward: {
moveSpeed = ForwardSpeed;
break;
}
case TPAnimator.Direction.RightForward: {
moveSpeed = ForwardSpeed;
break;
}
case TPAnimator.Direction.LeftBackward: {
moveSpeed = BackwardSpeed;
break;
}
case TPAnimator.Direction.RightBackward: {
moveSpeed = BackwardSpeed;
break;
}
}
return moveSpeed;
}
}
I tried to do MoveVector.magnitude *= MoveSpeed but I get get these errors:
error CS0019: Operator *=' cannot be applied to operands of type
float' and method group' and error CS0200: Property or indexer
UnityEngine.Vector3.magnitude' cannot be assigned to (it is read only)
Thanks for your help
Comment
Answer by MakeCodeNow · Oct 04, 2015 at 03:05 AM
The problem is that you can't assign directly to the magnitude of a vector, which is the = part of *=.
I've already figured that out, but I'm still getting errors.