Hi I’m fairly new to Unity and me and my friend are trying to make a Diablo style of game for fun / hobby
I have been working on Click to Move character but I cannot seem to get the collisions to work my character it just walks thought any of my walls / objects to get to the place where I clicked on the terrain but we want it to not be able to walk thought anything
I have been using the Click to Move script from http://wiki.unity3d.com/index.php/Click_To_Move_C and it working 100% how I want apart from the collisions not working so could some one help us on how we could get collisions working please.
I have also looked at Pathfinding but this was not really what we wanted
Moving the object by setting the transform position will effectively “teleport” your object to the destination position. You need to attach a rigidbody and collider on your object and move it with Rigidbody.MovePosition (Unity - Scripting API: Rigidbody.MovePosition). This way, movement takes physics into account and takes care of handling collisions.
What would I need to change on the code I was using in order to use the Rigidbody.MovePosition ? sorry I’m kinda new to C# learning as I go also thanks for the fast reply.
Basically, you just get the Input in the Update() method and set the required variables (the destinationPosition variable). Then, in the FixedUpdate, you just move the rigidbody towards the destination position.
Try this. I wrote it in notepad it might not work but it can point you in the right direction :
using UnityEngine;
using System.Collections;
public class moveOnMouseClick : MonoBehaviour {
private Transform myTransform; // this transform
private Vector3 destinationPosition; // The destination Point
private float destinationDistance; // The distance between myTransform and destinationPosition
private Rigidbody rigid; // this rigidbody
public float moveSpeed; // The Speed the character will move
void Start () {
myTransform = transform; // sets myTransform to this GameObject.transform
destinationPosition = myTransform.position; // prevents myTransform reset
rigid = GetComponent<Rigidbody>();
}
void Update () {
//Moves the player if the mouse button is held down
if (Input.GetMouseButton(0)&& GUIUtility.hotControl ==0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destinationPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
myTransform.rotation = targetRotation;
}
}
}
void FixedUpdate() {
destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
if(destinationDistance > .5f) {
Vector3 direction = destinationPosition - myTransform.position;
direction = direction.normalized;
rigid.MovePosition(myTransform.position + direction * moveSpeed * Time.deltaTime);
}
}
}
I checked that code out it loaded with no errors but my character still just walks though everything. my Character has a Capsule Collider + the Rigibody and my objects have the Mesh Collider and also tryed with and without a Rigibody on the objects as well.
Do you use physical layers? You need to make sure the layer of your character can collide with the layer of your environment objects. Also, your colliders need to have the IsTrigger property unchecked, is that the case?
Not 100% sure on the layers question but the Layers option in the top right is set to default if that is what you mean and the IsTrigger I have it ticked on the charcter object only which I read you had to have ticked on.
I found this bit of code on the forums which collides with stuff
using UnityEngine;
using System.Collections;
public class ClickToMoveTwo : MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Only problem with this code is that it is WASD to move and not click to move and it uses Character Controller and not the Rigidbody thought I dont no if that is really a problem but i just need to edit it to Move with Click on terrain and not WASD if that is possible
I looked at the Rigidbody code again you sent and turns out I had Is Kinematic ticked on the Player and unticking it made it collide with Gameobjects now but not my problem is when I got up and Ramp then go back down it he does not stick to the ramp and kinda slow falls of it and also the character acts kinda jittery if its standing Idle I have also noticed my character starts in the ground when I click play and when I move it goes normal again :S.
Edit: the jitter stops if I tick all of the Freeze Position boxes - X - Y - Z but then I don’t collide with things again :S